Package ste.lloop

Class Loop

java.lang.Object
ste.lloop.Loop

public final class Loop extends Object
Provides a fluent API for creating loops.

This class is the main entry point for creating loops. Use one of the static on methods to start building a loop.

Example usage:


 // Numeric loop from 0 to 10 (inclusive)
 Loop.on().from(0).to(10).loop(i -> {
     // do something with i
 });

 // Loop over an array of strings
 Loop.on("a", "b", "c").loop(element -> {
    // do something with element
 });

 // Loop over an array of strings, from index 1 up to (and including) index 3
 Loop.on(new String[]{"a", "b", "c", "d"}).from(1).to(3).loop((index, element) -> {
    // do something with index and element
 });

 // Numeric loop from 0 to 10, with a step of 2
 Loop.on().from(0).to(10).step(2).loop(i -> {
     // i will be 0, 2, 4, 6, 8, 10
 });
 
  • Method Details

    • on

      public static NumericSequence on()
      Creates a new numeric loop.
      Returns:
      a new NumericSequence instance
    • on

      @SafeVarargs public static <T> ArraySequence<T> on(T... items)
      Creates a new loop over the given items. This method supports both varargs and passing an array directly.
      Type Parameters:
      T - the type of the items
      Parameters:
      items - the items to loop over (can be varargs or an array)
      Returns:
      a new ArraySequence instance
    • on

      public static <T> ListSequence<T> on(List<T> list)
      Creates a new loop over the given list.
      Type Parameters:
      T - the type of the items in the list
      Parameters:
      list - the list to loop over
      Returns:
      a new ListSequence instance
    • on

      public static <T> ForwardOnlySequence<T> on(Iterable<T> iterable)
      Creates a new loop over the given iterable.
      Type Parameters:
      T - the type of the items in the iterable
      Parameters:
      iterable - the iterable to loop over
      Returns:
      a new ForwardOnlySequence instance
    • brk

      public static void brk(Object value)
      Throws a ReturnValue exception with the given value. This method is intended to be used inside a loop to break out of it and return a value.
      Parameters:
      value - the value to return
      Throws:
      ReturnValue - with the given value
    • _break_

      public static void _break_(Object value)
      Same as brk() - which version do you prefer? please let me know, we will remove the less voted one.
      Parameters:
      value - the value to return
      Throws:
      ReturnValue - with the given value