com.jsftoolkit.utils.ConcatenatedIterator Maven / Gradle / Ivy
Go to download
The core classes for the JSF Toolkit Component Framework. Includes all
framework base and utility classes as well as component
kick-start/code-generation and registration tools.
Also includes some classes for testing that are reused in other projects.
They cannot be factored out into a separate project because they are
referenced by the tests and they reference this code (circular
dependence).
The newest version!
package com.jsftoolkit.utils;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Returns the results of a series of iterators, in the order they were provided
* to the constructor.
*
* @author noah
*
* @param
*/
public class ConcatenatedIterator implements Iterator {
private final Iterator extends T>[] iterators;
private int lastReturned;
private int next;
public ConcatenatedIterator(final Iterator extends T>... iterators) {
super();
this.iterators = iterators;
}
public boolean hasNext() {
boolean result = next < iterators.length && iterators[next].hasNext();
while (!result && next < iterators.length - 1) {
result = iterators[++next].hasNext();
}
return result;
}
public T next() {
// find the next iterator with an element
if (!hasNext()) {
throw new NoSuchElementException();
}
return iterators[lastReturned = next].next();
}
public void remove() {
iterators[lastReturned].remove();
}
}