Package org.hipparchus.util
Class Incrementor
- java.lang.Object
-
- org.hipparchus.util.Incrementor
-
public class Incrementor extends Object
Utility that increments a counter until a maximum is reached, at which point, the instance will by default throw aMathIllegalStateException. 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 interfaceIncrementor.MaxCountExceededCallbackDefines a method to be called at counter exhaustion.
-
Constructor Summary
Constructors Constructor Description Incrementor()Creates an Incrementor.Incrementor(int max)Creates an Incrementor.Incrementor(int max, Incrementor.MaxCountExceededCallback cb)Creates an Incrementor.
-
Method Summary
All 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.intgetCount()Gets the current count.intgetMaximalCount()Gets the upper limit of the counter.voidincrement()Adds the increment value to the current iteration count.voidincrement(int nTimes)Performs multiple increments.voidreset()Resets the counter to 0.IncrementorwithCallback(Incrementor.MaxCountExceededCallback cb)Creates a new instance with a given callback.IncrementorwithCount(int value)Creates a new instance and set the counter to the given value.IncrementorwithMaximalCount(int max)Creates a new instance with a given maximal count.
-
-
-
Constructor Detail
-
Incrementor
public Incrementor()
Creates an Incrementor.The maximal value will be set to
Integer.MAX_VALUE.
-
Incrementor
public Incrementor(int max)
Creates an Incrementor.- Parameters:
max- Maximal count.- Throws:
MathIllegalArgumentException- ifmaxis negative.
-
Incrementor
public Incrementor(int max, Incrementor.MaxCountExceededCallback cb) throws NullArgumentExceptionCreates an Incrementor.- Parameters:
max- Maximal count.cb- Function to be called when the maximal count has been reached.- Throws:
NullArgumentException- ifcbisnull.MathIllegalArgumentException- ifmaxis negative.
-
-
Method Detail
-
withCount
public Incrementor withCount(int value)
Creates a new instance and set the counter to the given value.- Parameters:
value- Value of the counter.- Returns:
- a new instance.
-
withMaximalCount
public Incrementor withMaximalCount(int max)
Creates a new instance with a given maximal count. The counter is reset to 0.- Parameters:
max- Maximal count.- Returns:
- a new instance.
- Throws:
MathIllegalArgumentException- ifmaxis negative.
-
withCallback
public Incrementor withCallback(Incrementor.MaxCountExceededCallback cb)
Creates a new instance with a given callback. The counter is reset to 0.- 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 aMathIllegalStateException,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.- Throws:
MathIllegalArgumentException- ifnTimesis negative.
-
increment
public void increment(int nTimes)
Performs multiple increments.- Parameters:
nTimes- Number of increments.- Throws:
MathIllegalArgumentException- ifnTimesis negative.- See Also:
increment()
-
increment
public void increment()
Adds 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.- See Also:
increment(int)
-
reset
public void reset()
Resets the counter to 0.
-
-