Class FilterIterator

java.lang.Object
org.apache.commons.collections.iterators.FilterIterator
All Implemented Interfaces:
Iterator
Direct Known Subclasses:
UniqueFilterIterator

public class FilterIterator extends Object implements Iterator
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:
Commons Collections 1.0
Version:
$Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
Author:
James Strachan, Jan Sorensen, Ralph Wagner, Stephen Colebourne
  • Constructor Details

    • FilterIterator

      public FilterIterator()
      Constructs a new FilterIterator that will not function until setIterator is invoked.
    • FilterIterator

      public FilterIterator(Iterator iterator)
      Constructs a new FilterIterator that will not function until setPredicate is invoked.
      Parameters:
      iterator - the iterator to use
    • FilterIterator

      public FilterIterator(Iterator iterator, Predicate predicate)
      Constructs a new FilterIterator that will use the given iterator and predicate.
      Parameters:
      iterator - the iterator to use
      predicate - 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 interface Iterator
      Returns:
      true if there is another object that matches the predicate
      Throws:
      NullPointerException - if either the iterator or predicate are null
    • next

      public Object next()
      Returns the next object that matches the predicate.
      Specified by:
      next in interface Iterator
      Returns:
      the next object which matches the given predicate
      Throws:
      NullPointerException - if either the iterator or predicate are null
      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 if next() was called, but not after hasNext(), because the hasNext() call changes the base iterator.
      Specified by:
      remove in interface Iterator
      Throws:
      IllegalStateException - if hasNext() has already been called.
    • getIterator

      public Iterator getIterator()
      Gets the iterator this iterator is using.
      Returns:
      the iterator
    • setIterator

      public void setIterator(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