Created
February 19, 2017 00:09
-
-
Save t81lal/ef85a81ce8d2e11b1cc95a4acae71e4e to your computer and use it in GitHub Desktop.
IteratorIterator.java
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
package org.mapleir.stdlib.collections; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.Set; | |
public abstract class IteratorIterator<T> implements Iterator<T> { | |
protected Iterator<T> current; | |
private boolean findNext() { | |
Iterator<T> nextIt = nextIterator(); | |
if(nextIt == null) { | |
return false; | |
} else { | |
/* If this next iterator has no | |
* elements in it, the next call to | |
* hasNext will get a new iterator, | |
* checking it until we find a | |
* valid one. */ | |
current = nextIt; | |
return hasNext(nextIt); | |
} | |
} | |
private boolean hasNext(Iterator<T> it) { | |
if(it == null) { | |
return findNext(); | |
} else { | |
if(it.hasNext()) { | |
return true; | |
} else { | |
return findNext(); | |
} | |
} | |
} | |
@Override | |
public boolean hasNext() { | |
return hasNext(current); | |
} | |
@Override | |
public T next() { | |
return current.next(); | |
} | |
public abstract Iterator<T> nextIterator(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment