Last active
September 14, 2017 18:59
-
-
Save VenkataRaju/b886b8ef6b5132bc423a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public abstract class AbstractIterator<T> implements Iterator<T> | |
{ | |
private boolean hasMoreData = true, nextComputed; | |
private T data; | |
@Override | |
public final boolean hasNext() | |
{ | |
if (hasMoreData && !nextComputed) | |
{ | |
data = computeNext(); | |
nextComputed = true; | |
} | |
return hasMoreData; | |
} | |
@Override | |
public final T next() | |
{ | |
if (!hasNext()) | |
throw new NoSuchElementException(); | |
nextComputed = false; | |
return data; | |
} | |
protected abstract T computeNext(); | |
protected final T endOfData() | |
{ | |
hasMoreData = false; | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment