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

org.daisy.dotify.common.collection.CompoundIterator Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package org.daisy.dotify.common.collection;

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

/**
 * Provides an iterator for a collection of iterables.
 *
 * @param  the type of iterator
 * @author Joel Håkansson
 */
public class CompoundIterator implements Iterator {
    List> iterators;

    /**
     * Creates a new compound iterator.
     *
     * @param iterables the iterables to use in this iterator
     */
    public CompoundIterator(Iterable> iterables) {
        iterators = new ArrayList<>();
        for (Iterable e : iterables) {
            iterators.add(e.iterator());
        }
    }

    @Override
    public boolean hasNext() {
        for (Iterator e : iterators) {
            if (e.hasNext()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public T next() {
        for (Iterator e : iterators) {
            if (e.hasNext()) {
                return e.next();
            }
        }
        throw new NoSuchElementException();
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy