Class CountedFilterIterator<E>

  • All Implemented Interfaces:
    java.util.Enumeration<E>, java.util.Iterator<E>

    public class CountedFilterIterator<E>
    extends IteratorDecorator<E>
    An iterator that filters the results of an existing iterator.

    The iterator allows a filter to be applied to the objects it returns, only returning those that pass through the given filter, if any.

    The iterator can also restrict the number of returned elements to a given maximum.

    Author:
    Garret Wilson
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int count
      The total number of integers returned.
      protected int maxCount
      The number of elements to return, or -1 if all of the elements in the proxied iterator should be returned.
      protected static java.lang.Object NO_NEXT_OBJECT
      The constant object representing no next object available.
      protected java.lang.Object primedNext
      The value that has been retrieved and has passed the filter and is waiting to be returned, or NO_NEXT_OBJECT if there is no primed next object.
    • Constructor Summary

      Constructors 
      Constructor Description
      CountedFilterIterator​(java.util.Iterator<E> iterator)
      Iterator constructor.
      CountedFilterIterator​(java.util.Iterator<E> iterator, int maxCount)
      Iterator and count constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Filter<E> getFilter()  
      protected java.lang.Object getNext()  
      boolean hasNext()  
      E next()  
      protected boolean primeNext()
      Retrieves the next object and stores it locally to return from the next call to next().
      void setFilter​(Filter<E> filter)
      Sets the filter used to exclude items from the iterator.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Enumeration

        asIterator
      • Methods inherited from interface java.util.Iterator

        forEachRemaining
    • Field Detail

      • maxCount

        protected final int maxCount
        The number of elements to return, or -1 if all of the elements in the proxied iterator should be returned.
      • count

        protected int count
        The total number of integers returned.
      • NO_NEXT_OBJECT

        protected static final java.lang.Object NO_NEXT_OBJECT
        The constant object representing no next object available.
      • primedNext

        protected java.lang.Object primedNext
        The value that has been retrieved and has passed the filter and is waiting to be returned, or NO_NEXT_OBJECT if there is no primed next object.
    • Constructor Detail

      • CountedFilterIterator

        public CountedFilterIterator​(java.util.Iterator<E> iterator)
        Iterator constructor.
        Parameters:
        iterator - The iterator this iterator should proxy.
      • CountedFilterIterator

        public CountedFilterIterator​(java.util.Iterator<E> iterator,
                                     int maxCount)
        Iterator and count constructor.
        Parameters:
        iterator - The iterator this iterator should proxy.
        maxCount - The number of elements to return, or -1 if all of the elements in the proxied iterator should be returned.
    • Method Detail

      • getFilter

        public Filter<E> getFilter()
        Returns:
        The filter used to exclude items from the iterator.
      • setFilter

        public void setFilter​(Filter<E> filter)
        Sets the filter used to exclude items from the iterator.
        Parameters:
        filter - The new filter to use, or null if there should be no filtering.
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<E>
        Overrides:
        hasNext in class AbstractIteratorDecorator<E>
        Returns:
        true if the iteration has more elements.
      • next

        public E next()
        Specified by:
        next in interface java.util.Iterator<E>
        Overrides:
        next in class AbstractIteratorDecorator<E>
        Returns:
        The next element in the iteration.
        Throws:
        java.util.NoSuchElementException - Thrown if the iteration has no more elements.
      • primeNext

        protected boolean primeNext()
        Retrieves the next object and stores it locally to return from the next call to next(). If an object is already primed, no action occurs.
        Returns:
        true if there is another integer left to retrieve.
      • getNext

        protected java.lang.Object getNext()
        Returns:
        The next random integer in the iteration, or NO_NEXT_OBJECT if the iteration has no more elements.