Package com.twelvemonkeys.util
Class FilterIterator<E>
- java.lang.Object
-
- com.twelvemonkeys.util.FilterIterator<E>
-
- All Implemented Interfaces:
java.util.Iterator<E>
public class FilterIterator<E> extends java.lang.Object implements java.util.Iterator<E>
Wraps (decorates) anIterator
with extra functionality, to allow element filtering. Each element is filtered against the givenFilter
, and only elements that areaccept
ed are returned by thenext
method.The optional
remove
operation is implemented, but may throwUnsupportedOperationException
if the underlying iterator does not support the remove operation.- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/FilterIterator.java#1 $
- See Also:
FilterIterator.Filter
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
FilterIterator.Filter<E>
Used to tests whether or not an element fulfills certain criteria, and hence should be accepted by the FilterIterator instance.
-
Constructor Summary
Constructors Constructor Description FilterIterator(java.util.Iterator<E> pIterator, FilterIterator.Filter<E> pFilter)
Creates aFilterIterator
that wraps theIterator
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
hasNext()
Returnstrue
if the iteration has more elements.E
next()
Returns the next element in the iteration.void
remove()
Removes from the underlying collection the last element returned by the iterator (optional operation).
-
-
-
Field Detail
-
filter
protected final FilterIterator.Filter<E> filter
-
iterator
protected final java.util.Iterator<E> iterator
-
next
private E next
-
current
private E current
-
-
Constructor Detail
-
FilterIterator
public FilterIterator(java.util.Iterator<E> pIterator, FilterIterator.Filter<E> pFilter)
Creates aFilterIterator
that wraps theIterator
. Each element is filtered against the givenFilter
, and only elements that areaccept
ed are returned by thenext
method.- Parameters:
pIterator
- the iterator to filterpFilter
- the filter- See Also:
FilterIterator.Filter
-
-
Method Detail
-
hasNext
public boolean hasNext()
Returnstrue
if the iteration has more elements. (In other words, returnstrue
ifnext
would return an element rather than throwing an exception.)- Specified by:
hasNext
in interfacejava.util.Iterator<E>
- Returns:
true
if the iterator has more elements.- See Also:
FilterIterator.Filter.accept(E)
-
next
public E next()
Returns the next element in the iteration.- Specified by:
next
in interfacejava.util.Iterator<E>
- Returns:
- the next element in the iteration.
- See Also:
FilterIterator.Filter.accept(E)
-
remove
public void remove()
Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call tonext
. The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.- Specified by:
remove
in interfacejava.util.Iterator<E>
-
-