Class RandomIntegerIterator
java.lang.Object
com.globalmentor.collections.iterators.RandomIntegerIterator
An iterator that iterates a sequence of random iterators. The iterator has the ability to restrict its output to a particular range. A subset of the range
can optionally be returned. This iterator can keep track of the numbers used and only return unused integers.
- Author:
- Garret Wilson
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
The total number of integers returned.Create a new set to hold the integers excluded from the iterator.protected final int
The number of integers to return, or -1 if an unlimited number of integers should be returned.protected Integer
The value that has been retrieved and has passed the filter and is waiting to be returned, ornull
if there is no primed next object.protected final Random
The random number generator.protected final int
The absolute positive length of the range of possible values.protected final int
The upper bound, inclusive, of the range to include.protected final int
The lower bound, inclusive, of the range to include.protected final boolean
true
if values should be repeated, elsefalse
. -
Constructor Summary
ConstructorDescriptionRandomIntegerIterator
(int maxValue) Shuffle constructor.RandomIntegerIterator
(int maxValue, int maxCount) Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive).RandomIntegerIterator
(int maxValue, int maxCount, boolean repeat) Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive).RandomIntegerIterator
(int rangeMin, int rangeMax, int maxCount, boolean repeat) Full constructor with default random number generator.RandomIntegerIterator
(Random random, int maxValue) Shuffle constructor with random number generator.RandomIntegerIterator
(Random random, int maxValue, int maxCount) Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive).RandomIntegerIterator
(Random random, int maxValue, int maxCount, boolean repeat) Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive).RandomIntegerIterator
(Random random, int rangeMin, int rangeMax, int maxCount, boolean repeat) Full constructor with random number generator. -
Method Summary
Modifier and TypeMethodDescriptionprotected Integer
getNext()
boolean
hasNext()
next()
protected boolean
Retrieves the next object and stores it locally to return from the next call tonext()
.void
remove()
This implementation does not support element removal, and always throws an exception.void
setExcluded
(int integer, boolean exclude) Determines whether the given integer value should be excluded from the iteration.void
setExcluded
(Integer integer, boolean exclude) Determines whether the given integer value should be excluded from the iteration.void
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.Iterator
forEachRemaining
-
Field Details
-
random
The random number generator. -
rangeMin
protected final int rangeMinThe lower bound, inclusive, of the range to include. -
rangeMax
protected final int rangeMaxThe upper bound, inclusive, of the range to include. -
range
protected final int rangeThe absolute positive length of the range of possible values. -
maxCount
protected final int maxCountThe number of integers to return, or -1 if an unlimited number of integers should be returned. -
repeat
protected final boolean repeattrue
if values should be repeated, elsefalse
. -
excludedIntegerSortedSet
Create a new set to hold the integers excluded from the iterator. -
count
protected int countThe total number of integers returned. -
primedNext
The value that has been retrieved and has passed the filter and is waiting to be returned, ornull
if there is no primed next object.
-
-
Constructor Details
-
RandomIntegerIterator
public RandomIntegerIterator(int maxValue) Shuffle constructor. Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive). Values returned will be 0≤value<maxValue
, and will not be repeated. All numbers will be returned.- Parameters:
maxValue
- The upper bound, exclusive, of the values to return.
-
RandomIntegerIterator
Shuffle constructor with random number generator. Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive). Values returned will be 0≤value<maxValue
, and will not be repeated. All numbers will be returned.- Parameters:
random
- The random number generator.maxValue
- The upper bound, exclusive, of the values to return.
-
RandomIntegerIterator
public RandomIntegerIterator(int maxValue, int maxCount) Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive). Values returned will be 0≤value<maxValue
, and will not be repeated.- Parameters:
maxValue
- The upper bound, exclusive, of the values to return.maxCount
- The number of integers to return, or -1 if an unlimited number of integers should be returned.- Throws:
IllegalArgumentException
- Thrown if the given maximum count is greater than the allowed range.
-
RandomIntegerIterator
Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive). Values returned will be 0≤value<maxValue
, and will not be repeated.- Parameters:
random
- The random number generator.maxValue
- The upper bound, exclusive, of the values to return.maxCount
- The number of integers to return, or -1 if an unlimited number of integers should be returned.- Throws:
IllegalArgumentException
- Thrown if the given maximum count is greater than the allowed range.
-
RandomIntegerIterator
public RandomIntegerIterator(int maxValue, int maxCount, boolean repeat) Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive). Values returned will be 0≤value<maxValue
.- Parameters:
maxValue
- The upper bound, exclusive, of the values to return.maxCount
- The number of integers to return, or -1 if an unlimited number of integers should be returned.repeat
-true
if values should be repeated, elsefalse
.- Throws:
IllegalArgumentException
- Thrown if the given maximum count is greater than the allowed range.
-
RandomIntegerIterator
Constructs an iterator that returns numbers between zero (inclusive) and the given maximum value (exclusive). Values returned will be 0≤value<maxValue
.- Parameters:
random
- The random number generator.maxValue
- The upper bound, exclusive, of the values to return.maxCount
- The number of integers to return, or -1 if an unlimited number of integers should be returned.repeat
-true
if values should be repeated, elsefalse
.- Throws:
IllegalArgumentException
- Thrown if the given maximum count is greater than the allowed range.
-
RandomIntegerIterator
public RandomIntegerIterator(int rangeMin, int rangeMax, int maxCount, boolean repeat) Full constructor with default random number generator.- Parameters:
rangeMin
- The lower bound, inclusive, of the range to include.rangeMax
- The upper bound, inclusive, of the range to include.maxCount
- The number of integers to return, or -1 if an unlimited number of integers should be returned.repeat
-true
if values should be repeated, elsefalse
.- Throws:
IllegalArgumentException
- Thrown if the given maximum count is greater than the allowed range.
-
RandomIntegerIterator
public RandomIntegerIterator(Random random, int rangeMin, int rangeMax, int maxCount, boolean repeat) Full constructor with random number generator.- Parameters:
random
- The random number generator.rangeMin
- The lower bound, inclusive, of the range to include.rangeMax
- The upper bound, inclusive, of the range to include.maxCount
- The number of integers to return, or -1 if an unlimited number of integers should be returned.repeat
-true
if values should be repeated, elsefalse
.- Throws:
IllegalArgumentException
- Thrown if the given maximum count is greater than the allowed range.
-
-
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.
-
setExcluded
public void setExcluded(int integer, boolean exclude) Determines whether the given integer value should be excluded from the iteration.This method may safely be called in between calls to
next()
.- Parameters:
integer
- The integer to exclude or not.exclude
-true
if the value should be excluded from future calls tonext()
, elsefalse
if it should be possible (but not guaranteed) that the value will be returned from future calls tonext()
.
-
setExcluded
Determines whether the given integer value should be excluded from the iteration.This method may safely be called in between calls to
next()
.- Parameters:
integer
- The integer to exclude or not.exclude
-true
if the value should be excluded from future calls tonext()
, elsefalse
if it should be possible (but not guaranteed) that the value will be returned from future calls tonext()
.
-
hasNext
public boolean hasNext() -
next
- Specified by:
next
in interfaceIterator<Integer>
- Returns:
- The next random integer 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
null
if the iteration has no more elements.
-
remove
public void remove()This implementation does not support element removal, and always throws an exception.- Specified by:
remove
in interfaceIterator<Integer>
- Throws:
UnsupportedOperationException
- Thrown because theremove
operation is not supported by this iterator.
-