Package org.apache.commons.lang3
Class Functions
java.lang.Object
org.apache.commons.lang3.Functions
Deprecated.
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 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.
- Since:
- 3.9
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic interfaceFunctions.FailableBiConsumer<O1,O2, T extends Throwable> Deprecated.UseFailableBiConsumer.static interfaceDeprecated.UseFailableBiFunction.static interfaceFunctions.FailableBiPredicate<O1,O2, T extends Throwable> Deprecated.UseFailableBiPredicate.static interfaceFunctions.FailableCallable<R,T extends Throwable> Deprecated.UseFailableCallable.static interfaceFunctions.FailableConsumer<O,T extends Throwable> Deprecated.UseFailableConsumer.static interfaceFunctions.FailableFunction<I,R, T extends Throwable> Deprecated.UseFailableFunction.static interfaceFunctions.FailablePredicate<I,T extends Throwable> Deprecated.UseFailablePredicate.static interfaceFunctions.FailableRunnable<T extends Throwable>Deprecated.UseFailableRunnable.static interfaceFunctions.FailableSupplier<R,T extends Throwable> Deprecated.UseFailableSupplier.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic <O1,O2, T extends Throwable> 
 voidaccept(Functions.FailableBiConsumer<O1, O2, T> consumer, O1 object1, O2 object2) Deprecated.Consumes a consumer and rethrows any exception as aRuntimeException.static <O,T extends Throwable> 
 voidaccept(Functions.FailableConsumer<O, T> consumer, O object) Deprecated.Consumes a consumer and rethrows any exception as aRuntimeException.static <O1,O2, O, T extends Throwable> 
 Oapply(Functions.FailableBiFunction<O1, O2, O, T> function, O1 input1, O2 input2) Deprecated.Applies a function and rethrows any exception as aRuntimeException.static <I,O, T extends Throwable> 
 Oapply(Functions.FailableFunction<I, O, T> function, I input) Deprecated.Applies a function and rethrows any exception as aRuntimeException.static <O1,O2> BiConsumer<O1, O2> asBiConsumer(Functions.FailableBiConsumer<O1, O2, ?> consumer) Deprecated.Converts the givenFunctions.FailableBiConsumerinto a standardBiConsumer.static <O1,O2, O> BiFunction<O1, O2, O> asBiFunction(Functions.FailableBiFunction<O1, O2, O, ?> function) Deprecated.Converts the givenFunctions.FailableBiFunctioninto a standardBiFunction.static <O1,O2> BiPredicate<O1, O2> asBiPredicate(Functions.FailableBiPredicate<O1, O2, ?> predicate) Deprecated.Converts the givenFunctions.FailableBiPredicateinto a standardBiPredicate.static <O> Callable<O>asCallable(Functions.FailableCallable<O, ?> callable) Deprecated.Converts the givenFunctions.FailableCallableinto a standardCallable.static <I> Consumer<I>asConsumer(Functions.FailableConsumer<I, ?> consumer) Deprecated.Converts the givenFunctions.FailableConsumerinto a standardConsumer.static <I,O> Function<I, O> asFunction(Functions.FailableFunction<I, O, ?> function) Deprecated.Converts the givenFunctions.FailableFunctioninto a standardFunction.static <I> Predicate<I>asPredicate(Functions.FailablePredicate<I, ?> predicate) Deprecated.Converts the givenFunctions.FailablePredicateinto a standardPredicate.static RunnableasRunnable(Functions.FailableRunnable<?> runnable) Deprecated.Converts the givenFunctions.FailableRunnableinto a standardRunnable.static <O> Supplier<O>asSupplier(Functions.FailableSupplier<O, ?> supplier) Deprecated.Converts the givenFunctions.FailableSupplierinto a standardSupplier.static <O,T extends Throwable> 
 Ocall(Functions.FailableCallable<O, T> callable) Deprecated.Calls a callable and rethrows any exception as aRuntimeException.static <O,T extends Throwable> 
 Oget(Functions.FailableSupplier<O, T> supplier) Deprecated.Invokes a supplier, and returns the result.static RuntimeExceptionDeprecated.Rethrows aThrowableas an unchecked exception.static <T extends Throwable>
 voidrun(Functions.FailableRunnable<T> runnable) Deprecated.Runs a runnable and rethrows any exception as aRuntimeException.static <O> Streams.FailableStream<O>stream(Collection<O> collection) Deprecated.Converts the given collection into aStreams.FailableStream.static <O> Streams.FailableStream<O>Deprecated.Converts the given stream into aStreams.FailableStream.static <O1,O2, T extends Throwable> 
 booleantest(Functions.FailableBiPredicate<O1, O2, T> predicate, O1 object1, O2 object2) Deprecated.Tests a predicate and rethrows any exception as aRuntimeException.static <O,T extends Throwable> 
 booleantest(Functions.FailablePredicate<O, T> predicate, O object) Deprecated.Tests a predicate and rethrows any exception as aRuntimeException.static voidtryWithResources(Functions.FailableRunnable<? extends Throwable> action, Functions.FailableConsumer<Throwable, ? extends Throwable> errorHandler, Functions.FailableRunnable<? extends Throwable>... resources) Deprecated.A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseableinterface.static voidtryWithResources(Functions.FailableRunnable<? extends Throwable> action, Functions.FailableRunnable<? extends Throwable>... resources) Deprecated.A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseableinterface.
- 
Constructor Details- 
Functionspublic Functions()Deprecated.Constructs a new instance.
 
- 
- 
Method Details- 
acceptpublic static <O1,O2, void acceptT extends Throwable> (Functions.FailableBiConsumer<O1, O2, T> consumer, O1 object1, O2 object2) Deprecated.Consumes a consumer and rethrows any exception as aRuntimeException.- Type Parameters:
- O1- the type of the first argument the consumer accepts
- O2- the type of the second argument the consumer accepts
- T- 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
 
- 
acceptpublic static <O,T extends Throwable> void accept(Functions.FailableConsumer<O, T> consumer, O object) Deprecated.Consumes a consumer and rethrows any exception as aRuntimeException.- Type Parameters:
- O- the type the consumer accepts
- T- the type of checked exception the consumer may throw
- Parameters:
- consumer- the consumer to consume
- object- the object to consume by- consumer
 
- 
applypublic static <O1,O2, O applyO, T extends Throwable> (Functions.FailableBiFunction<O1, O2, O, T> function, O1 input1, O2 input2) Deprecated.Applies a function and rethrows any exception as aRuntimeException.- Type Parameters:
- O1- the type of the first argument the function accepts
- O2- the type of the second argument the function accepts
- O- the return type of the function
- T- 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
 
- 
applypublic static <I,O, O applyT extends Throwable> (Functions.FailableFunction<I, O, T> function, I input) Deprecated.Applies a function and rethrows any exception as aRuntimeException.- Type Parameters:
- I- the type of the argument the function accepts
- O- the return type of the function
- T- 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
 
- 
asBiConsumerpublic static <O1,O2> BiConsumer<O1,O2> asBiConsumer(Functions.FailableBiConsumer<O1, O2, ?> consumer) Deprecated.Converts the givenFunctions.FailableBiConsumerinto a standardBiConsumer.- Type Parameters:
- O1- the type of the first argument of the consumers
- O2- the type of the second argument of the consumers
- Parameters:
- consumer- a failable- BiConsumer
- Returns:
- a standard BiConsumer
- Since:
- 3.10
 
- 
asBiFunctionpublic static <O1,O2, BiFunction<O1,O> O2, asBiFunctionO> (Functions.FailableBiFunction<O1, O2, O, ?> function) Deprecated.Converts the givenFunctions.FailableBiFunctioninto a standardBiFunction.- Type Parameters:
- O1- the type of the first argument of the input of the functions
- O2- the type of the second argument of the input of the functions
- O- the type of the output of the functions
- Parameters:
- function- a- Functions.FailableBiFunction
- Returns:
- a standard BiFunction
- Since:
- 3.10
 
- 
asBiPredicatepublic static <O1,O2> BiPredicate<O1,O2> asBiPredicate(Functions.FailableBiPredicate<O1, O2, ?> predicate) Deprecated.Converts the givenFunctions.FailableBiPredicateinto a standardBiPredicate.- Type Parameters:
- O1- the type of the first argument used by the predicates
- O2- the type of the second argument used by the predicates
- Parameters:
- predicate- a- Functions.FailableBiPredicate
- Returns:
- a standard BiPredicate
- Since:
- 3.10
 
- 
asCallableDeprecated.Converts the givenFunctions.FailableCallableinto a standardCallable.- Type Parameters:
- O- the type used by the callables
- Parameters:
- callable- a- Functions.FailableCallable
- Returns:
- a standard Callable
- Since:
- 3.10
 
- 
asConsumerDeprecated.Converts the givenFunctions.FailableConsumerinto a standardConsumer.- Type Parameters:
- I- the type used by the consumers
- Parameters:
- consumer- a- Functions.FailableConsumer
- Returns:
- a standard Consumer
- Since:
- 3.10
 
- 
asFunctionDeprecated.Converts the givenFunctions.FailableFunctioninto a standardFunction.- Type Parameters:
- I- the type of the input of the functions
- O- the type of the output of the functions
- Parameters:
- function- a {code FailableFunction}
- Returns:
- a standard Function
- Since:
- 3.10
 
- 
asPredicateDeprecated.Converts the givenFunctions.FailablePredicateinto a standardPredicate.- Type Parameters:
- I- the type used by the predicates
- Parameters:
- predicate- a- Functions.FailablePredicate
- Returns:
- a standard Predicate
- Since:
- 3.10
 
- 
asRunnableDeprecated.Converts the givenFunctions.FailableRunnableinto a standardRunnable.- Parameters:
- runnable- a- Functions.FailableRunnable
- Returns:
- a standard Runnable
- Since:
- 3.10
 
- 
asSupplierDeprecated.Converts the givenFunctions.FailableSupplierinto a standardSupplier.- Type Parameters:
- O- the type supplied by the suppliers
- Parameters:
- supplier- a- Functions.FailableSupplier
- Returns:
- a standard Supplier
- Since:
- 3.10
 
- 
callDeprecated.Calls a callable and rethrows any exception as aRuntimeException.- Type Parameters:
- O- the return type of the callable
- T- the type of checked exception the callable may throw
- Parameters:
- callable- the callable to call
- Returns:
- the value returned from the callable
 
- 
getDeprecated.Invokes a supplier, and returns the result.- Type Parameters:
- O- The suppliers output type.
- T- 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
- Since:
- 3.10
 
- 
rethrowDeprecated.Rethrows 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.
 
- 
runDeprecated.Runs a runnable and rethrows any exception as aRuntimeException.- Type Parameters:
- T- the type of checked exception the runnable may throw
- Parameters:
- runnable- The runnable to run
 
- 
streamDeprecated.Converts the given collection into aStreams.FailableStream. TheStreams.FailableStreamconsists of the collections elements. Shortcut forFunctions.stream(collection.stream()); - Type Parameters:
- O- 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.
- Since:
- 3.10
 
- 
streamDeprecated.Converts the given stream into aStreams.FailableStream. TheStreams.FailableStreamconsists of the same elements, than the input stream. However, failable lambdas, likeFunctions.FailablePredicate,Functions.FailableFunction, andFunctions.FailableConsumermay be applied, rather thanPredicate,Function,Consumer, etc.- Type Parameters:
- O- The streams element type.
- Parameters:
- stream- The stream, which is being converted into a- Streams.FailableStream.
- Returns:
- The created Streams.FailableStream.
- Since:
- 3.10
 
- 
testpublic static <O1,O2, boolean testT extends Throwable> (Functions.FailableBiPredicate<O1, O2, T> predicate, O1 object1, O2 object2) Deprecated.Tests a predicate and rethrows any exception as aRuntimeException.- Type Parameters:
- O1- the type of the first argument the predicate tests
- O2- the type of the second argument the predicate tests
- T- 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
 
- 
testpublic static <O,T extends Throwable> boolean test(Functions.FailablePredicate<O, T> predicate, O object) Deprecated.Tests a predicate and rethrows any exception as aRuntimeException.- Type Parameters:
- O- the type of argument the predicate tests
- T- 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(Functions.FailableRunnable<? extends Throwable> action, Functions.FailableConsumer<Throwable, ? extends Throwable> errorHandler, Functions.FailableRunnable<? extends Throwable>... resources) Deprecated.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- Functions.FailableRunnable, which will be executed.
- See Also:
 
- 
tryWithResources@SafeVarargs public static void tryWithResources(Functions.FailableRunnable<? extends Throwable> action, Functions.FailableRunnable<? extends Throwable>... resources) Deprecated.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- Functions.FailableRunnable, which will be executed.
- See Also:
 
 
- 
Failable.