Class Failable
java.lang.Object
org.apache.commons.lang3.function.Failable
This class provides utility functions, and classes for working with the 
java.util.function package, 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, AKA instances of Exception. 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 a FailableConsumer<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 second version.
- Since:
- 3.11
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T,U, E extends Throwable> 
 voidaccept(FailableBiConsumer<T, U, E> consumer, T object1, U object2) Consumes a consumer and rethrows any exception as aRuntimeException.static <T,E extends Throwable> 
 voidaccept(FailableConsumer<T, E> consumer, T object) Consumes a consumer and rethrows any exception as aRuntimeException.static <E extends Throwable>
 voidaccept(FailableDoubleConsumer<E> consumer, double value) Consumes a consumer and rethrows any exception as aRuntimeException.static <E extends Throwable>
 voidaccept(FailableIntConsumer<E> consumer, int value) Consumes a consumer and rethrows any exception as aRuntimeException.static <E extends Throwable>
 voidaccept(FailableLongConsumer<E> consumer, long value) Consumes a consumer and rethrows any exception as aRuntimeException.static <T,U, R, E extends Throwable> 
 Rapply(FailableBiFunction<T, U, R, E> function, T input1, U input2) Applies a function and rethrows any exception as aRuntimeException.static <T,R, E extends Throwable> 
 Rapply(FailableFunction<T, R, E> function, T input) Applies a function and rethrows any exception as aRuntimeException.static <E extends Throwable>
 doubleapplyAsDouble(FailableDoubleBinaryOperator<E> function, double left, double right) Applies a function and rethrows any exception as aRuntimeException.static <T,U> BiConsumer<T, U> asBiConsumer(FailableBiConsumer<T, U, ?> consumer) Converts the givenFailableBiConsumerinto a standardBiConsumer.static <T,U, R> BiFunction<T, U, R> asBiFunction(FailableBiFunction<T, U, R, ?> function) Converts the givenFailableBiFunctioninto a standardBiFunction.static <T,U> BiPredicate<T, U> asBiPredicate(FailableBiPredicate<T, U, ?> predicate) Converts the givenFailableBiPredicateinto a standardBiPredicate.static <V> Callable<V>asCallable(FailableCallable<V, ?> callable) Converts the givenFailableCallableinto a standardCallable.static <T> Consumer<T>asConsumer(FailableConsumer<T, ?> consumer) Converts the givenFailableConsumerinto a standardConsumer.static <T,R> Function<T, R> asFunction(FailableFunction<T, R, ?> function) Converts the givenFailableFunctioninto a standardFunction.static <T> Predicate<T>asPredicate(FailablePredicate<T, ?> predicate) Converts the givenFailablePredicateinto a standardPredicate.static RunnableasRunnable(FailableRunnable<?> runnable) Converts the givenFailableRunnableinto a standardRunnable.static <T> Supplier<T>asSupplier(FailableSupplier<T, ?> supplier) Converts the givenFailableSupplierinto a standardSupplier.static <V,E extends Throwable> 
 Vcall(FailableCallable<V, E> callable) Calls a callable and rethrows any exception as aRuntimeException.static <T,E extends Throwable> 
 Tget(FailableSupplier<T, E> supplier) Invokes a supplier, and returns the result.static <E extends Throwable>
 booleangetAsBoolean(FailableBooleanSupplier<E> supplier) Invokes a boolean supplier, and returns the result.static <E extends Throwable>
 doublegetAsDouble(FailableDoubleSupplier<E> supplier) Invokes a double supplier, and returns the result.static <E extends Throwable>
 intgetAsInt(FailableIntSupplier<E> supplier) Invokes an int supplier, and returns the result.static <E extends Throwable>
 longgetAsLong(FailableLongSupplier<E> supplier) Invokes a long supplier, and returns the result.static <E extends Throwable>
 shortgetAsShort(FailableShortSupplier<E> supplier) Invokes a short supplier, and returns the result.static RuntimeExceptionRethrows aThrowableas an unchecked exception.static <E extends Throwable>
 voidrun(FailableRunnable<E> runnable) Runs a runnable and rethrows any exception as aRuntimeException.static <E> Streams.FailableStream<E>stream(Collection<E> collection) Converts the given collection into aStreams.FailableStream.static <T> Streams.FailableStream<T>Converts the given stream into aStreams.FailableStream.static <T,U, E extends Throwable> 
 booleantest(FailableBiPredicate<T, U, E> predicate, T object1, U object2) Tests a predicate and rethrows any exception as aRuntimeException.static <T,E extends Throwable> 
 booleantest(FailablePredicate<T, E> predicate, T object) Tests a predicate and rethrows any exception as aRuntimeException.static voidtryWithResources(FailableRunnable<? extends Throwable> action, FailableConsumer<Throwable, ? extends Throwable> errorHandler, FailableRunnable<? extends Throwable>... resources) A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseableinterface.static voidtryWithResources(FailableRunnable<? extends Throwable> action, FailableRunnable<? extends Throwable>... resources) A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseableinterface.
- 
Method Details- 
acceptpublic static <T,U, void acceptE extends Throwable> (FailableBiConsumer<T, U, E> consumer, T object1, U object2) Consumes a consumer and rethrows any exception as aRuntimeException.- 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
- Parameters:
- consumer- the consumer to consume
- object1- the first object to consume by- consumer
- object2- the second object to consume by- consumer
 
- 
acceptConsumes a consumer and rethrows any exception as aRuntimeException.- Type Parameters:
- T- the type the consumer accepts
- E- the type of checked exception the consumer may throw
- Parameters:
- consumer- the consumer to consume
- object- the object to consume by- consumer
 
- 
acceptConsumes a consumer and rethrows any exception as aRuntimeException.- Type Parameters:
- E- the type of checked exception the consumer may throw
- Parameters:
- consumer- the consumer to consume
- value- the value to consume by- consumer
 
- 
acceptConsumes a consumer and rethrows any exception as aRuntimeException.- Type Parameters:
- E- the type of checked exception the consumer may throw
- Parameters:
- consumer- the consumer to consume
- value- the value to consume by- consumer
 
- 
acceptConsumes a consumer and rethrows any exception as aRuntimeException.- Type Parameters:
- E- the type of checked exception the consumer may throw
- Parameters:
- consumer- the consumer to consume
- value- the value to consume by- consumer
 
- 
applypublic static <T,U, R applyR, E extends Throwable> (FailableBiFunction<T, U, R, E> function, T input1, U input2) Applies a function and rethrows any exception as aRuntimeException.- 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
- Parameters:
- function- the function to apply
- input1- the first input to apply- functionon
- input2- the second input to apply- functionon
- Returns:
- the value returned from the function
 
- 
applyApplies a function and rethrows any exception as aRuntimeException.- 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
- Parameters:
- function- the function to apply
- input- the input to apply- functionon
- Returns:
- the value returned from the function
 
- 
applyAsDoublepublic static <E extends Throwable> double applyAsDouble(FailableDoubleBinaryOperator<E> function, double left, double right) Applies a function and rethrows any exception as aRuntimeException.- Type Parameters:
- E- the type of checked exception the function may throw
- Parameters:
- function- the function to apply
- left- the first input to apply- functionon
- right- the second input to apply- functionon
- Returns:
- the value returned from the function
 
- 
asBiConsumerConverts the givenFailableBiConsumerinto 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
 
- 
asBiFunctionConverts the givenFailableBiFunctioninto 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
 
- 
asBiPredicateConverts the givenFailableBiPredicateinto 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
 
- 
asCallableConverts the givenFailableCallableinto a standardCallable.- Type Parameters:
- V- the type used by the callables
- Parameters:
- callable- a- FailableCallable
- Returns:
- a standard Callable
 
- 
asConsumerConverts the givenFailableConsumerinto a standardConsumer.- Type Parameters:
- T- the type used by the consumers
- Parameters:
- consumer- a- FailableConsumer
- Returns:
- a standard Consumer
 
- 
asFunctionConverts the givenFailableFunctioninto 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
 
- 
asPredicateConverts the givenFailablePredicateinto a standardPredicate.- Type Parameters:
- T- the type used by the predicates
- Parameters:
- predicate- a- FailablePredicate
- Returns:
- a standard Predicate
 
- 
asRunnableConverts the givenFailableRunnableinto a standardRunnable.- Parameters:
- runnable- a- FailableRunnable
- Returns:
- a standard Runnable
 
- 
asSupplierConverts the givenFailableSupplierinto a standardSupplier.- Type Parameters:
- T- the type supplied by the suppliers
- Parameters:
- supplier- a- FailableSupplier
- Returns:
- a standard Supplier
 
- 
callCalls a callable and rethrows any exception as aRuntimeException.- Type Parameters:
- V- the return type of the callable
- E- the type of checked exception the callable may throw
- Parameters:
- callable- the callable to call
- Returns:
- the value returned from the callable
 
- 
getInvokes a supplier, and returns the result.- Type Parameters:
- T- The suppliers output type.
- E- The type of checked exception, which the supplier can throw.
- Parameters:
- supplier- The supplier to invoke.
- Returns:
- The object, which has been created by the supplier
 
- 
getAsBooleanInvokes a boolean supplier, and returns the result.- Type Parameters:
- E- The type of checked exception, which the supplier can throw.
- Parameters:
- supplier- The boolean supplier to invoke.
- Returns:
- The boolean, which has been created by the supplier
 
- 
getAsDoubleInvokes a double supplier, and returns the result.- Type Parameters:
- E- The type of checked exception, which the supplier can throw.
- Parameters:
- supplier- The double supplier to invoke.
- Returns:
- The double, which has been created by the supplier
 
- 
getAsIntInvokes an int supplier, and returns the result.- Type Parameters:
- E- The type of checked exception, which the supplier can throw.
- Parameters:
- supplier- The int supplier to invoke.
- Returns:
- The int, which has been created by the supplier
 
- 
getAsLongInvokes a long supplier, and returns the result.- Type Parameters:
- E- The type of checked exception, which the supplier can throw.
- Parameters:
- supplier- The long supplier to invoke.
- Returns:
- The long, which has been created by the supplier
 
- 
getAsShortInvokes a short supplier, and returns the result.- Type Parameters:
- E- The type of checked exception, which the supplier can throw.
- Parameters:
- supplier- The short supplier to invoke.
- Returns:
- The short, which has been created by the supplier
 
- 
rethrowRethrows aThrowableas 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.
 
- 
runRuns a runnable and rethrows any exception as aRuntimeException.- Type Parameters:
- E- the type of checked exception the runnable may throw
- Parameters:
- runnable- The runnable to run
 
- 
streamConverts the given collection into aStreams.FailableStream. TheStreams.FailableStreamconsists of the collections elements. Shortcut forFunctions.stream(collection.stream()); - Type Parameters:
- E- The collections element type. (In turn, the result streams element type.)
- Parameters:
- collection- The collection, which is being converted into a- Streams.FailableStream.
- Returns:
- The created Streams.FailableStream.
 
- 
streamConverts the given stream into aStreams.FailableStream. TheStreams.FailableStreamconsists of the same elements, than the input stream. However, failable lambdas, likeFailablePredicate,FailableFunction, andFailableConsumermay be applied, rather thanPredicate,Function,Consumer, etc.- Type Parameters:
- T- The streams element type.
- Parameters:
- stream- The stream, which is being converted into a- Streams.FailableStream.
- Returns:
- The created Streams.FailableStream.
 
- 
testpublic static <T,U, boolean testE extends Throwable> (FailableBiPredicate<T, U, E> predicate, T object1, U object2) Tests a predicate and rethrows any exception as aRuntimeException.- 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
- Parameters:
- predicate- the predicate to test
- object1- the first input to test by- predicate
- object2- the second input to test by- predicate
- Returns:
- the boolean value returned by the predicate
 
- 
testTests a predicate and rethrows any exception as aRuntimeException.- Type Parameters:
- T- the type of argument the predicate tests
- E- the type of checked exception the predicate may throw
- Parameters:
- predicate- the predicate to test
- object- the input to test by- predicate
- Returns:
- the boolean value returned by the predicate
 
- 
tryWithResources@SafeVarargs public static void tryWithResources(FailableRunnable<? extends Throwable> action, FailableConsumer<Throwable, ? extends Throwable> errorHandler, FailableRunnable<? extends Throwable>... resources) A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseableinterface. 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:
 
- 
tryWithResources@SafeVarargs public static void tryWithResources(FailableRunnable<? extends Throwable> action, FailableRunnable<? extends Throwable>... resources) A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseableinterface. 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:
 
 
-