Class 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 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.
    • 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 - if max 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 counter nTimes is allowed.
        Returns:
        false if calling increment() will trigger a MathIllegalStateException, 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 calling increment(nTimes) would call the callback true otherwise.
        Throws:
        MathIllegalArgumentException - if nTimes is negative.
      • increment

        public void increment​(int nTimes)
        Performs multiple increments.
        Parameters:
        nTimes - Number of increments.
        Throws:
        MathIllegalArgumentException - if nTimes is negative.
        See Also:
        increment()
      • reset

        public void reset()
        Resets the counter to 0.