001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.repository;
017
018import java.util.HashSet;
019import java.util.List;
020import java.util.NoSuchElementException;
021import java.util.Set;
022import java.util.stream.Collectors;
023
024/**
025 * Implementation of a {@link AmetysObjectIterable} based on a list of others {@link AmetysObjectIterable},
026 * returning duplicate objects only once.
027 * @param <A> the actual type of {@link AmetysObject}s.
028 */
029public class UniqueChainedAmetysObjectIterable<A extends AmetysObject> implements AmetysObjectIterable<A>
030{
031    private List<AmetysObjectIterable<A>> _iterables;
032    
033    /**
034     * Creates a {@link UniqueChainedAmetysObjectIterable}.
035     * @param iterables a list of {@link AmetysObjectIterable}.
036     */
037    public UniqueChainedAmetysObjectIterable(List<AmetysObjectIterable<A>> iterables)
038    {
039        _iterables = iterables;
040    }
041    
042    @Override
043    public long getSize()
044    {
045        return -1;
046    }
047    
048    public AmetysObjectIterator<A> iterator()
049    {
050        List<AmetysObjectIterator<A>> its = _iterables.stream().map(it -> it.iterator()).collect(Collectors.toList());
051        return new UniqueChainedIterator(its);
052    }
053    
054    public void close()
055    {
056        for (AmetysObjectIterable<A> it : _iterables)
057        {
058            it.close();
059        }
060    }
061    
062    class UniqueChainedIterator implements AmetysObjectIterator<A>
063    {
064        private Set<String> _identifiers = new HashSet<>(); 
065        private A _nextObject;
066        private boolean _nextObjectSet;
067        private long _position;
068        private int _currentIterator;
069        private List<AmetysObjectIterator<A>> _its;
070        
071        public UniqueChainedIterator(List<AmetysObjectIterator<A>> its)
072        {
073            _its = its;
074        }
075        
076        @Override
077        public long getSize()
078        {
079            return -1;
080        }
081        
082        public long getPosition()
083        {
084            return _position;
085        }
086        
087        @Override
088        public boolean hasNext()
089        {
090            if (_nextObjectSet)
091            {
092                return true;
093            }
094            else
095            {
096                return _fetchNextObject();
097            }
098        }
099        
100        @Override
101        public A next()
102        {
103            if (!_nextObjectSet)
104            {
105                if (!_fetchNextObject())
106                {
107                    throw new NoSuchElementException();
108                }
109            }
110            
111            _nextObjectSet = false;
112            _position++;
113            return _nextObject;
114        }
115        
116        /**
117         * Set nextObject to the next object. 
118         * @return If there are no more objects then return false. Otherwise, return true.
119         */
120        private boolean _fetchNextObject()
121        {
122            while (_internalHasNext())
123            {
124                A object = _internalNext();
125                // Will return true if the object did not exist.
126                if (_identifiers.add(object.getId()))
127                {
128                    _nextObject = object;
129                    _nextObjectSet = true;
130                    return true;
131                }
132            }
133            return false;
134        }
135        
136        private boolean _internalHasNext()
137        {
138            while (_currentIterator < _its.size() && !_its.get(_currentIterator).hasNext())
139            {
140                _currentIterator++;
141            }
142            
143            return _currentIterator < _its.size() && _its.get(_currentIterator).hasNext();
144        }
145        
146        private A _internalNext()
147        {
148            return _its.get(_currentIterator).next();
149        }
150    }
151}