org.snapscript.common.CompoundIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap-all Show documentation
Show all versions of snap-all Show documentation
Dynamic scripting for the JVM
package org.snapscript.common;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class CompoundIterator implements Iterator{
private Iterator[] iterators;
private Object next;
private Set done;
private int index;
public CompoundIterator(Iterator... iterators) {
this.done = new HashSet();
this.iterators = iterators;
}
@Override
public boolean hasNext() {
if(next == null) {
while(index < iterators.length) {
Iterator iterator = iterators[index];
while(iterator.hasNext()) {
Object value = iterator.next();
if(done.add(value)) {
next = value;
return true;
}
}
index++;
}
}
return next != null;
}
@Override
public T next() {
Object local = next;
if(local == null) {
if(!hasNext()) {
return null;
}
local = next;
}
next = null;
return (T)local;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not supported");
}
}