Last active
November 26, 2015 00:23
-
-
Save reutsharabani/03a457b401b81e448ce2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Deque; | |
import java.util.Iterator; | |
import java.util.ArrayDeque; | |
public class ExtendableIterator<T> implements Iterator<T> { | |
public Deque<Iterator<T>> its = new ArrayDeque<Iterator<T>>(); | |
public ExtendableIterator() { | |
} | |
public ExtendableIterator(Iterator<T> it) { | |
this(); | |
this.extend(it); | |
} | |
@Override | |
public boolean hasNext() { | |
// this is true since we never hold empty iterators | |
return !its.isEmpty() && its.peekLast().hasNext(); | |
} | |
@Override | |
public T next() { | |
T next = its.peekFirst().next(); | |
if (!its.peekFirst().hasNext()) { | |
its.removeFirst(); | |
} | |
return next; | |
} | |
public void extend(Iterator<T> it) { | |
if (it.hasNext()) { | |
its.addLast(it); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment