com.tinkerpop.pipes.util.iterators.MultiIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pipes Show documentation
Show all versions of pipes Show documentation
Pipes is a dataflow framework written in Java that enables the splitting, merging, filtering, and
transformation of data from input to output.
Computations are expressed using a combinator model and are evaluated in a memory-efficient, lazy fashion.
package com.tinkerpop.pipes.util.iterators;
import com.tinkerpop.pipes.util.FastNoSuchElementException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* MultiIterator takes multiple iterators in its constructor and makes them behave like a single iterator.
* The order in which objects are next()'d are with respect to the order of the iterators passed into the constructor.
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class MultiIterator implements Iterator {
private final Iterator> iterators;
private Iterator currentIterator = null;
public MultiIterator(final Iterator... iterators) {
this(Arrays.asList(iterators));
}
public MultiIterator(final List> iterators) {
this.iterators = iterators.iterator();
if (this.iterators.hasNext())
this.currentIterator = this.iterators.next();
}
public void remove() {
throw new UnsupportedOperationException();
}
public T next() {
while (true) {
if (this.currentIterator.hasNext()) {
return this.currentIterator.next();
} else {
if (this.iterators.hasNext()) {
this.currentIterator = this.iterators.next();
} else {
throw FastNoSuchElementException.instance();
}
}
}
}
public boolean hasNext() {
while (true) {
if (null != this.currentIterator && this.currentIterator.hasNext()) {
return true;
} else if (this.iterators.hasNext()) {
this.currentIterator = iterators.next();
} else {
return false;
}
}
}
}