Class FunctionalIterate
Object
FunctionalIterate
Utilities for repeating operations a certain number of times.
- Author:
- Owen Feehan
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,
V, E extends Exception>
voiditerateMap
(Map<K, V> map, CheckedBiConsumer<K, V, E> consumer) Iterate through all entries in aMap
.static <E extends Exception>
voidrepeat
(int numberTimes, CheckedRunnable<E> operation) Repeats an operation a number of times.repeatAsStream
(int numberTimes, Class<? extends E> throwableClass, CheckedSupplier<T, E> supplier) Repeats an operation a number of times, to form aStream
.static <E extends Exception>
intrepeatCountSuccessful
(int numberTimes, CheckedBooleanSupplier<E> operation) Repeats an operation a number of times, counting the number of times the operation returns true.static <E extends Exception>
booleanrepeatUntil
(int maxNumberTimes, CheckedBooleanSupplier<E> supplier) Repeats an operation until a true value is returned.static <E extends Exception>
voidrepeatWithIndex
(int numberTimes, CheckedIntConsumer<E> operation) Repeats an operation a number of times, passing an increment index tooperation
each time.static <T,
E extends Exception>
voidreverseIterateArray
(T[] array, CheckedConsumer<T, E> consumer) Iterates through every element in an array in reverse order
-
Method Details
-
repeat
public static <E extends Exception> void repeat(int numberTimes, CheckedRunnable<E> operation) throws E Repeats an operation a number of times.- Type Parameters:
E
- an exception that may be thrown byoperation
.- Parameters:
numberTimes
- how many times to repeat the operationoperation
- the operation- Throws:
E
- ifoperation
throws it.
-
repeatCountSuccessful
public static <E extends Exception> int repeatCountSuccessful(int numberTimes, CheckedBooleanSupplier<E> operation) throws E Repeats an operation a number of times, counting the number of times the operation returns true.- Type Parameters:
E
- an exception that may be thrown byoperation
.- Parameters:
numberTimes
- how many times to repeat the operationoperation
- the operation, which returns true for success, or false for failure.- Returns:
- the number of successful operations (maximally
numberTimes
). - Throws:
E
- ifoperation
throws it.
-
repeatWithIndex
public static <E extends Exception> void repeatWithIndex(int numberTimes, CheckedIntConsumer<E> operation) throws E Repeats an operation a number of times, passing an increment index tooperation
each time.The index begins at 0 and will increment to
numberTimes -1
(inclusive).- Type Parameters:
E
- an exception that may be thrown byoperation
.- Parameters:
numberTimes
- how many times to repeat the operationoperation
- the operation to execute given an index- Throws:
E
- ifoperation
throws it.
-
repeatUntil
public static <E extends Exception> boolean repeatUntil(int maxNumberTimes, CheckedBooleanSupplier<E> supplier) throws E Repeats an operation until a true value is returned.- Type Parameters:
E
- an exception that may be thrown by the supplier- Parameters:
maxNumberTimes
- the maximum number of times to repeat if no true value is returned.supplier
- the operation to repeat that returns true or false- Returns:
- true if at least one operation returned true, or false if no operation did.
- Throws:
E
- ifsupplier
throws an exception.
-
repeatAsStream
public static <T,E extends Exception> Stream<T> repeatAsStream(int numberTimes, Class<? extends E> throwableClass, CheckedSupplier<T, E> supplier) throws ERepeats an operation a number of times, to form aStream
.- Type Parameters:
T
- type of element to created in theStream
.E
- an exception that may be thrown byoperation
.- Parameters:
numberTimes
- how many times to repeat the operationthrowableClass
- the class ofE
.supplier
- the operation- Returns:
- a newly created
Stream
ofnumberTimes
* the objects created bysupplier
. - Throws:
E
- ifoperation
throws it.
-
iterateMap
public static <K,V, void iterateMapE extends Exception> (Map<K, V> map, CheckedBiConsumer<K, throws EV, E> consumer) Iterate through all entries in aMap
.- Type Parameters:
K
- map key type, see corresponding parameter in @{link Map}.V
- map value type, see corresponding parameter in @{link Map}.E
- an exception that may be thrown byconsumer
.- Parameters:
map
- the map whose entries will be iterated.consumer
- called for each entry.- Throws:
E
- if throw byconsumer
.
-
reverseIterateArray
public static <T,E extends Exception> void reverseIterateArray(T[] array, CheckedConsumer<T, E> consumer) throws EIterates through every element in an array in reverse order- Type Parameters:
T
- array element typeE
- an exception that may be thrown byconsumer
.- Parameters:
array
- the arrayconsumer
- called for each element in the array.- Throws:
E
- if throw byconsumer
.
-