org.daisy.dotify.common.collection.CompoundIterator Maven / Gradle / Ivy
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 extends 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();
}
}