Class FilterIterator<E>
java.lang.Object
org.apache.openjpa.lib.util.collections.FilterIterator<E>
- All Implemented Interfaces:
Iterator<E>
Decorates another
Iterator
using a predicate to filter elements.
This iterator decorates the underlying iterator, only allowing through
those elements that match the specified Predicate
.
- Since:
- 1.0
-
Constructor Summary
ConstructorDescriptionConstructs a newFilterIterator
that will not function untilsetIterator
is invoked.FilterIterator
(Iterator<? extends E> iterator) Constructs a newFilterIterator
that will not function untilsetPredicate
is invoked.FilterIterator
(Iterator<? extends E> iterator, Predicate<? super E> predicate) Constructs a newFilterIterator
that will use the given iterator and predicate. -
Method Summary
Modifier and TypeMethodDescriptionGets the iterator this iterator is using.Gets the predicate this iterator is using.boolean
hasNext()
Returns true if the underlying iterator contains an object that matches the predicate.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
(Iterator<? extends E> iterator) Sets the iterator for this iterator to use.void
setPredicate
(Predicate<? super E> predicate) Sets the predicate this the iterator to use.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
FilterIterator
public FilterIterator()Constructs a newFilterIterator
that will not function untilsetIterator
is invoked. -
FilterIterator
Constructs a newFilterIterator
that will not function untilsetPredicate
is invoked.- Parameters:
iterator
- the iterator to use
-
FilterIterator
Constructs a newFilterIterator
that will use the given iterator and predicate.- Parameters:
iterator
- the iterator to usepredicate
- the predicate to use
-
-
Method Details
-
hasNext
public boolean hasNext()Returns true if the underlying iterator contains an object that matches the predicate.- Specified by:
hasNext
in interfaceIterator<E>
- Returns:
- true if there is another object that matches the predicate
- Throws:
NullPointerException
- if either the iterator or predicate are null
-
next
Returns the next object that matches the predicate.- Specified by:
next
in interfaceIterator<E>
- Returns:
- the next object which matches the given predicate
- Throws:
NullPointerException
- if either the iterator or predicate are nullNoSuchElementException
- if there are no more elements that match the predicate
-
remove
public void remove()Removes from the underlying collection of the base iterator the last element returned by this iterator. This method can only be called ifnext()
was called, but not afterhasNext()
, because thehasNext()
call changes the base iterator.- Specified by:
remove
in interfaceIterator<E>
- Throws:
IllegalStateException
- ifhasNext()
has already been called.
-
getIterator
Gets the iterator this iterator is using.- Returns:
- the iterator
-
setIterator
Sets the iterator for this iterator to use. If iteration has started, this effectively resets the iterator.- Parameters:
iterator
- the iterator to use
-
getPredicate
Gets the predicate this iterator is using.- Returns:
- the predicate
-
setPredicate
Sets the predicate this the iterator to use.- Parameters:
predicate
- the predicate to use
-