-
-
Save gabhi/631942b2c81e2e64024b to your computer and use it in GitHub Desktop.
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
public class FilteredIterator<T> implements Iterator<T> { | |
private Iterator<? extends T> iterator; | |
private Filter<T> filter; | |
private T nextElement; | |
private boolean hasNext; | |
/** | |
* Creates a new FilteredIterator using wrapping the iterator and returning only elements matching the filter. | |
* | |
* @param iterator | |
* the iterator to wrap | |
* @param filter | |
* elements must match this filter to be returned | |
*/ | |
public FilteredIterator(Iterator<? extends T> iterator, Filter<T> filter) { | |
this.iterator = iterator; | |
this.filter = filter; | |
nextMatch(); | |
} | |
@Override | |
public boolean hasNext() { | |
return hasNext; | |
} | |
@Override | |
public T next() { | |
if (!hasNext) { | |
throw new NoSuchElementException(); | |
} | |
return nextMatch(); | |
} | |
private T nextMatch() { | |
T oldMatch = nextElement; | |
while (iterator.hasNext()) { | |
T o = iterator.next(); | |
if (filter.matches(o)) { | |
hasNext = true; | |
nextElement = o; | |
return oldMatch; | |
} | |
} | |
hasNext = false; | |
return oldMatch; | |
} | |
@Override | |
public void remove() { | |
throw new UnsupportedOperationException(); | |
} | |
} | |
public interface Filter<T> { | |
/** | |
* Determines whether elements should be filtered or not. | |
* | |
* @param element the element to be matched against the filter | |
* @return {@code true} if the element matches the filter, otherwise {@code false} | |
*/ | |
public boolean matches(T element); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment