Class FilterIterator<E>

  • All Implemented Interfaces:
    java.util.Iterator<E>
    Direct Known Subclasses:
    UniqueFilterIterator

    public class FilterIterator<E>
    extends java.lang.Object
    implements java.util.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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Iterator<? extends E> iterator
      The iterator being used
      private E nextObject
      The next object in the iteration
      private boolean nextObjectSet
      Whether the next object has been calculated yet
      private Predicate<? super E> predicate
      The predicate being used
    • Constructor Summary

      Constructors 
      Constructor Description
      FilterIterator()
      Constructs a new FilterIterator that will not function until setIterator is invoked.
      FilterIterator​(java.util.Iterator<? extends E> iterator)
      Constructs a new FilterIterator that will not function until setPredicate is invoked.
      FilterIterator​(java.util.Iterator<? extends E> iterator, Predicate<? super E> predicate)
      Constructs a new FilterIterator that will use the given iterator and predicate.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Iterator<? extends E> getIterator()
      Gets the iterator this iterator is using.
      Predicate<? super E> getPredicate()
      Gets the predicate this iterator is using.
      boolean hasNext()
      Returns true if the underlying iterator contains an object that matches the predicate.
      E 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<? extends E> iterator)
      Sets the iterator for this iterator to use.
      private boolean setNextObject()
      Set nextObject to the next object.
      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
    • Field Detail

      • iterator

        private java.util.Iterator<? extends E> iterator
        The iterator being used
      • predicate

        private Predicate<? super E> predicate
        The predicate being used
      • nextObject

        private E 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 new FilterIterator that will not function until setIterator is invoked.
      • FilterIterator

        public FilterIterator​(java.util.Iterator<? extends E> iterator)
        Constructs a new FilterIterator that will not function until setPredicate is invoked.
        Parameters:
        iterator - the iterator to use
      • FilterIterator

        public FilterIterator​(java.util.Iterator<? extends E> iterator,
                              Predicate<? super E> 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 Detail

      • hasNext

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

        public java.util.Iterator<? extends E> getIterator()
        Gets the iterator this iterator is using.
        Returns:
        the iterator
      • setIterator

        public void setIterator​(java.util.Iterator<? extends E> 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<? super E> getPredicate()
        Gets the predicate this iterator is using.
        Returns:
        the predicate
      • setPredicate

        public void setPredicate​(Predicate<? super E> 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.