Class Functions
- java.lang.Object
-
- org.apache.commons.lang3.Functions
-
@Deprecated public class Functions extends java.lang.Object
Deprecated.UseFailable
.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:{ @code Consumer
consumer = m -> { try { m.invoke(o, args); } catch (Throwable t) { throw Functions.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.9
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Functions.FailableBiConsumer<O1,O2,T extends java.lang.Throwable>
Deprecated.UseFailableBiConsumer
.static interface
Functions.FailableBiFunction<O1,O2,R,T extends java.lang.Throwable>
Deprecated.UseFailableBiFunction
.static interface
Functions.FailableBiPredicate<O1,O2,T extends java.lang.Throwable>
Deprecated.UseFailableBiPredicate
.static interface
Functions.FailableCallable<R,T extends java.lang.Throwable>
Deprecated.UseFailableCallable
.static interface
Functions.FailableConsumer<O,T extends java.lang.Throwable>
Deprecated.UseFailableConsumer
.static interface
Functions.FailableFunction<I,R,T extends java.lang.Throwable>
Deprecated.UseFailableFunction
.static interface
Functions.FailablePredicate<I,T extends java.lang.Throwable>
Deprecated.UseFailablePredicate
.static interface
Functions.FailableRunnable<T extends java.lang.Throwable>
Deprecated.UseFailableRunnable
.static interface
Functions.FailableSupplier<R,T extends java.lang.Throwable>
Deprecated.UseFailableSupplier
.
-
Constructor Summary
Constructors Constructor Description Functions()
Deprecated.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <O1,O2,T extends java.lang.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 java.lang.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 java.lang.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 java.lang.Throwable>
Oapply(Functions.FailableFunction<I,O,T> function, I input)
Deprecated.Applies a function and rethrows any exception as aRuntimeException
.static <O1,O2>
java.util.function.BiConsumer<O1,O2>asBiConsumer(Functions.FailableBiConsumer<O1,O2,?> consumer)
Deprecated.Converts the givenFunctions.FailableBiConsumer
into a standardBiConsumer
.static <O1,O2,O>
java.util.function.BiFunction<O1,O2,O>asBiFunction(Functions.FailableBiFunction<O1,O2,O,?> function)
Deprecated.Converts the givenFunctions.FailableBiFunction
into a standardBiFunction
.static <O1,O2>
java.util.function.BiPredicate<O1,O2>asBiPredicate(Functions.FailableBiPredicate<O1,O2,?> predicate)
Deprecated.Converts the givenFunctions.FailableBiPredicate
into a standardBiPredicate
.static <O> java.util.concurrent.Callable<O>
asCallable(Functions.FailableCallable<O,?> callable)
Deprecated.Converts the givenFunctions.FailableCallable
into a standardCallable
.static <I> java.util.function.Consumer<I>
asConsumer(Functions.FailableConsumer<I,?> consumer)
Deprecated.Converts the givenFunctions.FailableConsumer
into a standardConsumer
.static <I,O>
java.util.function.Function<I,O>asFunction(Functions.FailableFunction<I,O,?> function)
Deprecated.Converts the givenFunctions.FailableFunction
into a standardFunction
.static <I> java.util.function.Predicate<I>
asPredicate(Functions.FailablePredicate<I,?> predicate)
Deprecated.Converts the givenFunctions.FailablePredicate
into a standardPredicate
.static java.lang.Runnable
asRunnable(Functions.FailableRunnable<?> runnable)
Deprecated.Converts the givenFunctions.FailableRunnable
into a standardRunnable
.static <O> java.util.function.Supplier<O>
asSupplier(Functions.FailableSupplier<O,?> supplier)
Deprecated.Converts the givenFunctions.FailableSupplier
into a standardSupplier
.static <O,T extends java.lang.Throwable>
Ocall(Functions.FailableCallable<O,T> callable)
Deprecated.Calls a callable and rethrows any exception as aRuntimeException
.static <O,T extends java.lang.Throwable>
Oget(Functions.FailableSupplier<O,T> supplier)
Deprecated.Invokes a supplier, and returns the result.static java.lang.RuntimeException
rethrow(java.lang.Throwable throwable)
Deprecated.Rethrows aThrowable
as an unchecked exception.static <T extends java.lang.Throwable>
voidrun(Functions.FailableRunnable<T> runnable)
Deprecated.Runs a runnable and rethrows any exception as aRuntimeException
.static <O> Streams.FailableStream<O>
stream(java.util.Collection<O> collection)
Deprecated.Converts the given collection into aStreams.FailableStream
.static <O> Streams.FailableStream<O>
stream(java.util.stream.Stream<O> stream)
Deprecated.Converts the given stream into aStreams.FailableStream
.static <O1,O2,T extends java.lang.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 java.lang.Throwable>
booleantest(Functions.FailablePredicate<O,T> predicate, O object)
Deprecated.Tests a predicate and rethrows any exception as aRuntimeException
.static void
tryWithResources(Functions.FailableRunnable<? extends java.lang.Throwable> action, Functions.FailableConsumer<java.lang.Throwable,? extends java.lang.Throwable> errorHandler, Functions.FailableRunnable<? extends java.lang.Throwable>... resources)
Deprecated.A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseable
interface.static void
tryWithResources(Functions.FailableRunnable<? extends java.lang.Throwable> action, Functions.FailableRunnable<? extends java.lang.Throwable>... resources)
Deprecated.A simple try-with-resources implementation, that can be used, if your objects do not implement theAutoCloseable
interface.
-
-
-
Method Detail
-
accept
public static <O1,O2,T extends java.lang.Throwable> void accept(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 acceptsO2
- the type of the second argument the consumer acceptsT
- 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 <O,T extends java.lang.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 acceptsT
- the type of checked exception the consumer may throw- Parameters:
consumer
- the consumer to consumeobject
- the object to consume byconsumer
-
apply
public static <O1,O2,O,T extends java.lang.Throwable> O apply(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 acceptsO2
- 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:
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 <I,O,T extends java.lang.Throwable> O apply(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 acceptsO
- the return type of the functionT
- 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
-
asBiConsumer
public static <O1,O2> java.util.function.BiConsumer<O1,O2> asBiConsumer(Functions.FailableBiConsumer<O1,O2,?> consumer)
Deprecated.Converts the givenFunctions.FailableBiConsumer
into a standardBiConsumer
.- Type Parameters:
O1
- the type of the first argument of the consumersO2
- the type of the second argument of the consumers- Parameters:
consumer
- a failableBiConsumer
- Returns:
- a standard
BiConsumer
- Since:
- 3.10
-
asBiFunction
public static <O1,O2,O> java.util.function.BiFunction<O1,O2,O> asBiFunction(Functions.FailableBiFunction<O1,O2,O,?> function)
Deprecated.Converts the givenFunctions.FailableBiFunction
into a standardBiFunction
.- Type Parameters:
O1
- the type of the first argument of the input of the functionsO2
- the type of the second argument of the input of the functionsO
- the type of the output of the functions- Parameters:
function
- aFailableBiFunction
- Returns:
- a standard
BiFunction
- Since:
- 3.10
-
asBiPredicate
public static <O1,O2> java.util.function.BiPredicate<O1,O2> asBiPredicate(Functions.FailableBiPredicate<O1,O2,?> predicate)
Deprecated.Converts the givenFunctions.FailableBiPredicate
into a standardBiPredicate
.- Type Parameters:
O1
- the type of the first argument used by the predicatesO2
- the type of the second argument used by the predicates- Parameters:
predicate
- aFailableBiPredicate
- Returns:
- a standard
BiPredicate
- Since:
- 3.10
-
asCallable
public static <O> java.util.concurrent.Callable<O> asCallable(Functions.FailableCallable<O,?> callable)
Deprecated.Converts the givenFunctions.FailableCallable
into a standardCallable
.- Type Parameters:
O
- the type used by the callables- Parameters:
callable
- aFailableCallable
- Returns:
- a standard
Callable
- Since:
- 3.10
-
asConsumer
public static <I> java.util.function.Consumer<I> asConsumer(Functions.FailableConsumer<I,?> consumer)
Deprecated.Converts the givenFunctions.FailableConsumer
into a standardConsumer
.- Type Parameters:
I
- the type used by the consumers- Parameters:
consumer
- aFailableConsumer
- Returns:
- a standard
Consumer
- Since:
- 3.10
-
asFunction
public static <I,O> java.util.function.Function<I,O> asFunction(Functions.FailableFunction<I,O,?> function)
Deprecated.Converts the givenFunctions.FailableFunction
into a standardFunction
.- Type Parameters:
I
- the type of the input of the functionsO
- the type of the output of the functions- Parameters:
function
- a {code FailableFunction}- Returns:
- a standard
Function
- Since:
- 3.10
-
asPredicate
public static <I> java.util.function.Predicate<I> asPredicate(Functions.FailablePredicate<I,?> predicate)
Deprecated.Converts the givenFunctions.FailablePredicate
into a standardPredicate
.- Type Parameters:
I
- the type used by the predicates- Parameters:
predicate
- aFailablePredicate
- Returns:
- a standard
Predicate
- Since:
- 3.10
-
asRunnable
public static java.lang.Runnable asRunnable(Functions.FailableRunnable<?> runnable)
Deprecated.Converts the givenFunctions.FailableRunnable
into a standardRunnable
.- Parameters:
runnable
- aFailableRunnable
- Returns:
- a standard
Runnable
- Since:
- 3.10
-
asSupplier
public static <O> java.util.function.Supplier<O> asSupplier(Functions.FailableSupplier<O,?> supplier)
Deprecated.Converts the givenFunctions.FailableSupplier
into a standardSupplier
.- Type Parameters:
O
- the type supplied by the suppliers- Parameters:
supplier
- aFailableSupplier
- Returns:
- a standard
Supplier
- Since:
- 3.10
-
call
public static <O,T extends java.lang.Throwable> O call(Functions.FailableCallable<O,T> callable)
Deprecated.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:
callable
- the callable to call- Returns:
- the value returned from the callable
-
get
public static <O,T extends java.lang.Throwable> O get(Functions.FailableSupplier<O,T> supplier)
Deprecated.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
-
rethrow
public static java.lang.RuntimeException rethrow(java.lang.Throwable throwable)
Deprecated.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 <T extends java.lang.Throwable> void run(Functions.FailableRunnable<T> runnable)
Deprecated.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
-
stream
public static <O> Streams.FailableStream<O> stream(java.util.Collection<O> collection)
Deprecated.Converts the given collection into aStreams.FailableStream
. TheStreams.FailableStream
consists 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 aStreams.FailableStream
.- Returns:
- The created
Streams.FailableStream
. - Since:
- 3.10
-
stream
public static <O> Streams.FailableStream<O> stream(java.util.stream.Stream<O> stream)
Deprecated.Converts the given stream into aStreams.FailableStream
. TheStreams.FailableStream
consists of the same elements, than the input stream. However, failable lambdas, likeFunctions.FailablePredicate
,Functions.FailableFunction
, andFunctions.FailableConsumer
may be applied, rather thanPredicate
,Function
,Consumer
, etc.- Type Parameters:
O
- The streams element type.- Parameters:
stream
- The stream, which is being converted into aStreams.FailableStream
.- Returns:
- The created
Streams.FailableStream
. - Since:
- 3.10
-
test
public static <O1,O2,T extends java.lang.Throwable> boolean test(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 testsO2
- the type of the second argument the predicate testsT
- 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 <O,T extends java.lang.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 testsT
- 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(Functions.FailableRunnable<? extends java.lang.Throwable> action, Functions.FailableConsumer<java.lang.Throwable,? extends java.lang.Throwable> errorHandler, Functions.FailableRunnable<? extends java.lang.Throwable>... resources)
Deprecated.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:{ @code 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 ofFunctions.FailableRunnable
, which will be executed.- See Also:
tryWithResources(FailableRunnable, FailableRunnable...)
-
tryWithResources
@SafeVarargs public static void tryWithResources(Functions.FailableRunnable<? extends java.lang.Throwable> action, Functions.FailableRunnable<? extends java.lang.Throwable>... resources)
Deprecated.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:{ @code 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 ofFunctions.FailableRunnable
, which will be executed.- See Also:
tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)
-
-