Class Loop
java.lang.Object
ste.lloop.Loop
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 Summary
Modifier and TypeMethodDescriptionstatic voidSame as brk() - which version do you prefer? please let me know, we will remove the less voted one.static voidThrows aReturnValueexception with the given value.static NumericSequenceon()Creates a new numeric loop.static LinesSequenceon(BufferedReader reader) Creates a new loop over the lines of a given BufferedReader.static LinesSequenceCreates a new loop over the lines of a given file.static CharacterSequenceon(CharSequence sequence) Creates a new loop over the given CharSequence.static <T> IterableSequence<T> Creates a new loop over the given iterable.static LinesSequenceCreates a new loop over the lines of a given path.static <T> IteratorSequence<T> on(Enumeration<T> enumeration) Creates a new loop over the given Enumeration, converting it to anIteratorusingEnumeration.asIterator()internally.static <T> IteratorSequence<T> Creates a new loop over the given Iterator.static <T> ListSequence<T> Creates a new loop over the given list.static <K,V> MapSequence <K, V> Creates a new loop over the given Map.static <T> ArraySequence<T> on(T... items) Creates a new loop over the given items.
-
Method Details
-
on
Creates a new numeric loop.- Returns:
- a new
NumericSequenceinstance
-
on
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
ArraySequenceinstance
-
on
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
ListSequenceinstance
-
on
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
ForwardOnlySequenceinstance
-
on
Creates a new loop over the given CharSequence.- Parameters:
sequence- the CharSequence to loop over- Returns:
- a new
CharacterSequence
-
on
Creates a new loop over the given Map.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
map- the map to loop over- Returns:
- a new
MapSequenceinstance
-
on
Creates a new loop over the given Enumeration, converting it to anIteratorusingEnumeration.asIterator()internally.- Type Parameters:
T- the type of the elements in the enumeration- Parameters:
enumeration- the Enumeration to loop over- Returns:
- a new
IteratorSequenceinstance
-
on
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
Creates a new loop over the lines of a given file.- Parameters:
file- the file to loop over- Returns:
- a new
LinesSequenceinstance
-
on
Creates a new loop over the lines of a given path.- Parameters:
path- the path to loop over- Returns:
- a new
LinesSequenceinstance
-
on
Creates a new loop over the lines of a given BufferedReader.- Parameters:
reader- the BufferedReader to loop over- Returns:
- a new
LinesSequenceinstance
-
brk
Throws aReturnValueexception 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_
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
-