Class FilterIterator
- java.lang.Object
-
- org.apache.commons.collections.iterators.FilterIterator
-
- All Implemented Interfaces:
java.util.Iterator
- Direct Known Subclasses:
UniqueFilterIterator
public class FilterIterator extends java.lang.Object implements java.util.IteratorDecorates anotherIteratorusing a predicate to filter elements.This iterator decorates the underlying iterator, only allowing through those elements that match the specified
Predicate.- Since:
- Commons Collections 1.0
- Version:
- $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.IteratoriteratorThe iterator being usedprivate java.lang.ObjectnextObjectThe next object in the iterationprivate booleannextObjectSetWhether the next object has been calculated yetprivate PredicatepredicateThe predicate being used
-
Constructor Summary
Constructors Constructor Description FilterIterator()Constructs a newFilterIteratorthat will not function untilsetIteratoris invoked.FilterIterator(java.util.Iterator iterator)Constructs a newFilterIteratorthat will not function untilsetPredicateis invoked.FilterIterator(java.util.Iterator iterator, Predicate predicate)Constructs a newFilterIteratorthat will use the given iterator and predicate.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.IteratorgetIterator()Gets the iterator this iterator is using.PredicategetPredicate()Gets the predicate this iterator is using.booleanhasNext()Returns true if the underlying iterator contains an object that matches the predicate.java.lang.Objectnext()Returns the next object that matches the predicate.voidremove()Removes from the underlying collection of the base iterator the last element returned by this iterator.voidsetIterator(java.util.Iterator iterator)Sets the iterator for this iterator to use.private booleansetNextObject()Set nextObject to the next object.voidsetPredicate(Predicate predicate)Sets the predicate this the iterator to use.
-
-
-
Field Detail
-
iterator
private java.util.Iterator iterator
The iterator being used
-
predicate
private Predicate predicate
The predicate being used
-
nextObject
private java.lang.Object nextObject
The next object in the iteration
-
nextObjectSet
private boolean nextObjectSet
Whether the next object has been calculated yet
-
-
Constructor Detail
-
FilterIterator
public FilterIterator()
Constructs a newFilterIteratorthat will not function untilsetIteratoris invoked.
-
FilterIterator
public FilterIterator(java.util.Iterator iterator)
Constructs a newFilterIteratorthat will not function untilsetPredicateis invoked.- Parameters:
iterator- the iterator to use
-
FilterIterator
public FilterIterator(java.util.Iterator iterator, Predicate predicate)Constructs a newFilterIteratorthat will use the given iterator and predicate.- Parameters:
iterator- the iterator to usepredicate- the predicate to use
-
-
Method Detail
-
hasNext
public boolean hasNext()
Returns true if the underlying iterator contains an object that matches the predicate.- Specified by:
hasNextin interfacejava.util.Iterator- Returns:
- true if there is another object that matches the predicate
- Throws:
java.lang.NullPointerException- if either the iterator or predicate are null
-
next
public java.lang.Object next()
Returns the next object that matches the predicate.- Specified by:
nextin interfacejava.util.Iterator- Returns:
- the next object which matches the given predicate
- Throws:
java.lang.NullPointerException- if either the iterator or predicate are nulljava.util.NoSuchElementException- 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:
removein interfacejava.util.Iterator- Throws:
java.lang.IllegalStateException- ifhasNext()has already been called.
-
getIterator
public java.util.Iterator getIterator()
Gets the iterator this iterator is using.- Returns:
- the iterator
-
setIterator
public void setIterator(java.util.Iterator iterator)
Sets the iterator for this iterator to use. If iteration has started, this effectively resets the iterator.- Parameters:
iterator- the iterator to use
-
getPredicate
public Predicate getPredicate()
Gets the predicate this iterator is using.- Returns:
- the predicate
-
setPredicate
public void setPredicate(Predicate predicate)
Sets the predicate this the iterator to use.- Parameters:
predicate- the predicate to use
-
setNextObject
private boolean setNextObject()
Set nextObject to the next object. If there are no more objects then return false. Otherwise, return true.
-
-