
be.bagofwords.iterator.InterleavedIteratorOfIterators Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bow-utils Show documentation
Show all versions of bow-utils Show documentation
Utility classes that are used in the count-db project and other bow-* projects
package be.bagofwords.iterator;
import java.util.List;
public class InterleavedIteratorOfIterators extends CloseableIterator {
private final List> iterators;
private int currPos;
private U next;
public InterleavedIteratorOfIterators(List> iterators) {
this.iterators = iterators;
this.currPos = 0;
next = findNext();
}
private U findNext() {
U result = null;
for (int i = 0; i < iterators.size(); i++) {
if (iterators.get(currPos).hasNext()) {
result = iterators.get(currPos).next();
break;
} else {
currPos = (currPos + 1) % iterators.size();
}
}
currPos = (currPos + 1) % iterators.size();
return result;
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public U next() {
U result = next;
next = findNext();
return result;
}
@Override
public void closeInt() {
for (CloseableIterator iterator : iterators) {
iterator.close();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy