Failable¶
- public class org.apache.commons.lang3.function.Failable[source]¶
This class provides utility functions, and classes for working with the
java.util.functionpackage, or more generally, with Java 8 lambdas. More specifically, it attempts to address the fact that lambdas are supposed not to throw Exceptions, at least not checked Exceptions, like instances ofException. This enforces the use of constructs like:Consumer<java.lang.reflect.Method> consumer = m -> { try { m.invoke(o, args); } catch (Throwable t) { throw Failable.rethrow(t); } };
By replacing a
Consumer<O>with aFailableConsumer<O,? extends Throwable>, this can be written like follows:Functions.accept(m -> m.invoke(o, args));
Obviously, the second version is much more concise and the spirit of Lambda expressions is met better than the first version.
- Since:
3.11
Methods¶
Consumes a consumer and rethrows any exception as a |
|
Consumes a consumer and rethrows any exception as a |
|
Consumes a consumer and rethrows any exception as a |
|
Consumes a consumer and rethrows any exception as a |
|
Consumes a consumer and rethrows any exception as a |
|
Applies a function and rethrows any exception as a |
|
Applies a function and rethrows any exception as a |
|
Applies a function and rethrows any exception as a |
|
Applies a value to a function if the value isn’t |
|
Applies values to a chain of functions, where a |
|
|
Applies values to a chain of functions, where a |
Converts the given |
|
Converts the given |
|
Converts the given |
|
Converts the given |
|
Converts the given |
|
Converts the given |
|
Converts the given |
|
Converts the given |
|
Converts the given |
|
Calls a callable and rethrows any exception as a |
|
Gets the result of invoking the supplier. |
|
Gets the result of invoking the boolean supplier. |
|
Gets the result of invoking the double supplier. |
|
Gets the result of invoking the int supplier. |
|
Gets the result of invoking the long supplier. |
|
Gets the result of invoking the short supplier. |
|
Rethrows a |
|
Runs a runnable and rethrows any exception as a |
|
Converts the given collection into a |
|
Converts the given stream into a |
|
Tests a predicate and rethrows any exception as a |
|
Tests a predicate and rethrows any exception as a |
|
|
A simple try-with-resources implementation, that can be used, if your objects do not implement the |
A simple try-with-resources implementation, that can be used, if your objects do not implement the |
- public static <T, U, E extends Throwable> void accept(FailableBiConsumer<T, U, E> consumer, T object1, U object2)[source]¶
Consumes a consumer and rethrows any exception as a
RuntimeException.- Parameters:
consumer – The consumer to accept, may be null for a noop.
object1 – The first object to consume by
consumerobject2 – The second object to consume by
consumer
- Type parameters:
<T> – The type of the first argument the consumer accepts
<U> – The type of the second argument the consumer accepts
<E> – The type of checked exception the consumer may throw
- public static <T, E extends Throwable> void accept(FailableConsumer<T, E> consumer, T object)[source]¶
Consumes a consumer and rethrows any exception as a
RuntimeException.- Parameters:
consumer – The consumer to accept, may be null for a noop.
object – The object to consume by
consumer
- Type parameters:
<T> – The type the consumer accepts
<E> – The type of checked exception the consumer may throw
- public static <E extends Throwable> void accept(FailableDoubleConsumer<E> consumer, double value)[source]¶
Consumes a consumer and rethrows any exception as a
RuntimeException.- Parameters:
consumer – The consumer to accept, may be null for a noop.
value – The value to consume by
consumer
- Type parameters:
<E> – The type of checked exception the consumer may throw
- public static <E extends Throwable> void accept(FailableIntConsumer<E> consumer, int value)[source]¶
Consumes a consumer and rethrows any exception as a
RuntimeException.- Parameters:
consumer – The consumer to accept, may be null for a noop.
value – The value to consume by
consumer
- Type parameters:
<E> – The type of checked exception the consumer may throw
- public static <E extends Throwable> void accept(FailableLongConsumer<E> consumer, long value)[source]¶
Consumes a consumer and rethrows any exception as a
RuntimeException.- Parameters:
consumer – The consumer to accept, may be null for a noop.
value – The value to consume by
consumer
- Type parameters:
<E> – The type of checked exception the consumer may throw
- public static <T, U, R, E extends Throwable> R apply(FailableBiFunction<T, U, R, E> function, T input1, U input2)[source]¶
Applies a function and rethrows any exception as a
RuntimeException.- Parameters:
function – The function to apply
input1 – The first input to apply
functiononinput2 – The second input to apply
functionon
- Type parameters:
<T> – The type of the first argument the function accepts
<U> – The type of the second argument the function accepts
<R> – The return type of the function
<E> – The type of checked exception the function may throw
- Returns:
The value returned from the function
- public static <T, R, E extends Throwable> R apply(FailableFunction<T, R, E> function, T input)[source]¶
Applies a function and rethrows any exception as a
RuntimeException.- Parameters:
function – The function to apply
input – The input to apply
functionon
- Type parameters:
<T> – The type of the argument the function accepts
<R> – The return type of the function
<E> – The type of checked exception the function may throw
- Returns:
The value returned from the function
- public static <E extends Throwable> double applyAsDouble(FailableDoubleBinaryOperator<E> function, double left, double right)[source]¶
Applies a function and rethrows any exception as a
RuntimeException.- Parameters:
function – The function to apply
left – The first input to apply
functiononright – The second input to apply
functionon
- Type parameters:
<E> – The type of checked exception the function may throw
- Returns:
The value returned from the function
- public static <T, R, E extends Throwable> R applyNonNull(T value, FailableFunction<? super T, ? extends R, E> mapper) throws E[source]¶
Applies a value to a function if the value isn’t
null, otherwise the method returnsnull. If the value isn’tnullthen return the result of the applying function.Failable.applyNonNull("a", String::toUpperCase) = "A" Failable.applyNonNull(null, String::toUpperCase) = null Failable.applyNonNull("a", s -> null) = null
Useful when working with expressions that may return
nullas it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to usingOptionalthat is shorter and has less allocation.- Type parameters:
<T> – The type of the input of this method and the function.
<R> – The type of the result of the function and this method.
<E> – The type of thrown exception or error.
- Parameters:
value – The value to apply the function to, may be
null.mapper – The function to apply, must not be
null.
- Returns:
The result of the function (which may be
null) ornullif the input value isnull.- Throws:
E – Thrown by the given function.
- See also:
- Since:
3.19.0
- public static <T, U, R, E1 extends Throwable, E2 extends Throwable> R applyNonNull(T value1, FailableFunction<? super T, ? extends U, E1> mapper1, FailableFunction<? super U, ? extends R, E2> mapper2) throws E1, E2[source]¶
Applies values to a chain of functions, where a
nullcan short-circuit each step. A function is only applied if the previous value is notnull, otherwise this method returnsnull.Failable.applyNonNull(" a ", String::toUpperCase, String::trim) = "A" Failable.applyNonNull(null, String::toUpperCase, String::trim) = null Failable.applyNonNull(" a ", s -> null, String::trim) = null Failable.applyNonNull(" a ", String::toUpperCase, s -> null) = null
Useful when working with expressions that may return
nullas it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to usingOptionalthat is shorter and has less allocation.- Type parameters:
<T> – The type of the input of this method and the first function.
<U> – The type of the result of the first function and the input to the second function.
<R> – The type of the result of the second function and this method.
<E1> – The type of thrown exception or error by the first function.
<E2> – The type of thrown exception or error by the second function.
- Parameters:
value1 – The value to apply the functions to, may be
null.mapper1 – The first function to apply, must not be
null.mapper2 – The second function to apply, must not be
null.
- Returns:
The result of the final function (which may be
null) ornullif the input value or any intermediate value isnull.- Throws:
E1 – Thrown by the first function.
E2 – Thrown by the second function.
- See also:
- Since:
3.19.0
- public static <T, U, V, R, E1 extends Throwable, E2 extends Throwable, E3 extends Throwable> R applyNonNull(T value1, FailableFunction<? super T, ? extends U, E1> mapper1, FailableFunction<? super U, ? extends V, E2> mapper2, FailableFunction<? super V, ? extends R, E3> mapper3) throws E1, E2, E3[source]¶
Applies values to a chain of functions, where a
nullcan short-circuit each step. A function is only applied if the previous value is notnull, otherwise this method returnsnull.Failable.applyNonNull(" abc ", String::toUpperCase, String::trim, StringUtils::reverse) = "CBA" Failable.applyNonNull(null, String::toUpperCase, String::trim, StringUtils::reverse) = null Failable.applyNonNull(" abc ", s -> null, String::trim, StringUtils::reverse) = null Failable.applyNonNull(" abc ", String::toUpperCase, s -> null, StringUtils::reverse) = null Failable.applyNonNull(" abc ", String::toUpperCase, String::trim, s -> null) = null
Useful when working with expressions that may return
nullas it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to usingOptionalthat is shorter and has less allocation.- Type parameters:
<T> – The type of the input of this method and the first function.
<U> – The type of the result of the first function and the input to the second function.
<V> – The type of the result of the second function and the input to the third function.
<R> – The type of the result of the third function and this method.
<E1> – The type of thrown exception or error by the first function.
<E2> – The type of thrown exception or error by the second function.
<E3> – The type of thrown exception or error by the second function.
- Parameters:
value1 – The value to apply the first function, may be
null.mapper1 – The first function to apply, must not be
null.mapper2 – The second function to apply, must not be
null.mapper3 – The third function to apply, must not be
null.
- Returns:
The result of the final function (which may be
null) ornullif the input value or any intermediate value isnull.- Throws:
E1 – Thrown by the first function.
E2 – Thrown by the second function.
E3 – Thrown by the third function.
- See also:
- Since:
3.19.0
- public static <T, U> BiConsumer<T, U> asBiConsumer(FailableBiConsumer<T, U, ?> consumer)[source]¶
Converts the given
FailableBiConsumerinto a standardBiConsumer.- Type parameters:
<T> – The type of the first argument of the consumers
<U> – The type of the second argument of the consumers
- Parameters:
consumer – A failable
BiConsumer- Returns:
A standard
BiConsumer
- public static <T, U, R> BiFunction<T, U, R> asBiFunction(FailableBiFunction<T, U, R, ?> function)[source]¶
Converts the given
FailableBiFunctioninto a standardBiFunction.- Type parameters:
<T> – The type of the first argument of the input of the functions
<U> – The type of the second argument of the input of the functions
<R> – The type of the output of the functions
- Parameters:
function – A
FailableBiFunction- Returns:
A standard
BiFunction
- public static <T, U> BiPredicate<T, U> asBiPredicate(FailableBiPredicate<T, U, ?> predicate)[source]¶
Converts the given
FailableBiPredicateinto a standardBiPredicate.- Type parameters:
<T> – The type of the first argument used by the predicates
<U> – The type of the second argument used by the predicates
- Parameters:
predicate – A
FailableBiPredicate- Returns:
A standard
BiPredicate
- public static <V> Callable<V> asCallable(FailableCallable<V, ?> callable)[source]¶
Converts the given
FailableCallableinto a standardCallable.- Type parameters:
<V> – The type used by the callables
- Parameters:
callable – A
FailableCallable- Returns:
A standard
Callable
- public static <T> Consumer<T> asConsumer(FailableConsumer<T, ?> consumer)[source]¶
Converts the given
FailableConsumerinto a standardConsumer.- Type parameters:
<T> – The type used by the consumers
- Parameters:
consumer – A
FailableConsumer- Returns:
A standard
Consumer
- public static <T, R> Function<T, R> asFunction(FailableFunction<T, R, ?> function)[source]¶
Converts the given
FailableFunctioninto a standardFunction.- Type parameters:
<T> – The type of the input of the functions
<R> – The type of the output of the functions
- Parameters:
function – A {code FailableFunction}
- Returns:
A standard
Function
- public static <T> Predicate<T> asPredicate(FailablePredicate<T, ?> predicate)[source]¶
Converts the given
FailablePredicateinto a standardPredicate.- Type parameters:
<T> – The type used by the predicates
- Parameters:
predicate – A
FailablePredicate- Returns:
A standard
Predicate
- public static Runnable asRunnable(FailableRunnable<?> runnable)[source]¶
Converts the given
FailableRunnableinto a standardRunnable.- Parameters:
runnable – A
FailableRunnable- Returns:
A standard
Runnable
- public static <T> Supplier<T> asSupplier(FailableSupplier<T, ?> supplier)[source]¶
Converts the given
FailableSupplierinto a standardSupplier.- Type parameters:
<T> – The type supplied by the suppliers
- Parameters:
supplier – A
FailableSupplier- Returns:
A standard
Supplier
- public static <V, E extends Throwable> V call(FailableCallable<V, E> callable)[source]¶
Calls a callable and rethrows any exception as a
RuntimeException.- Parameters:
callable – The callable to call
- Type parameters:
<V> – The return type of the callable
<E> – The type of checked exception the callable may throw
- Returns:
The value returned from the callable
- public static <T, E extends Throwable> T get(FailableSupplier<T, E> supplier)[source]¶
Gets the result of invoking the supplier.
- Parameters:
supplier – The supplier to invoke.
- Type parameters:
<T> – The supplier’s output type.
<E> – The type of checked exception, which the supplier can throw.
- Returns:
The object, which has been created by the supplier
- public static <E extends Throwable> boolean getAsBoolean(FailableBooleanSupplier<E> supplier)[source]¶
Gets the result of invoking the boolean supplier.
- Parameters:
supplier – The boolean supplier to invoke.
- Type parameters:
<E> – The type of checked exception, which the supplier can throw.
- Returns:
The boolean, which has been created by the supplier
- public static <E extends Throwable> double getAsDouble(FailableDoubleSupplier<E> supplier)[source]¶
Gets the result of invoking the double supplier.
- Parameters:
supplier – The double supplier to invoke.
- Type parameters:
<E> – The type of checked exception, which the supplier can throw.
- Returns:
The double, which has been created by the supplier
- public static <E extends Throwable> int getAsInt(FailableIntSupplier<E> supplier)[source]¶
Gets the result of invoking the int supplier.
- Parameters:
supplier – The int supplier to invoke.
- Type parameters:
<E> – The type of checked exception, which the supplier can throw.
- Returns:
The int, which has been created by the supplier
- public static <E extends Throwable> long getAsLong(FailableLongSupplier<E> supplier)[source]¶
Gets the result of invoking the long supplier.
- Parameters:
supplier – The long supplier to invoke.
- Type parameters:
<E> – The type of checked exception, which the supplier can throw.
- Returns:
The long, which has been created by the supplier
- public static <E extends Throwable> short getAsShort(FailableShortSupplier<E> supplier)[source]¶
Gets the result of invoking the short supplier.
- Parameters:
supplier – The short supplier to invoke.
- Type parameters:
<E> – The type of checked exception, which the supplier can throw.
- Returns:
The short, which has been created by the supplier
- public static RuntimeException rethrow(Throwable throwable)[source]¶
Rethrows a
Throwableas an unchecked exception. If the argument is already unchecked, namely aRuntimeExceptionorErrorthen the argument will be rethrown without modification. If the exception isIOExceptionthen it will be wrapped into aUncheckedIOException. In every other cases the exception will be wrapped into aUndeclaredThrowableExceptionNote that there is a declared return type for this method, even though it never returns. The reason for that is to support the usual pattern:
throw rethrow(myUncheckedException);
instead of just calling the method. This pattern may help the Java compiler to recognize that at that point an exception will be thrown and the code flow analysis will not demand otherwise mandatory commands that could follow the method call, like a
returnstatement from a value returning method.- Parameters:
throwable – The throwable to rethrow possibly wrapped into an unchecked exception
- Returns:
Never returns anything, this method never terminates normally.
- public static <E extends Throwable> void run(FailableRunnable<E> runnable)[source]¶
Runs a runnable and rethrows any exception as a
RuntimeException.- Parameters:
runnable – The runnable to run, may be null for a noop.
- Type parameters:
<E> – The type of checked exception the runnable may throw.
- public static <E> Streams.FailableStream<E> stream(Collection<E> collection)[source]¶
Converts the given collection into a
FailableStream. TheFailableStreamconsists of the collections elements. Shortcut forFunctions.stream(collection.stream());
- Parameters:
collection – The collection, which is being converted into a
FailableStream.- Type parameters:
<E> – The collections element type. (In turn, the result streams element type.)
- Returns:
The created
FailableStream.
- public static <T> Streams.FailableStream<T> stream(Stream<T> stream)[source]¶
Converts the given stream into a
FailableStream. TheFailableStreamconsists of the same elements, than the input stream. However, failable lambdas, likeFailablePredicate,FailableFunction, andFailableConsumermay be applied, rather thanPredicate,Function,Consumer, etc.- Parameters:
stream – The stream, which is being converted into a
FailableStream.- Type parameters:
<T> – The streams element type.
- Returns:
The created
FailableStream.
- public static <T, U, E extends Throwable> boolean test(FailableBiPredicate<T, U, E> predicate, T object1, U object2)[source]¶
Tests a predicate and rethrows any exception as a
RuntimeException.- Parameters:
predicate – The predicate to test
object1 – The first input to test by
predicateobject2 – The second input to test by
predicate
- Type parameters:
<T> – The type of the first argument the predicate tests
<U> – The type of the second argument the predicate tests
<E> – The type of checked exception the predicate may throw
- Returns:
The boolean value returned by the predicate
- public static <T, E extends Throwable> boolean test(FailablePredicate<T, E> predicate, T object)[source]¶
Tests a predicate and rethrows any exception as a
RuntimeException.- Parameters:
predicate – The predicate to test
object – The input to test by
predicate
- Type parameters:
<T> – The type of argument the predicate tests
<E> – The type of checked exception the predicate may throw
- Returns:
The boolean value returned by the predicate
- @SafeVarargs public static void tryWithResources(FailableRunnable<? extends Throwable> action, FailableConsumer<Throwable, ? extends Throwable> errorHandler, FailableRunnable<? extends Throwable>... resources)[source]¶
A simple try-with-resources implementation, that can be used, if your objects do not implement the
AutoCloseableinterface. The method executes theaction. The method guarantees, that all theresourcesare being executed, in the given order, afterwards, and regardless of success, or failure. If either the original action, or any of the resource action fails, then the first failure (AKAThrowable) is rethrown. Example use:final FileInputStream fis = new FileInputStream("my.file"); Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());
- Parameters:
action – The action to execute. This object will always be invoked.
errorHandler – An optional error handler, which will be invoked finally, if any error occurred. The error handler will receive the first error, AKA
Throwable.resources – The resource actions to execute. All resource actions will be invoked, in the given order. A resource action is an instance of
FailableRunnable, which will be executed.
- See also:
- @SafeVarargs public static void tryWithResources(FailableRunnable<? extends Throwable> action, FailableRunnable<? extends Throwable>... resources)[source]¶
A simple try-with-resources implementation, that can be used, if your objects do not implement the
AutoCloseableinterface. The method executes theaction. The method guarantees, that all theresourcesare being executed, in the given order, afterwards, and regardless of success, or failure. If either the original action, or any of the resource action fails, then the first failure (AKAThrowable) is rethrown. Example use:final FileInputStream fis = new FileInputStream("my.file"); Functions.tryWithResources(useInputStream(fis), () -> fis.close());
- Parameters:
action – The action to execute. This object will always be invoked.
resources – The resource actions to execute. All resource actions will be invoked, in the given order. A resource action is an instance of
FailableRunnable, which will be executed.
- See also:
#tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)