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    import org.apache.directory.shared.i18n.I18n;
027    
028    
029    /**
030     * An Iterator that joins the results of many iterators.
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev: 919765 $
034     */
035    public class JoinIterator implements Iterator
036    {
037        /** the iterators whose results are joined */
038        private final Iterator[] iterators;
039    
040        private int index;
041    
042    
043        /**
044         * Creates an Iterator that joins other Iterators.
045         * 
046         * @param iterators
047         *            the Iterators whose results are joined
048         * @throws IllegalArgumentException
049         *             if a null array argument, or one with less than 2 elements is
050         *             used
051         */
052        public JoinIterator(Iterator[] iterators)
053        {
054            if ( iterators == null || iterators.length < 2 )
055            {
056                throw new IllegalArgumentException( I18n.err( I18n.ERR_04397 ) );
057            }
058    
059            if ( iterators != null )
060            {
061                this.iterators = new Iterator[ iterators.length ];
062                System.arraycopy( iterators, 0, this.iterators, 0, iterators.length );
063            } else {
064                this.iterators = null;
065            }
066            this.index = 0;
067        }
068    
069    
070        public void remove()
071        {
072            throw new UnsupportedOperationException();
073        }
074    
075    
076        public boolean hasNext()
077        {
078            for ( /** nada */
079            ; index < iterators.length; index++ )
080            {
081                if ( iterators[index].hasNext() )
082                {
083                    return true;
084                }
085            }
086    
087            return false;
088        }
089    
090    
091        public Object next()
092        {
093            for ( /** nada */
094            ; index < iterators.length; index++ )
095            {
096                if ( iterators[index].hasNext() )
097                {
098                    return iterators[index].next();
099                }
100            }
101    
102            throw new NoSuchElementException();
103        }
104    }