Package org.apache.commons.lang3
Class Functions
- java.lang.Object
-
- org.apache.commons.lang3.Functions
-
public class Functions extends java.lang.ObjectThis class provides utility functions, and classes for working with thejava.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, aka instances ofException. This enforces the use of constructs likeConsumer<java.lang.reflect.Method> consumer = (m) -> { try { m.invoke(o, args); } catch (Throwable t) { throw Functions.rethrow(t); } };By replacing aConsumer<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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceFunctions.FailableBiConsumer<O1,O2,T extends java.lang.Throwable>static interfaceFunctions.FailableBiFunction<I1,I2,O,T extends java.lang.Throwable>static interfaceFunctions.FailableBiPredicate<O1,O2,T extends java.lang.Throwable>static interfaceFunctions.FailableCallable<O,T extends java.lang.Throwable>static interfaceFunctions.FailableConsumer<O,T extends java.lang.Throwable>static interfaceFunctions.FailableFunction<I,O,T extends java.lang.Throwable>static interfaceFunctions.FailablePredicate<O,T extends java.lang.Throwable>static interfaceFunctions.FailableRunnable<T extends java.lang.Throwable>
-
Constructor Summary
Constructors Constructor Description Functions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <O1,O2,T extends java.lang.Throwable>
voidaccept(Functions.FailableBiConsumer<O1,O2,T> pConsumer, O1 pObject1, O2 pObject2)Consumes a consumer and rethrows any exception as aRuntimeException.static <O,T extends java.lang.Throwable>
voidaccept(Functions.FailableConsumer<O,T> pConsumer, O pObject)Consumes a consumer and rethrows any exception as aRuntimeException.static <I1,I2,O,T extends java.lang.Throwable>
Oapply(Functions.FailableBiFunction<I1,I2,O,T> pFunction, I1 pInput1, I2 pInput2)Applies a function and rethrows any exception as aRuntimeException.static <I,O,T extends java.lang.Throwable>
Oapply(Functions.FailableFunction<I,O,T> pFunction, I pInput)Applies a function and rethrows any exception as aRuntimeException.static <O,T extends java.lang.Throwable>
Ocall(Functions.FailableCallable<O,T> pCallable)Calls a callable and rethrows any exception as aRuntimeException.static java.lang.RuntimeExceptionrethrow(java.lang.Throwable pThrowable)Rethrow aThrowableas an unchecked exception.static <T extends java.lang.Throwable>
voidrun(Functions.FailableRunnable<T> pRunnable)Runs a runnable and rethrows any exception as aRuntimeException.static <O1,O2,T extends java.lang.Throwable>
booleantest(Functions.FailableBiPredicate<O1,O2,T> pPredicate, O1 pObject1, O2 pObject2)Tests a predicate and rethrows any exception as aRuntimeException.static <O,T extends java.lang.Throwable>
booleantest(Functions.FailablePredicate<O,T> pPredicate, O pObject)Tests a predicate and rethrows any exception as aRuntimeException.static voidtryWithResources(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 theAutoCloseableinterface.static voidtryWithResources(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 theAutoCloseableinterface.
-
-
-
Method Detail
-
run
public static <T extends java.lang.Throwable> void run(Functions.FailableRunnable<T> pRunnable)
Runs a runnable and rethrows any exception as aRuntimeException.- Type Parameters:
T- the type of checked exception the runnable may throw- Parameters:
pRunnable- The runnable to run
-
call
public static <O,T extends java.lang.Throwable> O call(Functions.FailableCallable<O,T> pCallable)
Calls a callable and rethrows any exception as aRuntimeException.- Type Parameters:
O- the return type of the callableT- the type of checked exception the callable may throw- Parameters:
pCallable- the callable to call- Returns:
- the value returned from the callable
-
accept
public static <O,T extends java.lang.Throwable> void accept(Functions.FailableConsumer<O,T> pConsumer, O pObject)
Consumes a consumer and rethrows any exception as aRuntimeException.- Type Parameters:
O- the type the consumer acceptsT- the type of checked exception the consumer may throw- Parameters:
pConsumer- the consumer to consumepObject- the object to consume bypConsumer
-
accept
public static <O1,O2,T extends java.lang.Throwable> void accept(Functions.FailableBiConsumer<O1,O2,T> pConsumer, O1 pObject1, O2 pObject2)
Consumes a consumer and rethrows any exception as aRuntimeException.- Type Parameters:
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 throw- Parameters:
pConsumer- the consumer to consumepObject1- the first object to consume bypConsumerpObject2- the second object to consume bypConsumer
-
apply
public static <I,O,T extends java.lang.Throwable> O apply(Functions.FailableFunction<I,O,T> pFunction, I pInput)
Applies a function and rethrows any exception as aRuntimeException.- Type Parameters:
I- the type of the argument the function acceptsO- the return type of the functionT- the type of checked exception the function may throw- Parameters:
pFunction- the function to applypInput- the input to applypFunctionon- Returns:
- the value returned from the function
-
apply
public static <I1,I2,O,T extends java.lang.Throwable> O apply(Functions.FailableBiFunction<I1,I2,O,T> pFunction, I1 pInput1, I2 pInput2)
Applies a function and rethrows any exception as aRuntimeException.- Type Parameters:
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 throw- Parameters:
pFunction- the function to applypInput1- the first input to applypFunctiononpInput2- the second input to applypFunctionon- Returns:
- the value returned from the function
-
test
public static <O,T extends java.lang.Throwable> boolean test(Functions.FailablePredicate<O,T> pPredicate, O pObject)
Tests a predicate and rethrows any exception as aRuntimeException.- Type Parameters:
O- the type of argument the predicate testsT- the type of checked exception the predicate may throw- Parameters:
pPredicate- the predicate to testpObject- the input to test bypPredicate- Returns:
- the boolean value returned by the predicate
-
test
public static <O1,O2,T extends java.lang.Throwable> boolean test(Functions.FailableBiPredicate<O1,O2,T> pPredicate, O1 pObject1, O2 pObject2)
Tests a predicate and rethrows any exception as aRuntimeException.- Type Parameters:
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 throw- Parameters:
pPredicate- the predicate to testpObject1- the first input to test bypPredicatepObject2- the second input to test bypPredicate- Returns:
- the boolean value returned by the predicate
-
tryWithResources
@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)
A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseableinterface. The method executes thepAction. The method guarantees, that all thepResourcesare 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 (akaThrowableis rethrown. Example use:final FileInputStream fis = new FileInputStream("my.file"); Functions.tryWithResources(useInputStream(fis), null, () -> fis.close());- Parameters:
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, akaThrowable.pResources- The resource actions to execute. All resource actions will be invoked, in the given order. A resource action is an instance ofFunctions.FailableRunnable, which will be executed.- See Also:
tryWithResources(FailableRunnable, FailableRunnable...)
-
tryWithResources
@SafeVarargs public 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 theAutoCloseableinterface. The method executes thepAction. The method guarantees, that all thepResourcesare 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 (akaThrowableis rethrown. Example use:final FileInputStream fis = new FileInputStream("my.file"); Functions.tryWithResources(useInputStream(fis), () -> fis.close());- Parameters:
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 ofFunctions.FailableRunnable, which will be executed.- See Also:
tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
-
rethrow
public static java.lang.RuntimeException rethrow(java.lang.Throwable pThrowable)
Rethrow aThrowableas an unchecked exception.- Parameters:
pThrowable- The throwable to rethrow- Returns:
- Never returns anything, this method never terminates normally
-
-