All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.archive.util.iterator.CompositeIterator Maven / Gradle / Ivy

Go to download

The Archive Commons Code Libraries project contains general Java utility libraries, as used by the Heritrix crawler and other projects.

There is a newer version: 3.4.0-20220727
Show newest version
/*
 *  This file is part of the Heritrix web crawler (crawler.archive.org).
 *
 *  Licensed to the Internet Archive (IA) by one or more individual 
 *  contributors. 
 *
 *  The IA licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.archive.util.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * An iterator that's built up out of any number of other iterators.
 * @author gojomo
 */
public class CompositeIterator implements Iterator {
    protected ArrayList> iterators = new ArrayList>();
    protected Iterator currentIterator;
    protected int indexOfCurrentIterator = -1;

    /**
     * Moves to the next (non empty) iterator. Returns false if there are no
     * more (non empty) iterators, true otherwise.
     * @return false if there are no more (non empty) iterators, true otherwise.
     */
    private boolean nextIterator() {
        if (++indexOfCurrentIterator < iterators.size()) {
            currentIterator = iterators.get(indexOfCurrentIterator);
            // If the new iterator was empty this will move us to the next one.
            return hasNext();
        } else {
            currentIterator = null;
            return false;
        }
    }

    /* (non-Javadoc)
     * @see java.util.Iterator#hasNext()
     */
    public boolean hasNext() {
        if(currentIterator!=null && currentIterator.hasNext()) {
            // Got more
            return true;
        } else {
            // Have got more if we can queue up a new iterator.
            return nextIterator();
        }
    }

    /* (non-Javadoc)
     * @see java.util.Iterator#next()
     */
    public E next() {
        if(hasNext()) {
            return currentIterator.next();
        } else {
            throw new NoSuchElementException();
        }
    }

    /* (non-Javadoc)
     * @see java.util.Iterator#remove()
     */
    public void remove() {
        throw new UnsupportedOperationException();
    }

    /**
     * Create an empty CompositeIterator. Internal
     * iterators may be added later.
     */
    public CompositeIterator() {
        super();
    }

    /**
     * Convenience method for concatenating together
     * two iterators.
     * @param i1
     * @param i2
     */
    public CompositeIterator(Iterator i1, Iterator i2) {
        this();
        add(i1);
        add(i2);
    }

    /**
     * Add an iterator to the internal chain.
     *
     * @param i an iterator to add.
     */
    public void add(Iterator i) {
        iterators.add(i);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy