Class CountedFilterIterator<E>

All Implemented Interfaces:
Enumeration<E>, 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 Details

    • 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 Object NO_NEXT_OBJECT
      The constant object representing no next object available.
    • primedNext

      protected 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 Details

    • CountedFilterIterator

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

      public CountedFilterIterator(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 Details

    • 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 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 Iterator<E>
      Overrides:
      next in class AbstractIteratorDecorator<E>
      Returns:
      The next element in the iteration.
      Throws:
      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 Object getNext()
      Returns:
      The next random integer in the iteration, or NO_NEXT_OBJECT if the iteration has no more elements.