Skip to content

Instantly share code, notes, and snippets.

@t81lal
Created February 19, 2017 00:09
Show Gist options
  • Save t81lal/ef85a81ce8d2e11b1cc95a4acae71e4e to your computer and use it in GitHub Desktop.
Save t81lal/ef85a81ce8d2e11b1cc95a4acae71e4e to your computer and use it in GitHub Desktop.
IteratorIterator.java
package org.mapleir.stdlib.collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public abstract class IteratorIterator<T> implements Iterator<T> {
protected Iterator<T> current;
private boolean findNext() {
Iterator<T> nextIt = nextIterator();
if(nextIt == null) {
return false;
} else {
/* If this next iterator has no
* elements in it, the next call to
* hasNext will get a new iterator,
* checking it until we find a
* valid one. */
current = nextIt;
return hasNext(nextIt);
}
}
private boolean hasNext(Iterator<T> it) {
if(it == null) {
return findNext();
} else {
if(it.hasNext()) {
return true;
} else {
return findNext();
}
}
}
@Override
public boolean hasNext() {
return hasNext(current);
}
@Override
public T next() {
return current.next();
}
public abstract Iterator<T> nextIterator();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment