Package org.hipparchus.util
Class Incrementor
java.lang.Object
org.hipparchus.util.Incrementor
Utility that increments a counter until a maximum is reached, at
which point, the instance will by default throw a
MathIllegalStateException.
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceDefines a method to be called at counter exhaustion. -
Constructor Summary
ConstructorsConstructorDescriptionCreates an Incrementor.Incrementor(int max) Creates an Incrementor.Incrementor(int max, Incrementor.MaxCountExceededCallback cb) Creates an Incrementor. -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks whether incrementing the counternTimesis allowed.booleancanIncrement(int nTimes) Checks whether incrementing the counter several times is allowed.intgetCount()Gets the current count.intGets the upper limit of the counter.voidAdds the increment value to the current iteration count.voidincrement(int nTimes) Performs multiple increments.voidreset()Resets the counter to 0.Creates a new instance with a given callback.withCount(int value) Creates a new instance and set the counter to the given value.withMaximalCount(int max) Creates a new instance with a given maximal count.
-
Constructor Details
-
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
Creates 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 Details
-
withCount
Creates a new instance and set the counter to the given value.- Parameters:
value- Value of the counter.- Returns:
- a new instance.
-
withMaximalCount
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
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
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:
-
reset
public void reset()Resets the counter to 0.
-