org.archive.util.iterator.CloseableCompositeIterator Maven / Gradle / Ivy
package org.archive.util.iterator;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
public class CloseableCompositeIterator implements CloseableIterator {
protected LinkedList> iters;
protected Iterator> iterPtr;
protected CloseableIterator currIter;
public CloseableCompositeIterator()
{
iters = new LinkedList>();
}
public void addFirst(CloseableIterator e)
{
iters.addFirst(e);
}
public void addLast(CloseableIterator e)
{
iters.addLast(e);
}
@Override
public boolean hasNext() {
if (iterPtr == null) {
iterPtr = iters.iterator();
currIter = iterPtr.next();
}
if (currIter == null) {
return false;
}
while (currIter != null) {
if (currIter.hasNext()) {
return true;
}
currIter = (iterPtr.hasNext() ? iterPtr.next() : null);
}
return false;
}
@Override
public E next() {
return currIter.next();
}
@Override
public void remove() {
currIter.remove();
}
@Override
public void close() throws IOException {
for (CloseableIterator e : iters) {
if (e != null) {
try {
e.close();
} catch (IOException io) {
}
}
}
}
}