ch.obermuhlner.scriptengine.java.util.CompositeIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-scriptengine Show documentation
Show all versions of java-scriptengine Show documentation
Java script engine for Java as a scripting language.
package ch.obermuhlner.scriptengine.java.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A {@link Iterator} that will iterate over several iterators.
*
* @param the type of elements returned by this iterator
*/
public class CompositeIterator implements Iterator {
private final Iterator extends T>[] iterators;
private int iteratorIndex = 0;
/**
* Creates a {@link CompositeIterator} over the specified iterators.
*
* @param iterators the {@link Iterator}s
*/
public CompositeIterator(Iterator extends T>... iterators) {
this.iterators = iterators;
}
@Override
public boolean hasNext() {
if (iteratorIndex >= iterators.length) {
return false;
}
if (iterators[iteratorIndex].hasNext()) {
return true;
}
iteratorIndex++;
if (iteratorIndex >= iterators.length) {
return false;
}
return iterators[iteratorIndex].hasNext();
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return iterators[iteratorIndex].next();
}
}