public static class IntegerSequence.Incrementor
extends java.lang.Object
implements java.util.Iterator<java.lang.Integer>
MaxCountExceededException.
However, the user is able to override this behaviour by defining a
custom callback, in order to e.g.
select which exception must be thrown.| Modifier and Type | Class and Description |
|---|---|
static interface |
IntegerSequence.Incrementor.MaxCountExceededCallback
Defines a method to be called at counter exhaustion.
|
| Modifier and Type | Field and Description |
|---|---|
private static IntegerSequence.Incrementor.MaxCountExceededCallback |
CALLBACK
Default callback.
|
private int |
count
Current count.
|
private int |
increment
Increment.
|
private int |
init
Initial value the counter.
|
private IntegerSequence.Incrementor.MaxCountExceededCallback |
maxCountCallback
Function called at counter exhaustion.
|
private int |
maximalCount
Upper limit for the counter.
|
| Modifier | Constructor and Description |
|---|---|
private |
IntegerSequence.Incrementor(int start,
int max,
int step,
IntegerSequence.Incrementor.MaxCountExceededCallback cb)
Creates an incrementor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
canIncrement()
Checks whether incrementing the counter
nTimes is allowed. |
boolean |
canIncrement(int nTimes)
Checks whether incrementing the counter several times is allowed.
|
static IntegerSequence.Incrementor |
create()
Factory method that creates a default instance.
|
int |
getCount()
Gets the current count.
|
int |
getMaximalCount()
Gets the upper limit of the counter.
|
boolean |
hasNext() |
void |
increment()
Adds the increment value to the current iteration count.
|
void |
increment(int nTimes)
Performs multiple increments.
|
java.lang.Integer |
next() |
void |
remove()
Not applicable.
|
IntegerSequence.Incrementor |
withCallback(IntegerSequence.Incrementor.MaxCountExceededCallback cb)
Creates a new instance with a given callback.
|
IntegerSequence.Incrementor |
withIncrement(int step)
Creates a new instance with a given increment.
|
IntegerSequence.Incrementor |
withMaximalCount(int max)
Creates a new instance with a given maximal count.
|
IntegerSequence.Incrementor |
withStart(int start)
Creates a new instance with a given initial value.
|
private static final IntegerSequence.Incrementor.MaxCountExceededCallback CALLBACK
private final int init
private final int maximalCount
private final int increment
private final IntegerSequence.Incrementor.MaxCountExceededCallback maxCountCallback
private int count
private IntegerSequence.Incrementor(int start,
int max,
int step,
IntegerSequence.Incrementor.MaxCountExceededCallback cb)
throws NullArgumentException
max is reached
or when nTimes increments have been performed.start - Initial value.max - Maximal count.step - Increment.cb - Function to be called when the maximal count has been reached.NullArgumentException - if cb is null.public static IntegerSequence.Incrementor create()
withMaximalCount.public IntegerSequence.Incrementor withStart(int start)
start - Initial value of the counter.public IntegerSequence.Incrementor withMaximalCount(int max)
max - Maximal count.public IntegerSequence.Incrementor withIncrement(int step)
step - Increment.public IntegerSequence.Incrementor withCallback(IntegerSequence.Incrementor.MaxCountExceededCallback cb)
cb - Callback to be called at counter exhaustion.public int getMaximalCount()
public int getCount()
public boolean canIncrement()
nTimes is allowed.false if calling increment()
will trigger a MaxCountExceededException,
true otherwise.public boolean canIncrement(int nTimes)
nTimes - Number of increments.false if calling increment(nTimes) would call the callback
true otherwise.public void increment(int nTimes)
throws MaxCountExceededException
nTimes - Number of increments.MaxCountExceededException - at counter exhaustion.NotStrictlyPositiveException - if nTimes <= 0.increment()public void increment()
throws MaxCountExceededException
trigger method of the
callback object passed to the
withCallback(MaxCountExceededCallback) method.
If not explicitly set, a default callback is used that will throw
a MaxCountExceededException.MaxCountExceededException - at counter exhaustion, unless a
custom callback has been set.increment(int)public boolean hasNext()
hasNext in interface java.util.Iterator<java.lang.Integer>public java.lang.Integer next()
next in interface java.util.Iterator<java.lang.Integer>public void remove()
remove in interface java.util.Iterator<java.lang.Integer>MathUnsupportedOperationExceptionCopyright (c) 2003-2017 Apache Software Foundation