Skip to content

Instantly share code, notes, and snippets.

@VenkataRaju
Last active September 14, 2017 18:59
Show Gist options
  • Save VenkataRaju/b886b8ef6b5132bc423a to your computer and use it in GitHub Desktop.
Save VenkataRaju/b886b8ef6b5132bc423a to your computer and use it in GitHub Desktop.
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