Class LambdaUtils
java.lang.Object
no.mnemonic.commons.utilities.lambda.LambdaUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> void
forEachTry
(Collection<T> values, ExceptionalConsumer<T, ? extends Exception> consumer) Try to perform operation on multiple values, ignoring any exception thrown.static <T,
E extends Exception>
voidforEachTry
(Collection<T> values, ExceptionalConsumer<T, E> consumer, Consumer<Throwable> onException) Try to perform operation on multiple values, ignoring any exception thrown, instead pass any error to exception consumer.static <T> T
Invoke callable to fetch result and return it.static <T> T
Invoke callable to fetch result and return it.tryStream
(Collection<T> collection) Wrap stream into a TryStream.static boolean
tryTo
(ExceptionalTask callable) Call provided task, ignore any exception thrown.static boolean
tryTo
(ExceptionalTask callable, Consumer<Throwable> onException) Call provided task, ignore any exception thrown, instead passing it to a provided exception handler.static boolean
waitFor
(BooleanSupplier booleanSupplier, long waitTime, TimeUnit timeUnit) Simple helper to wait for a predicate to return true.
-
Method Details
-
waitFor
public static boolean waitFor(BooleanSupplier booleanSupplier, long waitTime, TimeUnit timeUnit) throws InterruptedException Simple helper to wait for a predicate to return true. Will test predicate every 100ms until true or timeout.- Parameters:
booleanSupplier
- the predicatewaitTime
- how long to wait before giving uptimeUnit
- time unit of waitTime- Returns:
- true if predicate returns true within timeout, false if timed out without getting true value
- Throws:
InterruptedException
- if interrupted during sleep.
-
tryTo
Call provided task, ignore any exception thrown. Convenience method to call a method/lambda without having to wrap with try/catch- Parameters:
callable
- task to call- Returns:
- true if task was successful, false if exception was caught
-
tryTo
Call provided task, ignore any exception thrown, instead passing it to a provided exception handler. Convenience method to call a method/lambda without having to wrap with try/catchLambdaUtils.tryTo(()->doSomethingThatMightThrowAnException(), error->LOGGER.warn("Error doing something", error));
- Parameters:
callable
- task to callonException
- consumer to provide any exception caught- Returns:
- true if task was successful, false if exception was caught
-
forEachTry
public static <T> void forEachTry(Collection<T> values, ExceptionalConsumer<T, ? extends Exception> consumer) Try to perform operation on multiple values, ignoring any exception thrown. Convenience method to call forEach on a collection of values without having to use try/catch in lambda- Type Parameters:
T
- value type- Parameters:
values
- valuesconsumer
- consumer to handle value, which may throw exception
-
forEachTry
public static <T,E extends Exception> void forEachTry(Collection<T> values, ExceptionalConsumer<T, E> consumer, Consumer<Throwable> onException) Try to perform operation on multiple values, ignoring any exception thrown, instead pass any error to exception consumer. Convenience method to call forEach on a collection of values without having to use try/catch in lambda- Type Parameters:
T
- value type- Parameters:
values
- valuesconsumer
- consumer to handle value, which may throw exceptiononException
- exception handler to pass any exception to. Might be called once for every invocation of consumer.
-
tryResult
Invoke callable to fetch result and return it. If exception is thrown, return default value instead. Use this method to avoid try/catch blocks where the expected exception should result in a default value.- Type Parameters:
T
- value type- Parameters:
supplier
- callable to fetch result fromdefaultValue
- value to return if callable fails- Returns:
- the value from the callable, or defaultValue on exception
-
tryResult
public static <T> T tryResult(Callable<T> supplier, Supplier<T> defaultValueSupplier, Consumer<Throwable> onException) Invoke callable to fetch result and return it. If exception is thrown, return value from defaultValueSupplier instead. Notify onException of any exception caught. Use this method to avoid try/catch blocks where the expected exception should result in a default value.- Type Parameters:
T
- value type- Parameters:
supplier
- callable to fetch result fromdefaultValueSupplier
- supplier to fetch default value fromonException
- exception consumer to notify on exception- Returns:
- the value from the callable, or defaultValue on exception
- See Also:
-
tryStream
Wrap stream into a TryStream. A TryStream allows using map/filter lambdas which throws checked exceptions. Any exception thrown will be caught and rethrown by this method, this is just a convenience method to avoid having to use try/catch inside your lambdas.- Type Parameters:
T
- stream typeE
- checked exception- Parameters:
stream
- any stream- Returns:
- a TryStream wrapping the stream
- See Also:
-
tryStream
-