public class FilterIterator
extends java.lang.Object
implements java.util.Iterator
Iterator
using a predicate to filter elements.
This iterator decorates the underlying iterator, only allowing through
those elements that match the specified Predicate
.
Modifier and Type | Field and Description |
---|---|
private java.util.Iterator |
iterator
The iterator being used
|
private java.lang.Object |
nextObject
The next object in the iteration
|
private boolean |
nextObjectSet
Whether the next object has been calculated yet
|
private Predicate |
predicate
The predicate being used
|
Constructor and Description |
---|
FilterIterator()
Constructs a new
FilterIterator that will not function
until setIterator is invoked. |
FilterIterator(java.util.Iterator iterator)
Constructs a new
FilterIterator that will not function
until setPredicate is invoked. |
FilterIterator(java.util.Iterator iterator,
Predicate predicate)
Constructs a new
FilterIterator that will use the
given iterator and predicate. |
Modifier and Type | Method and Description |
---|---|
java.util.Iterator |
getIterator()
Gets the iterator this iterator is using.
|
Predicate |
getPredicate()
Gets the predicate this iterator is using.
|
boolean |
hasNext()
Returns true if the underlying iterator contains an object that
matches the predicate.
|
java.lang.Object |
next()
Returns the next object that matches the predicate.
|
void |
remove()
Removes from the underlying collection of the base iterator the last
element returned by this iterator.
|
void |
setIterator(java.util.Iterator iterator)
Sets the iterator for this iterator to use.
|
private boolean |
setNextObject()
Set nextObject to the next object.
|
void |
setPredicate(Predicate predicate)
Sets the predicate this the iterator to use.
|
private java.util.Iterator iterator
private Predicate predicate
private java.lang.Object nextObject
private boolean nextObjectSet
public FilterIterator()
FilterIterator
that will not function
until setIterator
is invoked.public FilterIterator(java.util.Iterator iterator)
FilterIterator
that will not function
until setPredicate
is invoked.iterator
- the iterator to usepublic FilterIterator(java.util.Iterator iterator, Predicate predicate)
FilterIterator
that will use the
given iterator and predicate.iterator
- the iterator to usepredicate
- the predicate to usepublic boolean hasNext()
hasNext
in interface java.util.Iterator
java.lang.NullPointerException
- if either the iterator or predicate are nullpublic java.lang.Object next()
next
in interface java.util.Iterator
java.lang.NullPointerException
- if either the iterator or predicate are nulljava.util.NoSuchElementException
- if there are no more elements that
match the predicatepublic void remove()
next()
was called, but not after
hasNext()
, because the hasNext()
call
changes the base iterator.remove
in interface java.util.Iterator
java.lang.IllegalStateException
- if hasNext()
has already
been called.public java.util.Iterator getIterator()
public void setIterator(java.util.Iterator iterator)
iterator
- the iterator to usepublic Predicate getPredicate()
public void setPredicate(Predicate predicate)
predicate
- the predicate to useprivate boolean setNextObject()