001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.util;
021    
022    
023    import java.util.Iterator;
024    import java.util.NoSuchElementException;
025    
026    
027    /**
028     * An Iterator that joins the results of many iterators.
029     * 
030     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031     * @version $Rev: 596943 $
032     */
033    public class JoinIterator implements Iterator
034    {
035        /** the iterators whose results are joined */
036        private final Iterator[] iterators;
037    
038        private int index;
039    
040    
041        /**
042         * Creates an Iterator that joins other Iterators.
043         * 
044         * @param iterators
045         *            the Iterators whose results are joined
046         * @throws IllegalArgumentException
047         *             if a null array argument, or one with less than 2 elements is
048         *             used
049         */
050        public JoinIterator(Iterator[] iterators)
051        {
052            if ( iterators == null || iterators.length < 2 )
053            {
054                throw new IllegalArgumentException( "Iterator[] arg must not be "
055                    + "null, empty or composed of less than two Iterators" );
056            }
057    
058            if ( iterators != null )
059            {
060                this.iterators = new Iterator[ iterators.length ];
061                System.arraycopy( iterators, 0, this.iterators, 0, iterators.length );
062            } else {
063                this.iterators = null;
064            }
065            this.index = 0;
066        }
067    
068    
069        public void remove()
070        {
071            throw new UnsupportedOperationException();
072        }
073    
074    
075        public boolean hasNext()
076        {
077            for ( /** nada */
078            ; index < iterators.length; index++ )
079            {
080                if ( iterators[index].hasNext() )
081                {
082                    return true;
083                }
084            }
085    
086            return false;
087        }
088    
089    
090        public Object next()
091        {
092            for ( /** nada */
093            ; index < iterators.length; index++ )
094            {
095                if ( iterators[index].hasNext() )
096                {
097                    return iterators[index].next();
098                }
099            }
100    
101            throw new NoSuchElementException();
102        }
103    }