public class Failable extends Object
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.
Modifier and Type | Method and Description |
---|---|
static <T,U,E extends Throwable> |
accept(FailableBiConsumer<T,U,E> consumer,
T object1,
U object2)
Consumes a consumer and rethrows any exception as a
RuntimeException . |
static <T,E extends Throwable> |
accept(FailableConsumer<T,E> consumer,
T object)
Consumes a consumer and rethrows any exception as a
RuntimeException . |
static <E extends Throwable> |
accept(FailableDoubleConsumer<E> consumer,
double value)
Consumes a consumer and rethrows any exception as a
RuntimeException . |
static <E extends Throwable> |
accept(FailableIntConsumer<E> consumer,
int value)
Consumes a consumer and rethrows any exception as a
RuntimeException . |
static <E extends Throwable> |
accept(FailableLongConsumer<E> consumer,
long value)
Consumes a consumer and rethrows any exception as a
RuntimeException . |
static <T,U,R,E extends Throwable> |
apply(FailableBiFunction<T,U,R,E> function,
T input1,
U input2)
Applies a function and rethrows any exception as a
RuntimeException . |
static <T,R,E extends Throwable> |
apply(FailableFunction<T,R,E> function,
T input)
Applies a function and rethrows any exception as a
RuntimeException . |
static <E extends Throwable> |
applyAsDouble(FailableDoubleBinaryOperator<E> function,
double left,
double right)
Applies a function and rethrows any exception as a
RuntimeException . |
static <T,U> BiConsumer<T,U> |
asBiConsumer(FailableBiConsumer<T,U,?> consumer)
Converts the given
FailableBiConsumer into a standard BiConsumer . |
static <T,U,R> BiFunction<T,U,R> |
asBiFunction(FailableBiFunction<T,U,R,?> function)
Converts the given
FailableBiFunction into a standard BiFunction . |
static <T,U> BiPredicate<T,U> |
asBiPredicate(FailableBiPredicate<T,U,?> predicate)
Converts the given
FailableBiPredicate into a standard BiPredicate . |
static <V> Callable<V> |
asCallable(FailableCallable<V,?> callable)
Converts the given
FailableCallable into a standard Callable . |
static <T> Consumer<T> |
asConsumer(FailableConsumer<T,?> consumer)
Converts the given
FailableConsumer into a standard Consumer . |
static <T,R> Function<T,R> |
asFunction(FailableFunction<T,R,?> function)
Converts the given
FailableFunction into a standard Function . |
static <T> Predicate<T> |
asPredicate(FailablePredicate<T,?> predicate)
Converts the given
FailablePredicate into a standard Predicate . |
static Runnable |
asRunnable(FailableRunnable<?> runnable)
Converts the given
FailableRunnable into a standard Runnable . |
static <T> Supplier<T> |
asSupplier(FailableSupplier<T,?> supplier)
Converts the given
FailableSupplier into a standard Supplier . |
static <V,E extends Throwable> |
call(FailableCallable<V,E> callable)
Calls a callable and rethrows any exception as a
RuntimeException . |
static <T,E extends Throwable> |
get(FailableSupplier<T,E> supplier)
Invokes a supplier, and returns the result.
|
static <E extends Throwable> |
getAsBoolean(FailableBooleanSupplier<E> supplier)
Invokes a boolean supplier, and returns the result.
|
static <E extends Throwable> |
getAsDouble(FailableDoubleSupplier<E> supplier)
Invokes a double supplier, and returns the result.
|
static <E extends Throwable> |
getAsInt(FailableIntSupplier<E> supplier)
Invokes an int supplier, and returns the result.
|
static <E extends Throwable> |
getAsLong(FailableLongSupplier<E> supplier)
Invokes a long supplier, and returns the result.
|
static <E extends Throwable> |
getAsShort(FailableShortSupplier<E> supplier)
Invokes a short supplier, and returns the result.
|
static RuntimeException |
rethrow(Throwable throwable)
Rethrows a
Throwable as an unchecked exception. |
static <E extends Throwable> |
run(FailableRunnable<E> runnable)
Runs a runnable and rethrows any exception as a
RuntimeException . |
static <E> Streams.FailableStream<E> |
stream(Collection<E> collection)
Converts the given collection into a
Streams.FailableStream . |
static <T> Streams.FailableStream<T> |
stream(Stream<T> stream)
Converts the given stream into a
Streams.FailableStream . |
static <T,U,E extends Throwable> |
test(FailableBiPredicate<T,U,E> predicate,
T object1,
U object2)
Tests a predicate and rethrows any exception as a
RuntimeException . |
static <T,E extends Throwable> |
test(FailablePredicate<T,E> predicate,
T object)
Tests a predicate and rethrows any exception as a
RuntimeException . |
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 the
AutoCloseable interface. |
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 the
AutoCloseable interface. |
public static <T,U,E extends Throwable> void accept(FailableBiConsumer<T,U,E> consumer, T object1, U object2)
RuntimeException
.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 throwconsumer
- the consumer to consumeobject1
- the first object to consume by consumer
object2
- the second object to consume by consumer
public static <T,E extends Throwable> void accept(FailableConsumer<T,E> consumer, T object)
RuntimeException
.T
- the type the consumer acceptsE
- the type of checked exception the consumer may throwconsumer
- the consumer to consumeobject
- the object to consume by consumer
public static <E extends Throwable> void accept(FailableDoubleConsumer<E> consumer, double value)
RuntimeException
.E
- the type of checked exception the consumer may throwconsumer
- the consumer to consumevalue
- the value to consume by consumer
public static <E extends Throwable> void accept(FailableIntConsumer<E> consumer, int value)
RuntimeException
.E
- the type of checked exception the consumer may throwconsumer
- the consumer to consumevalue
- the value to consume by consumer
public static <E extends Throwable> void accept(FailableLongConsumer<E> consumer, long value)
RuntimeException
.E
- the type of checked exception the consumer may throwconsumer
- the consumer to consumevalue
- the value to consume by consumer
public static <T,U,R,E extends Throwable> R apply(FailableBiFunction<T,U,R,E> function, T input1, U input2)
RuntimeException
.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 throwfunction
- the function to applyinput1
- the first input to apply function
oninput2
- the second input to apply function
onpublic static <T,R,E extends Throwable> R apply(FailableFunction<T,R,E> function, T input)
RuntimeException
.T
- the type of the argument the function acceptsR
- the return type of the functionE
- the type of checked exception the function may throwfunction
- the function to applyinput
- the input to apply function
onpublic static <E extends Throwable> double applyAsDouble(FailableDoubleBinaryOperator<E> function, double left, double right)
RuntimeException
.E
- the type of checked exception the function may throwfunction
- the function to applyleft
- the first input to apply function
onright
- the second input to apply function
onpublic static <T,U> BiConsumer<T,U> asBiConsumer(FailableBiConsumer<T,U,?> consumer)
FailableBiConsumer
into a standard BiConsumer
.T
- the type of the first argument of the consumersU
- the type of the second argument of the consumersconsumer
- a failable BiConsumer
BiConsumer
public static <T,U,R> BiFunction<T,U,R> asBiFunction(FailableBiFunction<T,U,R,?> function)
FailableBiFunction
into a standard BiFunction
.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 functionsfunction
- a FailableBiFunction
BiFunction
public static <T,U> BiPredicate<T,U> asBiPredicate(FailableBiPredicate<T,U,?> predicate)
FailableBiPredicate
into a standard BiPredicate
.T
- the type of the first argument used by the predicatesU
- the type of the second argument used by the predicatespredicate
- a FailableBiPredicate
BiPredicate
public static <V> Callable<V> asCallable(FailableCallable<V,?> callable)
FailableCallable
into a standard Callable
.V
- the type used by the callablescallable
- a FailableCallable
Callable
public static <T> Consumer<T> asConsumer(FailableConsumer<T,?> consumer)
FailableConsumer
into a standard Consumer
.T
- the type used by the consumersconsumer
- a FailableConsumer
Consumer
public static <T,R> Function<T,R> asFunction(FailableFunction<T,R,?> function)
FailableFunction
into a standard Function
.T
- the type of the input of the functionsR
- the type of the output of the functionsfunction
- a {code FailableFunction}Function
public static <T> Predicate<T> asPredicate(FailablePredicate<T,?> predicate)
FailablePredicate
into a standard Predicate
.T
- the type used by the predicatespredicate
- a FailablePredicate
Predicate
public static Runnable asRunnable(FailableRunnable<?> runnable)
FailableRunnable
into a standard Runnable
.runnable
- a FailableRunnable
Runnable
public static <T> Supplier<T> asSupplier(FailableSupplier<T,?> supplier)
FailableSupplier
into a standard Supplier
.T
- the type supplied by the supplierssupplier
- a FailableSupplier
Supplier
public static <V,E extends Throwable> V call(FailableCallable<V,E> callable)
RuntimeException
.V
- the return type of the callableE
- the type of checked exception the callable may throwcallable
- the callable to callpublic static <T,E extends Throwable> T get(FailableSupplier<T,E> supplier)
T
- The suppliers output type.E
- The type of checked exception, which the supplier can throw.supplier
- The supplier to invoke.public static <E extends Throwable> boolean getAsBoolean(FailableBooleanSupplier<E> supplier)
E
- The type of checked exception, which the supplier can throw.supplier
- The boolean supplier to invoke.public static <E extends Throwable> double getAsDouble(FailableDoubleSupplier<E> supplier)
E
- The type of checked exception, which the supplier can throw.supplier
- The double supplier to invoke.public static <E extends Throwable> int getAsInt(FailableIntSupplier<E> supplier)
E
- The type of checked exception, which the supplier can throw.supplier
- The int supplier to invoke.public static <E extends Throwable> long getAsLong(FailableLongSupplier<E> supplier)
E
- The type of checked exception, which the supplier can throw.supplier
- The long supplier to invoke.public static <E extends Throwable> short getAsShort(FailableShortSupplier<E> supplier)
E
- The type of checked exception, which the supplier can throw.supplier
- The short supplier to invoke.public static RuntimeException rethrow(Throwable throwable)
Rethrows a Throwable
as an unchecked exception. If the argument is already unchecked, namely a
RuntimeException
or Error
then the argument will be rethrown without modification. If the
exception is IOException
then it will be wrapped into a UncheckedIOException
. In every other
cases the exception will be wrapped into a UndeclaredThrowableException
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.
throwable
- The throwable to rethrow ossibly wrapped into an unchecked exceptionpublic static <E extends Throwable> void run(FailableRunnable<E> runnable)
RuntimeException
.E
- the type of checked exception the runnable may throwrunnable
- The runnable to runpublic static <E> Streams.FailableStream<E> stream(Collection<E> collection)
Streams.FailableStream
. The Streams.FailableStream
consists of the
collections elements. Shortcut for
Functions.stream(collection.stream());
E
- The collections element type. (In turn, the result streams element type.)collection
- The collection, which is being converted into a Streams.FailableStream
.Streams.FailableStream
.public static <T> Streams.FailableStream<T> stream(Stream<T> stream)
Streams.FailableStream
. The Streams.FailableStream
consists of the same
elements, than the input stream. However, failable lambdas, like FailablePredicate
,
FailableFunction
, and FailableConsumer
may be applied, rather than Predicate
,
Function
, Consumer
, etc.T
- The streams element type.stream
- The stream, which is being converted into a Streams.FailableStream
.Streams.FailableStream
.public static <T,U,E extends Throwable> boolean test(FailableBiPredicate<T,U,E> predicate, T object1, U object2)
RuntimeException
.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 throwpredicate
- the predicate to testobject1
- the first input to test by predicate
object2
- the second input to test by predicate
public static <T,E extends Throwable> boolean test(FailablePredicate<T,E> predicate, T object)
RuntimeException
.T
- the type of argument the predicate testsE
- the type of checked exception the predicate may throwpredicate
- the predicate to testobject
- the input to test by predicate
@SafeVarargs public static void tryWithResources(FailableRunnable<? extends Throwable> action, FailableConsumer<Throwable,? extends Throwable> errorHandler, FailableRunnable<? extends Throwable>... resources)
AutoCloseable
interface. The method executes the action
. The method guarantees, that all
the resources
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 (AKA
Throwable
is rethrown. Example use:
final FileInputStream fis = new FileInputStream("my.file"); Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());
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.tryWithResources(FailableRunnable, FailableRunnable...)
@SafeVarargs public static void tryWithResources(FailableRunnable<? extends Throwable> action, FailableRunnable<? extends Throwable>... resources)
AutoCloseable
interface. The method executes the action
. The method guarantees, that all
the resources
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 (AKA
Throwable
is rethrown. Example use:
final FileInputStream fis = new FileInputStream("my.file"); Functions.tryWithResources(useInputStream(fis), () -> fis.close());
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.tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
Copyright © 2001–2021 The Apache Software Foundation. All rights reserved.