Class CountedFilterIterator<E>
java.lang.Object
com.globalmentor.collections.iterators.AbstractIteratorDecorator<E>
com.globalmentor.collections.iterators.IteratorDecorator<E>
com.globalmentor.collections.iterators.CountedFilterIterator<E>
- All Implemented Interfaces:
Enumeration<E>
,Iterator<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
Modifier and TypeFieldDescriptionprotected int
The total number of integers returned.protected final int
The number of elements to return, or -1 if all of the elements in the proxied iterator should be returned.protected static final Object
The constant object representing no next object available.protected Object
The value that has been retrieved and has passed the filter and is waiting to be returned, orNO_NEXT_OBJECT
if there is no primed next object. -
Constructor Summary
ConstructorDescriptionCountedFilterIterator
(Iterator<E> iterator) Iterator constructor.CountedFilterIterator
(Iterator<E> iterator, int maxCount) Iterator and count constructor. -
Method Summary
Methods inherited from class com.globalmentor.collections.iterators.IteratorDecorator
getIterator
Methods inherited from class com.globalmentor.collections.iterators.AbstractIteratorDecorator
hasMoreElements, nextElement, remove
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 Details
-
maxCount
protected final int maxCountThe number of elements to return, or -1 if all of the elements in the proxied iterator should be returned. -
count
protected int countThe total number of integers returned. -
NO_NEXT_OBJECT
The constant object representing no next object available. -
primedNext
The value that has been retrieved and has passed the filter and is waiting to be returned, orNO_NEXT_OBJECT
if there is no primed next object.
-
-
Constructor Details
-
CountedFilterIterator
Iterator constructor.- Parameters:
iterator
- The iterator this iterator should proxy.
-
CountedFilterIterator
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
- Returns:
- The filter used to exclude items from the iterator.
-
setFilter
Sets the filter used to exclude items from the iterator.- Parameters:
filter
- The new filter to use, ornull
if there should be no filtering.
-
hasNext
public boolean hasNext() -
next
- Specified by:
next
in interfaceIterator<E>
- Overrides:
next
in classAbstractIteratorDecorator<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 tonext()
. If an object is already primed, no action occurs.- Returns:
true
if there is another integer left to retrieve.
-
getNext
- Returns:
- The next random integer in the iteration, or
NO_NEXT_OBJECT
if the iteration has no more elements.
-