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:
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, orNO_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 tonext()
.void
setFilter(Filter<E> filter)
Sets the filter used to exclude items from the iterator.-
Methods inherited from class com.globalmentor.collections.iterators.IteratorDecorator
getIterator
-
Methods inherited from class com.globalmentor.collections.iterators.AbstractIteratorDecorator
hasMoreElements, nextElement, remove
-
-
-
-
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, orNO_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
-
setFilter
public void setFilter(Filter<E> filter)
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()
- Specified by:
hasNext
in interfacejava.util.Iterator<E>
- Overrides:
hasNext
in classAbstractIteratorDecorator<E>
- Returns:
true
if the iteration has more elements.
-
next
public E next()
- Specified by:
next
in interfacejava.util.Iterator<E>
- Overrides:
next
in classAbstractIteratorDecorator<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 tonext()
. 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.
-
-