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
 });

 // Loop over an iterator
 Loop.on(iterator).loop(element -> {
     // do something with element
 });

 // Loop over lines from a file
 Loop.on(new File("path/to/file.txt")).loop(line -> {
     // do something with each line
 });

 // Loop over lines from a Path
 Loop.on(Paths.get("path/to/file.txt")).loop((index, line) -> {
     // do something with index and line
 });

 // Loop over lines from a BufferedReader
 Loop.on(new BufferedReader(new FileReader("path/to/file.txt"))).loop(line -> {
     // do something with each line
 });
 
  • 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> IterableSequence<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
    • on

      public static CharacterSequence on(CharSequence sequence)
      Creates a new loop over the given CharSequence.
      Parameters:
      sequence - the CharSequence to loop over
      Returns:
      a new CharacterSequence
    • on

      public static <K,V> MapSequence<K,V> on(Map<K,V> map)
      Creates a new loop over the given Map.
      Type Parameters:
      K - the type of the keys in the map
      V - the type of the values in the map
      Parameters:
      map - the map to loop over
      Returns:
      a new MapSequence instance
    • on

      public static <T> IteratorSequence<T> on(Enumeration<T> enumeration)
      Creates a new loop over the given Enumeration, converting it to an Iterator using Enumeration.asIterator() internally.
      Type Parameters:
      T - the type of the elements in the enumeration
      Parameters:
      enumeration - the Enumeration to loop over
      Returns:
      a new IteratorSequence instance
    • on

      public static <T> IteratorSequence<T> on(Iterator<T> iterator)
      Creates a new loop over the given Iterator.
      Type Parameters:
      T - the type of the elements in the iterator
      Parameters:
      iterator - the Iterator to loop over
      Returns:
      a new IteratorSequence
    • on

      public static LinesSequence on(File file)
      Creates a new loop over the lines of a given file.
      Parameters:
      file - the file to loop over
      Returns:
      a new LinesSequence instance
    • on

      public static LinesSequence on(Path path)
      Creates a new loop over the lines of a given path.
      Parameters:
      path - the path to loop over
      Returns:
      a new LinesSequence instance
    • on

      public static LinesSequence on(BufferedReader reader)
      Creates a new loop over the lines of a given BufferedReader.
      Parameters:
      reader - the BufferedReader to loop over
      Returns:
      a new LinesSequence 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