Class IntegerSequence.Incrementor

  • All Implemented Interfaces:
    java.util.Iterator<java.lang.Integer>
    Enclosing class:
    IntegerSequence

    public static class IntegerSequence.Incrementor
    extends java.lang.Object
    implements java.util.Iterator<java.lang.Integer>
    Utility that increments a counter until a maximum is reached, at which point, the instance will by default throw a MaxCountExceededException. 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.
    • Constructor Detail

      • Incrementor

        private Incrementor​(int start,
                            int max,
                            int step,
                            IntegerSequence.Incrementor.MaxCountExceededCallback cb)
                     throws NullArgumentException
        Creates an incrementor. The counter will be exhausted either when max is reached or when nTimes increments have been performed.
        Parameters:
        start - Initial value.
        max - Maximal count.
        step - Increment.
        cb - Function to be called when the maximal count has been reached.
        Throws:
        NullArgumentException - if cb is null.
    • Method Detail

      • create

        public static IntegerSequence.Incrementor create()
        Factory method that creates a default instance. The initial and maximal values are set to 0. For the new instance to be useful, the maximal count must be set by calling withMaximalCount.
        Returns:
        an new instance.
      • withStart

        public IntegerSequence.Incrementor withStart​(int start)
        Creates a new instance with a given initial value. The counter is reset to the initial value.
        Parameters:
        start - Initial value of the counter.
        Returns:
        a new instance.
      • withMaximalCount

        public IntegerSequence.Incrementor withMaximalCount​(int max)
        Creates a new instance with a given maximal count. The counter is reset to the initial value.
        Parameters:
        max - Maximal count.
        Returns:
        a new instance.
      • withIncrement

        public IntegerSequence.Incrementor withIncrement​(int step)
        Creates a new instance with a given increment. The counter is reset to the initial value.
        Parameters:
        step - Increment.
        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 MaxCountExceededException, 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.
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<java.lang.Integer>
      • next

        public java.lang.Integer next()
        Specified by:
        next in interface java.util.Iterator<java.lang.Integer>