public class Functions
extends java.lang.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 Functions.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 | Class and Description |
---|---|
static interface |
Functions.FailableBiConsumer<O1,O2,T extends java.lang.Throwable> |
static interface |
Functions.FailableBiFunction<I1,I2,O,T extends java.lang.Throwable> |
static interface |
Functions.FailableBiPredicate<O1,O2,T extends java.lang.Throwable> |
static interface |
Functions.FailableCallable<O,T extends java.lang.Throwable> |
static interface |
Functions.FailableConsumer<O,T extends java.lang.Throwable> |
static interface |
Functions.FailableFunction<I,O,T extends java.lang.Throwable> |
static interface |
Functions.FailablePredicate<O,T extends java.lang.Throwable> |
static interface |
Functions.FailableRunnable<T extends java.lang.Throwable> |
Constructor and Description |
---|
Functions() |
Modifier and Type | Method and Description |
---|---|
static <O1,O2,T extends java.lang.Throwable> |
accept(Functions.FailableBiConsumer<O1,O2,T> pConsumer,
O1 pObject1,
O2 pObject2)
Consumes a consumer and rethrows any exception as a
RuntimeException . |
static <O,T extends java.lang.Throwable> |
accept(Functions.FailableConsumer<O,T> pConsumer,
O pObject)
Consumes a consumer and rethrows any exception as a
RuntimeException . |
static <I1,I2,O,T extends java.lang.Throwable> |
apply(Functions.FailableBiFunction<I1,I2,O,T> pFunction,
I1 pInput1,
I2 pInput2)
Applies a function and rethrows any exception as a
RuntimeException . |
static <I,O,T extends java.lang.Throwable> |
apply(Functions.FailableFunction<I,O,T> pFunction,
I pInput)
Applies a function and rethrows any exception as a
RuntimeException . |
static <O,T extends java.lang.Throwable> |
call(Functions.FailableCallable<O,T> pCallable)
Calls a callable and rethrows any exception as a
RuntimeException . |
static java.lang.RuntimeException |
rethrow(java.lang.Throwable pThrowable)
Rethrow a
Throwable as an unchecked exception. |
static <T extends java.lang.Throwable> |
run(Functions.FailableRunnable<T> pRunnable)
Runs a runnable and rethrows any exception as a
RuntimeException . |
static <O1,O2,T extends java.lang.Throwable> |
test(Functions.FailableBiPredicate<O1,O2,T> pPredicate,
O1 pObject1,
O2 pObject2)
Tests a predicate and rethrows any exception as a
RuntimeException . |
static <O,T extends java.lang.Throwable> |
test(Functions.FailablePredicate<O,T> pPredicate,
O pObject)
Tests a predicate and rethrows any exception as a
RuntimeException . |
static void |
tryWithResources(Functions.FailableRunnable<? extends java.lang.Throwable> pAction,
Functions.FailableConsumer<java.lang.Throwable,? extends java.lang.Throwable> pErrorHandler,
Functions.FailableRunnable<? extends java.lang.Throwable>... pResources)
A simple try-with-resources implementation, that can be used, if your
objects do not implement the
AutoCloseable interface. |
static void |
tryWithResources(Functions.FailableRunnable<? extends java.lang.Throwable> pAction,
Functions.FailableRunnable<? extends java.lang.Throwable>... pResources)
A simple try-with-resources implementation, that can be used, if your
objects do not implement the
AutoCloseable interface. |
public static <T extends java.lang.Throwable> void run(Functions.FailableRunnable<T> pRunnable)
RuntimeException
.T
- the type of checked exception the runnable may throwpRunnable
- The runnable to runpublic static <O,T extends java.lang.Throwable> O call(Functions.FailableCallable<O,T> pCallable)
RuntimeException
.O
- the return type of the callableT
- the type of checked exception the callable may throwpCallable
- the callable to callpublic static <O,T extends java.lang.Throwable> void accept(Functions.FailableConsumer<O,T> pConsumer, O pObject)
RuntimeException
.O
- the type the consumer acceptsT
- the type of checked exception the consumer may throwpConsumer
- the consumer to consumepObject
- the object to consume by pConsumer
public static <O1,O2,T extends java.lang.Throwable> void accept(Functions.FailableBiConsumer<O1,O2,T> pConsumer, O1 pObject1, O2 pObject2)
RuntimeException
.O1
- the type of the first argument the consumer acceptsO2
- the type of the second argument the consumer acceptsT
- the type of checked exception the consumer may throwpConsumer
- the consumer to consumepObject1
- the first object to consume by pConsumer
pObject2
- the second object to consume by pConsumer
public static <I,O,T extends java.lang.Throwable> O apply(Functions.FailableFunction<I,O,T> pFunction, I pInput)
RuntimeException
.I
- the type of the argument the function acceptsO
- the return type of the functionT
- the type of checked exception the function may throwpFunction
- the function to applypInput
- the input to apply pFunction
onpublic static <I1,I2,O,T extends java.lang.Throwable> O apply(Functions.FailableBiFunction<I1,I2,O,T> pFunction, I1 pInput1, I2 pInput2)
RuntimeException
.I1
- the type of the first argument the function acceptsI2
- the type of the second argument the function acceptsO
- the return type of the functionT
- the type of checked exception the function may throwpFunction
- the function to applypInput1
- the first input to apply pFunction
onpInput2
- the second input to apply pFunction
onpublic static <O,T extends java.lang.Throwable> boolean test(Functions.FailablePredicate<O,T> pPredicate, O pObject)
RuntimeException
.O
- the type of argument the predicate testsT
- the type of checked exception the predicate may throwpPredicate
- the predicate to testpObject
- the input to test by pPredicate
public static <O1,O2,T extends java.lang.Throwable> boolean test(Functions.FailableBiPredicate<O1,O2,T> pPredicate, O1 pObject1, O2 pObject2)
RuntimeException
.O1
- the type of the first argument the predicate testsO2
- the type of the second argument the predicate testsT
- the type of checked exception the predicate may throwpPredicate
- the predicate to testpObject1
- the first input to test by pPredicate
pObject2
- the second input to test by pPredicate
@SafeVarargs public static void tryWithResources(Functions.FailableRunnable<? extends java.lang.Throwable> pAction, Functions.FailableConsumer<java.lang.Throwable,? extends java.lang.Throwable> pErrorHandler, Functions.FailableRunnable<? extends java.lang.Throwable>... pResources)
AutoCloseable
interface. The method
executes the pAction
. The method guarantees, that all
the pResources
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());
pAction
- The action to execute. This object will always
be invoked.pErrorHandler
- An optional error handler, which will be invoked finally,
if any error occurred. The error handler will receive the first
error, aka Throwable
.pResources
- The resource actions to execute. All resource
actions will be invoked, in the given order. A resource action is an
instance of Functions.FailableRunnable
, which will be executed.tryWithResources(FailableRunnable, FailableRunnable...)
@SafeVarargs public static void tryWithResources(Functions.FailableRunnable<? extends java.lang.Throwable> pAction, Functions.FailableRunnable<? extends java.lang.Throwable>... pResources)
AutoCloseable
interface. The method
executes the pAction
. The method guarantees, that all
the pResources
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());
pAction
- The action to execute. This object will always
be invoked.pResources
- The resource actions to execute. All resource
actions will be invoked, in the given order. A resource action is an
instance of Functions.FailableRunnable
, which will be executed.tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
public static java.lang.RuntimeException rethrow(java.lang.Throwable pThrowable)
Throwable
as an unchecked exception.pThrowable
- The throwable to rethrow"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"