Package ste.lloop

Class NumericSequence

java.lang.Object
ste.lloop.NumericSequence

public class NumericSequence extends Object
A numeric loop that can be configured with a starting and ending value.

This class is not meant to be instantiated directly. Use Loop.on() to start building a numeric loop.

  • Field Details

    • from

      protected int from
      The starting value of the loop (inclusive).
    • to

      protected Integer to
      The ending value of the loop (inclusive).
    • step

      protected int step
      The step of the loop.
  • Constructor Details

    • NumericSequence

      public NumericSequence()
      Constructs a new NumericSequence instance.
  • Method Details

    • from

      public NumericSequence from(int from)
      Sets the starting value of the loop (inclusive).
      Parameters:
      from - the starting value
      Returns:
      this NumericSequence instance
    • to

      public NumericSequence to(int to)
      Sets the ending value of the loop (inclusive).
      Parameters:
      to - the ending value
      Returns:
      this NumericSequence instance
      Throws:
      IllegalArgumentException - if a negative step is set
    • step

      public NumericSequence step(int step)
      Sets the step of the loop.

      A negative step is allowed only if to is not set, in which case the loop will go backwards.

      The absolute value of the step is used as the increment. If the step is zero, the loop will not execute.

      Parameters:
      step - the step value
      Returns:
      this NumericSequence instance
      Throws:
      IllegalArgumentException - if a negative step is set when to is also set
    • loop

      public <R> R loop(Consumer<Integer> consumer)
      Executes the given consumer for each value in the loop.

      If to is set (finite loop) and from and to are equal, the loop will not execute.

      If to is not set (infinite loop), the loop will continue indefinitely until Loop.brk(Object) is called.

      Type Parameters:
      R - the type of the return value
      Parameters:
      consumer - the consumer to execute for each value
      Returns:
      the value passed to Loop.brk(Object), or null if the loop completes without a brk