Created
January 7, 2015 18:37
-
-
Save RX14/96b67545a1c7a3bd1907 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
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