Skip to content

Instantly share code, notes, and snippets.

@RX14
Created January 7, 2015 18:37
Show Gist options
  • Save RX14/96b67545a1c7a3bd1907 to your computer and use it in GitHub Desktop.
Save RX14/96b67545a1c7a3bd1907 to your computer and use it in GitHub Desktop.
package com.github.blamevic.enumerators;
import java.util.Iterator;
import java.util.NoSuchElementException;
class EnumeratorIterator<E> implements Iterator<E>
{
private Enumerator<E> enumerator;
private E next;
private boolean hasNext;
public EnumeratorIterator(Enumerator<E> enumerator)
{
this.enumerator = enumerator;
this.next = enumerator.current();
this.hasNext = true;
}
@Override
public boolean hasNext()
{
setNext();
return hasNext;
}
@Override
public E next()
{
setNext();
if (!hasNext) throw new NoSuchElementException();
hasNext = false;
return next;
}
private void setNext()
{
if (hasNext) return;
if (enumerator.moveNext())
{
this.next = enumerator.current();
hasNext = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment