Package ste.lloop

Class Sequence<T>

java.lang.Object
ste.lloop.Sequence<T>
Type Parameters:
T - the type of the elements in the array

public class Sequence<T> extends Object
A loop over an array that can be configured with a starting and ending index.

This class is not meant to be instantiated directly. Use Loop.on(Object[]) to start building an array loop.

The loop can be configured with a from index, a to index, and a step value. If from and to are equal, the loop will not execute. The loop can be executed by calling one of the two loop methods: one that provides both the index and the element, and one that provides only the element.

  • Method Details

    • step

      public Sequence<T> step(int step)
      Sets the step of the loop. The sign of the step determines the direction of the loop.

      If the step is positive, the loop will go from from to to. If the step is negative, the loop will go from to to from.

      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 Sequence instance
    • from

      public Sequence<T> from(int from)
      Sets the starting index of the loop (inclusive).
      Parameters:
      from - the starting index
      Returns:
      this Sequence instance
      Throws:
      IndexOutOfBoundsException - if the 'from' value is less than zero
    • to

      public Sequence<T> to(int to)
      Sets the ending index of the loop (inclusive).
      Parameters:
      to - the ending index
      Returns:
      this Sequence instance
    • loop

      public void loop(BiConsumer<Integer,T> consumer)
      Executes the given consumer for each element in the loop.

      If the array provided to the constructor was null, this method will do nothing.

      Parameters:
      consumer - the consumer to execute for each element
    • loop

      public void loop(Consumer<T> consumer)
      Executes the given consumer for each element in the loop.

      If the array provided to the constructor was null, this method will do nothing.

      Parameters:
      consumer - the consumer to execute for each element