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
Modifier and TypeClassDescriptionstatic interface
Defines a method to be called at counter exhaustion. -
Constructor Summary
ConstructorDescriptionCreates an Incrementor.Incrementor
(int max) Creates an Incrementor.Incrementor
(int max, Incrementor.MaxCountExceededCallback cb) Creates an Incrementor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks whether incrementing the counternTimes
is allowed.boolean
canIncrement
(int nTimes) Checks whether incrementing the counter several times is allowed.int
getCount()
Gets the current count.int
Gets the upper limit of the counter.void
Adds the increment value to the current iteration count.void
increment
(int nTimes) Performs multiple increments.void
reset()
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
- ifmax
is negative.
-
Incrementor
Creates an Incrementor.- Parameters:
max
- Maximal count.cb
- Function to be called when the maximal count has been reached.- Throws:
NullArgumentException
- ifcb
isnull
.MathIllegalArgumentException
- ifmax
is 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
- ifmax
is 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 counternTimes
is allowed.- Returns:
false
if callingincrement()
will trigger aMathIllegalStateException
,true
otherwise.
-
canIncrement
public boolean canIncrement(int nTimes) Checks whether incrementing the counter several times is allowed.- Parameters:
nTimes
- Number of increments.- Returns:
false
if callingincrement(nTimes)
would call thecallback
true
otherwise.- Throws:
MathIllegalArgumentException
- ifnTimes
is negative.
-
increment
public void increment(int nTimes) Performs multiple increments.- Parameters:
nTimes
- Number of increments.- Throws:
MathIllegalArgumentException
- ifnTimes
is negative.- See Also:
-
increment
public void increment()Adds the increment value to the current iteration count. At counter exhaustion, this method will call thetrigger
method of the callback object passed to thewithCallback(MaxCountExceededCallback)
method.- See Also:
-
reset
public void reset()Resets the counter to 0.
-