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 interface
Incrementor.MaxCountExceededCallback
Defines 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 boolean
canIncrement()
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
getMaximalCount()
Gets the upper limit of the counter.void
increment()
Adds the increment value to the current iteration count.void
increment(int nTimes)
Performs multiple increments.void
reset()
Resets the counter to 0.Incrementor
withCallback(Incrementor.MaxCountExceededCallback cb)
Creates a new instance with a given callback.Incrementor
withCount(int value)
Creates a new instance and set the counter to the given value.Incrementor
withMaximalCount(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
- ifmax
is negative.
-
Incrementor
public Incrementor(int max, Incrementor.MaxCountExceededCallback cb) throws NullArgumentException
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 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
- ifmax
is 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 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()
-
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:
increment(int)
-
reset
public void reset()
Resets the counter to 0.
-
-