Top Description Inners Constructors Methods
org.apache.commons.lang3

public Class Functions

extends Object
Class Inheritance
Annotations
@Deprecated
Imports
java.io.IOException, .UncheckedIOException, java.lang.reflect.UndeclaredThrowableException, java.util.Arrays, .Collection, .Objects, java.util.concurrent.Callable, java.util.function.BiConsumer, .BiFunction, .BiPredicate, .Consumer, .Function, .Predicate, .Supplier, java.util.stream.Stream, org.apache.commons.lang3.Streams.FailableStream, org.apache.commons.lang3.exception.ExceptionUtils, org.apache.commons.lang3.function.Failable, .FailableBooleanSupplier

Deprecated

Use org.apache.commons.lang3.function.Failable.

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 and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Functions.FailableBiConsumer<
Consumed type 1.
O1
,
Consumed type 2.
O2
,
Thrown exception.
T extends Throwable
>

A functional interface like BiConsumer that declares a Throwable.
public static interface
Functions.FailableBiFunction<
Input type 1.
O1
,
Input type 2.
O2
,
Return type.
R
,
Thrown exception.
T extends Throwable
>

A functional interface like BiFunction that declares a Throwable.
public static interface
Functions.FailableBiPredicate<
Predicate type 1.
O1
,
Predicate type 2.
O2
,
Thrown exception.
T extends Throwable
>

A functional interface like BiPredicate that declares a Throwable.
public static interface
Functions.FailableCallable<
Return type.
R
,
Thrown exception.
T extends Throwable
>

A functional interface like java.util.concurrent.Callable that declares a Throwable.
public static interface
Functions.FailableConsumer<
Consumed type 1.
O
,
Thrown exception.
T extends Throwable
>

A functional interface like Consumer that declares a Throwable.
public static interface
Functions.FailableFunction<
Input type 1.
I
,
Return type.
R
,
Thrown exception.
T extends Throwable
>

A functional interface like Function that declares a Throwable.
public static interface
Functions.FailablePredicate<
Predicate type 1.
I
,
Thrown exception.
T extends Throwable
>

A functional interface like Predicate that declares a Throwable.
public static interface
Functions.FailableRunnable<
Thrown exception.
T extends Throwable
>

A functional interface like Runnable that declares a Throwable.
public static interface
Functions.FailableSupplier<
Return type.
R
,
Thrown exception.
T extends Throwable
>

A functional interface like Supplier that declares a Throwable.

Constructor Summary

AccessConstructor and Description
public
Functions()

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the first argument the consumer accepts
O1
,
the type of the second argument the consumer accepts
O2
,
the type of checked exception the consumer may throw
T extends Throwable
>
void
accept(final Functions.FailableBiConsumer<O1, O2, T>
the consumer to consume
consumer
,
final O1
the first object to consume by consumer
object1
,
final O2
the second object to consume by consumer
object2
)
References Deprecated Functions.FailableBiConsumer is deprecated or references (maybe indirectly) at least one deprecated element.

Consumes a consumer and rethrows any exception as a RuntimeException.

public static <
the type the consumer accepts
O
,
the type of checked exception the consumer may throw
T extends Throwable
>
void
accept(final Functions.FailableConsumer<O, T>
the consumer to consume
consumer
,
final O
the object to consume by consumer
object
)
References Deprecated Functions.FailableConsumer is deprecated or references (maybe indirectly) at least one deprecated element.

Consumes a consumer and rethrows any exception as a RuntimeException.

public static <
the type of the first argument the function accepts
O1
,
the type of the second argument the function accepts
O2
,
the return type of the function
O
,
the type of checked exception the function may throw
T extends Throwable
>
O

Returns:

the value returned from the function
apply
(final Functions.FailableBiFunction<O1, O2, O, T>
the function to apply
function
,
final O1
the first input to apply function on
input1
,
final O2
the second input to apply function on
input2
)
References Deprecated Functions.FailableBiFunction is deprecated or references (maybe indirectly) at least one deprecated element.

Applies a function and rethrows any exception as a RuntimeException.

public static <
the type of the argument the function accepts
I
,
the return type of the function
O
,
the type of checked exception the function may throw
T extends Throwable
>
O

Returns:

the value returned from the function
apply
(final Functions.FailableFunction<I, O, T>
the function to apply
function
,
final I
the input to apply function on
input
)
References Deprecated Functions.FailableFunction is deprecated or references (maybe indirectly) at least one deprecated element.

Applies a function and rethrows any exception as a RuntimeException.

public static <
the type of the first argument of the consumers
O1
,
the type of the second argument of the consumers
O2
>
BiConsumer<O1, O2>

Returns:

a standard BiConsumer
asBiConsumer
(final Functions.FailableBiConsumer<O1, O2, ?>
a failable BiConsumer
consumer
)
References Deprecated Functions.FailableBiConsumer is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailableBiConsumer into a standard BiConsumer.

public static <
the type of the first argument of the input of the functions
O1
,
the type of the second argument of the input of the functions
O2
,
the type of the output of the functions
O
>
BiFunction<O1, O2, O>

Returns:

a standard BiFunction
asBiFunction
(final Functions.FailableBiFunction<O1, O2, O, ?> function)
References Deprecated Functions.FailableBiFunction is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailableBiFunction into a standard BiFunction.

public static <
the type of the first argument used by the predicates
O1
,
the type of the second argument used by the predicates
O2
>
BiPredicate<O1, O2>

Returns:

a standard BiPredicate
asBiPredicate
(final Functions.FailableBiPredicate<O1, O2, ?> predicate)
References Deprecated Functions.FailableBiPredicate is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailableBiPredicate into a standard BiPredicate.

public static <
the type used by the callables
O
>
Callable<O>

Returns:

a standard Callable
asCallable
(final Functions.FailableCallable<O, ?> callable)
References Deprecated Functions.FailableCallable is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailableCallable into a standard Callable.

public static <
the type used by the consumers
I
>
Consumer<I>

Returns:

a standard Consumer
asConsumer
(final Functions.FailableConsumer<I, ?> consumer)
References Deprecated Functions.FailableConsumer is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailableConsumer into a standard Consumer.

public static <
the type of the input of the functions
I
,
the type of the output of the functions
O
>
Function<I, O>

Returns:

a standard Function
asFunction
(final Functions.FailableFunction<I, O, ?>
a {code FailableFunction}
function
)
References Deprecated Functions.FailableFunction is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailableFunction into a standard Function.

public static <
the type used by the predicates
I
>
Predicate<I>

Returns:

a standard Predicate
asPredicate
(final Functions.FailablePredicate<I, ?> predicate)
References Deprecated Functions.FailablePredicate is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailablePredicate into a standard Predicate.

public static Runnable

Returns:

a standard Runnable
asRunnable
(final Functions.FailableRunnable<?> runnable)
References Deprecated Functions.FailableRunnable is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailableRunnable into a standard Runnable.

public static <
the type supplied by the suppliers
O
>
Supplier<O>

Returns:

a standard Supplier
asSupplier
(final Functions.FailableSupplier<O, ?> supplier)
References Deprecated Functions.FailableSupplier is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given FailableSupplier into a standard Supplier.

public static <
the return type of the callable
O
,
the type of checked exception the callable may throw
T extends Throwable
>
O

Returns:

the value returned from the callable
call
(final Functions.FailableCallable<O, T>
the callable to call
callable
)
References Deprecated Functions.FailableCallable is deprecated or references (maybe indirectly) at least one deprecated element.

Calls a callable and rethrows any exception as a RuntimeException.

public static <
The suppliers output type.
O
,
The type of checked exception, which the supplier can throw.
T extends Throwable
>
O

Returns:

The object, which has been created by the supplier
get
(final Functions.FailableSupplier<O, T>
The supplier to invoke.
supplier
)
References Deprecated Functions.FailableSupplier is deprecated or references (maybe indirectly) at least one deprecated element.

Invokes a supplier, and returns the result.

private static <
The type of checked exception, which the supplier can throw.
T extends Throwable
>
boolean

Returns:

The boolean, which has been created by the supplier
getAsBoolean
(final FailableBooleanSupplier<T>
The boolean supplier to invoke.
supplier
)

Invokes a boolean supplier, and returns the result.

public static RuntimeException

Returns:

Never returns anything, this method never terminates normally.
rethrow
(final Throwable
The throwable to rethrow possibly wrapped into an unchecked exception
throwable
)

Rethrows a Throwable as an unchecked exception.

public static <
the type of checked exception the runnable may throw
T extends Throwable
>
void
run(final Functions.FailableRunnable<T>
The runnable to run
runnable
)
References Deprecated Functions.FailableRunnable is deprecated or references (maybe indirectly) at least one deprecated element.

Runs a runnable and rethrows any exception as a RuntimeException.

public static <
The collections element type. (In turn, the result streams element type.)
O
>
Streams.FailableStream<O>

Returns:

The created FailableStream.
stream
(final Collection<O>
The collection, which is being converted into a FailableStream.
collection
)
References Deprecated Streams.FailableStream is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given collection into a FailableStream.

public static <
The streams element type.
O
>
Streams.FailableStream<O>

Returns:

The created FailableStream.
stream
(final Stream<O>
The stream, which is being converted into a FailableStream.
stream
)
References Deprecated Streams.FailableStream is deprecated or references (maybe indirectly) at least one deprecated element.

Converts the given stream into a FailableStream.

public static <
the type of the first argument the predicate tests
O1
,
the type of the second argument the predicate tests
O2
,
the type of checked exception the predicate may throw
T extends Throwable
>
boolean

Returns:

the boolean value returned by the predicate
test
(final Functions.FailableBiPredicate<O1, O2, T>
the predicate to test
predicate
,
final O1
the first input to test by predicate
object1
,
final O2
the second input to test by predicate
object2
)
References Deprecated Functions.FailableBiPredicate is deprecated or references (maybe indirectly) at least one deprecated element.

Tests a predicate and rethrows any exception as a RuntimeException.

public static <
the type of argument the predicate tests
O
,
the type of checked exception the predicate may throw
T extends Throwable
>
boolean

Returns:

the boolean value returned by the predicate
test
(final Functions.FailablePredicate<O, T>
the predicate to test
predicate
,
final O
the input to test by predicate
object
)
References Deprecated Functions.FailablePredicate is deprecated or references (maybe indirectly) at least one deprecated element.

Tests a predicate and rethrows any exception as a RuntimeException.

public static void
tryWithResources(final Functions.FailableRunnable<? extends Throwable>
The action to execute. This object will always be invoked.
action
,
final Functions.FailableConsumer<Throwable, ? extends Throwable>
An optional error handler, which will be invoked finally, if any error occurred. The error handler will receive the first error, AKA Throwable.
errorHandler
,
final Functions.FailableRunnable<? extends Throwable>...
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.
resources
)
References Deprecated Functions.FailableRunnable and Functions.FailableConsumer are deprecated or reference (maybe indirectly) at least one deprecated element.

A simple try-with-resources implementation, that can be used, if your objects do not implement the AutoCloseable interface.

public static void
tryWithResources(final Functions.FailableRunnable<? extends Throwable>
The action to execute. This object will always be invoked.
action
,
final Functions.FailableRunnable<? extends Throwable>...
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.
resources
)
References Deprecated Functions.FailableRunnable is deprecated or references (maybe indirectly) at least one deprecated element.

A simple try-with-resources implementation, that can be used, if your objects do not implement the AutoCloseable interface.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait