Class Failable
- java.lang.Object
-
- org.apache.commons.lang3.function.Failable
-
public class Failable extends java.lang.Object
This class provides utility functions, and classes for working with thejava.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 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 second version.
- Since:
- 3.11
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,U,E extends java.lang.Throwable>
voidaccept(FailableBiConsumer<T,U,E> consumer, T object1, U object2)
Consumes a consumer and rethrows any exception as aRuntimeException
.static <T,E extends java.lang.Throwable>
voidaccept(FailableConsumer<T,E> consumer, T object)
Consumes a consumer and rethrows any exception as aRuntimeException
.static <E extends java.lang.Throwable>
voidaccept(FailableDoubleConsumer<E> consumer, double value)
Consumes a consumer and rethrows any exception as aRuntimeException
.static <E extends java.lang.Throwable>
voidaccept(FailableIntConsumer<E> consumer, int value)
Consumes a consumer and rethrows any exception as aRuntimeException
.static <E extends java.lang.Throwable>
voidaccept(FailableLongConsumer<E> consumer, long value)
Consumes a consumer and rethrows any exception as aRuntimeException
.static <T,U,R,E extends java.lang.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 java.lang.Throwable>
Rapply(FailableFunction<T,R,E> function, T input)
Applies a function and rethrows any exception as aRuntimeException
.static <E extends java.lang.Throwable>
doubleapplyAsDouble(FailableDoubleBinaryOperator<E> function, double left, double right)
Applies a function and rethrows any exception as aRuntimeException
.static <T,U>
java.util.function.BiConsumer<T,U>asBiConsumer(FailableBiConsumer<T,U,?> consumer)
Converts the givenFailableBiConsumer
into a standardBiConsumer
.static <T,U,R>
java.util.function.BiFunction<T,U,R>asBiFunction(FailableBiFunction<T,U,R,?> function)
Converts the givenFailableBiFunction
into a standardBiFunction
.static <T,U>
java.util.function.BiPredicate<T,U>asBiPredicate(FailableBiPredicate<T,U,?> predicate)
Converts the givenFailableBiPredicate
into a standardBiPredicate
.static <V> java.util.concurrent.Callable<V>
asCallable(FailableCallable<V,?> callable)
Converts the givenFailableCallable
into a standardCallable
.static <T> java.util.function.Consumer<T>
asConsumer(FailableConsumer<T,?> consumer)
Converts the givenFailableConsumer
into a standardConsumer
.static <T,R>
java.util.function.Function<T,R>asFunction(FailableFunction<T,R,?> function)
Converts the givenFailableFunction
into a standardFunction
.static <T> java.util.function.Predicate<T>
asPredicate(FailablePredicate<T,?> predicate)
Converts the givenFailablePredicate
into a standardPredicate
.static java.lang.Runnable
asRunnable(FailableRunnable<?> runnable)
Converts the givenFailableRunnable
into a standardRunnable
.static <T> java.util.function.Supplier<T>
asSupplier(FailableSupplier<T,?> supplier)
Converts the givenFailableSupplier
into a standardSupplier
.static <V,E extends java.lang.Throwable>
Vcall(FailableCallable<V,E> callable)
Calls a callable and rethrows any exception as aRuntimeException
.static <T,E extends java.lang.Throwable>
Tget(FailableSupplier<T,E> supplier)
Invokes a supplier, and returns the result.static <E extends java.lang.Throwable>
booleangetAsBoolean(FailableBooleanSupplier<E> supplier)
Invokes a boolean supplier, and returns the result.static <E extends java.lang.Throwable>
doublegetAsDouble(FailableDoubleSupplier<E> supplier)
Invokes a double supplier, and returns the result.static <E extends java.lang.Throwable>
intgetAsInt(FailableIntSupplier<E> supplier)
Invokes an int supplier, and returns the result.static <E extends java.lang.Throwable>
longgetAsLong(FailableLongSupplier<E> supplier)
Invokes a long supplier, and returns the result.static <E extends java.lang.Throwable>
shortgetAsShort(FailableShortSupplier<E> supplier)
Invokes a short supplier, and returns the result.static java.lang.RuntimeException
rethrow(java.lang.Throwable throwable)
Rethrows aThrowable
as an unchecked exception.static <E extends java.lang.Throwable>
voidrun(FailableRunnable<E> runnable)
Runs a runnable and rethrows any exception as aRuntimeException
.static <E> Streams.FailableStream<E>
stream(java.util.Collection<E> collection)
Converts the given collection into aStreams.FailableStream
.static <T> Streams.FailableStream<T>
stream(java.util.stream.Stream<T> stream)
Converts the given stream into aStreams.FailableStream
.static <T,U,E extends java.lang.Throwable>
booleantest(FailableBiPredicate<T,U,E> predicate, T object1, U object2)
Tests a predicate and rethrows any exception as aRuntimeException
.static <T,E extends java.lang.Throwable>
booleantest(FailablePredicate<T,E> predicate, T object)
Tests a predicate and rethrows any exception as aRuntimeException
.static void
tryWithResources(FailableRunnable<? extends java.lang.Throwable> action, FailableConsumer<java.lang.Throwable,? extends java.lang.Throwable> errorHandler, FailableRunnable<? extends java.lang.Throwable>... resources)
A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseable
interface.static void
tryWithResources(FailableRunnable<? extends java.lang.Throwable> action, FailableRunnable<? extends java.lang.Throwable>... resources)
A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseable
interface.
-
-
-
Method Detail
-
accept
public static <T,U,E extends java.lang.Throwable> void accept(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 acceptsU
- the type of the second argument the consumer acceptsE
- the type of checked exception the consumer may throw- Parameters:
consumer
- the consumer to consumeobject1
- the first object to consume byconsumer
object2
- the second object to consume byconsumer
-
accept
public static <T,E extends java.lang.Throwable> void accept(FailableConsumer<T,E> consumer, T object)
Consumes a consumer and rethrows any exception as aRuntimeException
.- Type Parameters:
T
- the type the consumer acceptsE
- the type of checked exception the consumer may throw- Parameters:
consumer
- the consumer to consumeobject
- the object to consume byconsumer
-
accept
public static <E extends java.lang.Throwable> void accept(FailableDoubleConsumer<E> consumer, double value)
Consumes 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 consumevalue
- the value to consume byconsumer
-
accept
public static <E extends java.lang.Throwable> void accept(FailableIntConsumer<E> consumer, int value)
Consumes 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 consumevalue
- the value to consume byconsumer
-
accept
public static <E extends java.lang.Throwable> void accept(FailableLongConsumer<E> consumer, long value)
Consumes 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 consumevalue
- the value to consume byconsumer
-
apply
public static <T,U,R,E extends java.lang.Throwable> R apply(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 acceptsU
- the type of the second argument the function acceptsR
- the return type of the functionE
- the type of checked exception the function may throw- Parameters:
function
- the function to applyinput1
- the first input to applyfunction
oninput2
- the second input to applyfunction
on- Returns:
- the value returned from the function
-
apply
public static <T,R,E extends java.lang.Throwable> R apply(FailableFunction<T,R,E> function, T input)
Applies a function and rethrows any exception as aRuntimeException
.- Type Parameters:
T
- the type of the argument the function acceptsR
- the return type of the functionE
- the type of checked exception the function may throw- Parameters:
function
- the function to applyinput
- the input to applyfunction
on- Returns:
- the value returned from the function
-
applyAsDouble
public static <E extends java.lang.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 applyleft
- the first input to applyfunction
onright
- the second input to applyfunction
on- Returns:
- the value returned from the function
-
asBiConsumer
public static <T,U> java.util.function.BiConsumer<T,U> asBiConsumer(FailableBiConsumer<T,U,?> consumer)
Converts the givenFailableBiConsumer
into a standardBiConsumer
.- Type Parameters:
T
- the type of the first argument of the consumersU
- the type of the second argument of the consumers- Parameters:
consumer
- a failableBiConsumer
- Returns:
- a standard
BiConsumer
-
asBiFunction
public static <T,U,R> java.util.function.BiFunction<T,U,R> asBiFunction(FailableBiFunction<T,U,R,?> function)
Converts the givenFailableBiFunction
into a standardBiFunction
.- Type Parameters:
T
- the type of the first argument of the input of the functionsU
- the type of the second argument of the input of the functionsR
- the type of the output of the functions- Parameters:
function
- aFailableBiFunction
- Returns:
- a standard
BiFunction
-
asBiPredicate
public static <T,U> java.util.function.BiPredicate<T,U> asBiPredicate(FailableBiPredicate<T,U,?> predicate)
Converts the givenFailableBiPredicate
into a standardBiPredicate
.- Type Parameters:
T
- the type of the first argument used by the predicatesU
- the type of the second argument used by the predicates- Parameters:
predicate
- aFailableBiPredicate
- Returns:
- a standard
BiPredicate
-
asCallable
public static <V> java.util.concurrent.Callable<V> asCallable(FailableCallable<V,?> callable)
Converts the givenFailableCallable
into a standardCallable
.- Type Parameters:
V
- the type used by the callables- Parameters:
callable
- aFailableCallable
- Returns:
- a standard
Callable
-
asConsumer
public static <T> java.util.function.Consumer<T> asConsumer(FailableConsumer<T,?> consumer)
Converts the givenFailableConsumer
into a standardConsumer
.- Type Parameters:
T
- the type used by the consumers- Parameters:
consumer
- aFailableConsumer
- Returns:
- a standard
Consumer
-
asFunction
public static <T,R> java.util.function.Function<T,R> asFunction(FailableFunction<T,R,?> function)
Converts the givenFailableFunction
into a standardFunction
.- Type Parameters:
T
- the type of the input of the functionsR
- the type of the output of the functions- Parameters:
function
- a {code FailableFunction}- Returns:
- a standard
Function
-
asPredicate
public static <T> java.util.function.Predicate<T> asPredicate(FailablePredicate<T,?> predicate)
Converts the givenFailablePredicate
into a standardPredicate
.- Type Parameters:
T
- the type used by the predicates- Parameters:
predicate
- aFailablePredicate
- Returns:
- a standard
Predicate
-
asRunnable
public static java.lang.Runnable asRunnable(FailableRunnable<?> runnable)
Converts the givenFailableRunnable
into a standardRunnable
.- Parameters:
runnable
- aFailableRunnable
- Returns:
- a standard
Runnable
-
asSupplier
public static <T> java.util.function.Supplier<T> asSupplier(FailableSupplier<T,?> supplier)
Converts the givenFailableSupplier
into a standardSupplier
.- Type Parameters:
T
- the type supplied by the suppliers- Parameters:
supplier
- aFailableSupplier
- Returns:
- a standard
Supplier
-
call
public static <V,E extends java.lang.Throwable> V call(FailableCallable<V,E> callable)
Calls a callable and rethrows any exception as aRuntimeException
.- Type Parameters:
V
- the return type of the callableE
- the type of checked exception the callable may throw- Parameters:
callable
- the callable to call- Returns:
- the value returned from the callable
-
get
public static <T,E extends java.lang.Throwable> T get(FailableSupplier<T,E> supplier)
Invokes 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
-
getAsBoolean
public static <E extends java.lang.Throwable> boolean getAsBoolean(FailableBooleanSupplier<E> supplier)
Invokes 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
-
getAsDouble
public static <E extends java.lang.Throwable> double getAsDouble(FailableDoubleSupplier<E> supplier)
Invokes 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
-
getAsInt
public static <E extends java.lang.Throwable> int getAsInt(FailableIntSupplier<E> supplier)
Invokes 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
-
getAsLong
public static <E extends java.lang.Throwable> long getAsLong(FailableLongSupplier<E> supplier)
Invokes 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
-
getAsShort
public static <E extends java.lang.Throwable> short getAsShort(FailableShortSupplier<E> supplier)
Invokes 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
-
rethrow
public static java.lang.RuntimeException rethrow(java.lang.Throwable throwable)
Rethrows a
Throwable
as an unchecked exception. If the argument is already unchecked, namely aRuntimeException
orError
then the argument will be rethrown without modification. If the exception isIOException
then it will be wrapped into aUncheckedIOException
. In every other cases the exception will be wrapped into aUndeclaredThrowableException
Note 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
return
statement from a value returning method.- Parameters:
throwable
- The throwable to rethrow ossibly wrapped into an unchecked exception- Returns:
- Never returns anything, this method never terminates normally.
-
run
public static <E extends java.lang.Throwable> void run(FailableRunnable<E> runnable)
Runs 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
-
stream
public static <E> Streams.FailableStream<E> stream(java.util.Collection<E> collection)
Converts the given collection into aStreams.FailableStream
. TheStreams.FailableStream
consists 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 aStreams.FailableStream
.- Returns:
- The created
Streams.FailableStream
.
-
stream
public static <T> Streams.FailableStream<T> stream(java.util.stream.Stream<T> stream)
Converts the given stream into aStreams.FailableStream
. TheStreams.FailableStream
consists of the same elements, than the input stream. However, failable lambdas, likeFailablePredicate
,FailableFunction
, andFailableConsumer
may be applied, rather thanPredicate
,Function
,Consumer
, etc.- Type Parameters:
T
- The streams element type.- Parameters:
stream
- The stream, which is being converted into aStreams.FailableStream
.- Returns:
- The created
Streams.FailableStream
.
-
test
public static <T,U,E extends java.lang.Throwable> boolean test(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 testsU
- the type of the second argument the predicate testsE
- the type of checked exception the predicate may throw- Parameters:
predicate
- the predicate to testobject1
- the first input to test bypredicate
object2
- the second input to test bypredicate
- Returns:
- the boolean value returned by the predicate
-
test
public static <T,E extends java.lang.Throwable> boolean test(FailablePredicate<T,E> predicate, T object)
Tests a predicate and rethrows any exception as aRuntimeException
.- Type Parameters:
T
- the type of argument the predicate testsE
- the type of checked exception the predicate may throw- Parameters:
predicate
- the predicate to testobject
- the input to test bypredicate
- Returns:
- the boolean value returned by the predicate
-
tryWithResources
@SafeVarargs public static void tryWithResources(FailableRunnable<? extends java.lang.Throwable> action, FailableConsumer<java.lang.Throwable,? extends java.lang.Throwable> errorHandler, FailableRunnable<? extends java.lang.Throwable>... resources)
A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseable
interface. The method executes theaction
. The method guarantees, that all theresources
are 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, AKAThrowable
.resources
- The resource actions to execute. All resource actions will be invoked, in the given order. A resource action is an instance ofFailableRunnable
, which will be executed.- See Also:
tryWithResources(FailableRunnable, FailableRunnable...)
-
tryWithResources
@SafeVarargs public static void tryWithResources(FailableRunnable<? extends java.lang.Throwable> action, FailableRunnable<? extends java.lang.Throwable>... resources)
A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseable
interface. The method executes theaction
. The method guarantees, that all theresources
are 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 ofFailableRunnable
, which will be executed.- See Also:
tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
-
-