Package org.apache.commons.math3.util
Class IntegerSequence.Incrementor
- java.lang.Object
-
- org.apache.commons.math3.util.IntegerSequence.Incrementor
-
- All Implemented Interfaces:
java.util.Iterator<java.lang.Integer>
- Enclosing class:
- IntegerSequence
public static class IntegerSequence.Incrementor extends java.lang.Object implements java.util.Iterator<java.lang.Integer>Utility that increments a counter until a maximum is reached, at which point, the instance will by default throw aMaxCountExceededException. However, the user is able to override this behaviour by defining a customcallback, in order to e.g. select which exception must be thrown.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceIntegerSequence.Incrementor.MaxCountExceededCallbackDefines a method to be called at counter exhaustion.
-
Field Summary
Fields Modifier and Type Field Description private static IntegerSequence.Incrementor.MaxCountExceededCallbackCALLBACKDefault callback.private intcountCurrent count.private intincrementIncrement.private intinitInitial value the counter.private IntegerSequence.Incrementor.MaxCountExceededCallbackmaxCountCallbackFunction called at counter exhaustion.private intmaximalCountUpper limit for the counter.
-
Constructor Summary
Constructors Modifier Constructor Description privateIncrementor(int start, int max, int step, IntegerSequence.Incrementor.MaxCountExceededCallback cb)Creates an incrementor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanIncrement()Checks whether incrementing the counternTimesis allowed.booleancanIncrement(int nTimes)Checks whether incrementing the counter several times is allowed.static IntegerSequence.Incrementorcreate()Factory method that creates a default instance.intgetCount()Gets the current count.intgetMaximalCount()Gets the upper limit of the counter.booleanhasNext()voidincrement()Adds the increment value to the current iteration count.voidincrement(int nTimes)Performs multiple increments.java.lang.Integernext()voidremove()Not applicable.IntegerSequence.IncrementorwithCallback(IntegerSequence.Incrementor.MaxCountExceededCallback cb)Creates a new instance with a given callback.IntegerSequence.IncrementorwithIncrement(int step)Creates a new instance with a given increment.IntegerSequence.IncrementorwithMaximalCount(int max)Creates a new instance with a given maximal count.IntegerSequence.IncrementorwithStart(int start)Creates a new instance with a given initial value.
-
-
-
Field Detail
-
CALLBACK
private static final IntegerSequence.Incrementor.MaxCountExceededCallback CALLBACK
Default callback.
-
init
private final int init
Initial value the counter.
-
maximalCount
private final int maximalCount
Upper limit for the counter.
-
increment
private final int increment
Increment.
-
maxCountCallback
private final IntegerSequence.Incrementor.MaxCountExceededCallback maxCountCallback
Function called at counter exhaustion.
-
count
private int count
Current count.
-
-
Constructor Detail
-
Incrementor
private Incrementor(int start, int max, int step, IntegerSequence.Incrementor.MaxCountExceededCallback cb) throws NullArgumentExceptionCreates an incrementor. The counter will be exhausted either whenmaxis reached or whennTimesincrements have been performed.- Parameters:
start- Initial value.max- Maximal count.step- Increment.cb- Function to be called when the maximal count has been reached.- Throws:
NullArgumentException- ifcbisnull.
-
-
Method Detail
-
create
public static IntegerSequence.Incrementor create()
Factory method that creates a default instance. The initial and maximal values are set to 0. For the new instance to be useful, the maximal count must be set by callingwithMaximalCount.- Returns:
- an new instance.
-
withStart
public IntegerSequence.Incrementor withStart(int start)
Creates a new instance with a given initial value. The counter is reset to the initial value.- Parameters:
start- Initial value of the counter.- Returns:
- a new instance.
-
withMaximalCount
public IntegerSequence.Incrementor withMaximalCount(int max)
Creates a new instance with a given maximal count. The counter is reset to the initial value.- Parameters:
max- Maximal count.- Returns:
- a new instance.
-
withIncrement
public IntegerSequence.Incrementor withIncrement(int step)
Creates a new instance with a given increment. The counter is reset to the initial value.- Parameters:
step- Increment.- Returns:
- a new instance.
-
withCallback
public IntegerSequence.Incrementor withCallback(IntegerSequence.Incrementor.MaxCountExceededCallback cb)
Creates a new instance with a given callback. The counter is reset to the initial value.- Parameters:
cb- Callback to be called at counter exhaustion.- Returns:
- a new instance.
-
getMaximalCount
public int getMaximalCount()
Gets the upper limit of the counter.- Returns:
- the counter upper limit.
-
getCount
public int getCount()
Gets the current count.- Returns:
- the current count.
-
canIncrement
public boolean canIncrement()
Checks whether incrementing the counternTimesis allowed.- Returns:
falseif callingincrement()will trigger aMaxCountExceededException,trueotherwise.
-
canIncrement
public boolean canIncrement(int nTimes)
Checks whether incrementing the counter several times is allowed.- Parameters:
nTimes- Number of increments.- Returns:
falseif callingincrement(nTimes)would call thecallbacktrueotherwise.
-
increment
public void increment(int nTimes) throws MaxCountExceededExceptionPerforms multiple increments.- Parameters:
nTimes- Number of increments.- Throws:
MaxCountExceededException- at counter exhaustion.NotStrictlyPositiveException- ifnTimes <= 0.- See Also:
increment()
-
increment
public void increment() throws MaxCountExceededExceptionAdds the increment value to the current iteration count. At counter exhaustion, this method will call thetriggermethod of the callback object passed to thewithCallback(MaxCountExceededCallback)method. If not explicitly set, a default callback is used that will throw aMaxCountExceededException.- Throws:
MaxCountExceededException- at counter exhaustion, unless a customcallbackhas been set.- See Also:
increment(int)
-
hasNext
public boolean hasNext()
- Specified by:
hasNextin interfacejava.util.Iterator<java.lang.Integer>
-
next
public java.lang.Integer next()
- Specified by:
nextin interfacejava.util.Iterator<java.lang.Integer>
-
remove
public void remove()
Not applicable.- Specified by:
removein interfacejava.util.Iterator<java.lang.Integer>- Throws:
MathUnsupportedOperationException
-
-