Failable

public class org.apache.commons.lang3.function.Failable[source]

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, like 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 Failable.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 first version.

Since:

3.11

Methods

accept(FailableBiConsumer, Object, Object)

Consumes a consumer and rethrows any exception as a RuntimeException.

accept(FailableConsumer, Object)

Consumes a consumer and rethrows any exception as a RuntimeException.

accept(FailableDoubleConsumer, double)

Consumes a consumer and rethrows any exception as a RuntimeException.

accept(FailableIntConsumer, int)

Consumes a consumer and rethrows any exception as a RuntimeException.

accept(FailableLongConsumer, long)

Consumes a consumer and rethrows any exception as a RuntimeException.

apply(FailableBiFunction, Object, Object)

Applies a function and rethrows any exception as a RuntimeException.

apply(FailableFunction, Object)

Applies a function and rethrows any exception as a RuntimeException.

applyAsDouble(FailableDoubleBinaryOperator, double, double)

Applies a function and rethrows any exception as a RuntimeException.

applyNonNull(Object, FailableFunction)

Applies a value to a function if the value isn’t null, otherwise the method returns null.

applyNonNull(Object, FailableFunction, FailableFunction)

Applies values to a chain of functions, where a null can short-circuit each step.

applyNonNull(Object, FailableFunction, FailableFunction, FailableFunction)

Applies values to a chain of functions, where a null can short-circuit each step.

asBiConsumer(FailableBiConsumer)

Converts the given FailableBiConsumer into a standard BiConsumer.

asBiFunction(FailableBiFunction)

Converts the given FailableBiFunction into a standard BiFunction.

asBiPredicate(FailableBiPredicate)

Converts the given FailableBiPredicate into a standard BiPredicate.

asCallable(FailableCallable)

Converts the given FailableCallable into a standard Callable.

asConsumer(FailableConsumer)

Converts the given FailableConsumer into a standard Consumer.

asFunction(FailableFunction)

Converts the given FailableFunction into a standard Function.

asPredicate(FailablePredicate)

Converts the given FailablePredicate into a standard Predicate.

asRunnable(FailableRunnable)

Converts the given FailableRunnable into a standard Runnable.

asSupplier(FailableSupplier)

Converts the given FailableSupplier into a standard Supplier.

call(FailableCallable)

Calls a callable and rethrows any exception as a RuntimeException.

get(FailableSupplier)

Gets the result of invoking the supplier.

getAsBoolean(FailableBooleanSupplier)

Gets the result of invoking the boolean supplier.

getAsDouble(FailableDoubleSupplier)

Gets the result of invoking the double supplier.

getAsInt(FailableIntSupplier)

Gets the result of invoking the int supplier.

getAsLong(FailableLongSupplier)

Gets the result of invoking the long supplier.

getAsShort(FailableShortSupplier)

Gets the result of invoking the short supplier.

rethrow(Throwable)

Rethrows a Throwable as an unchecked exception.

run(FailableRunnable)

Runs a runnable and rethrows any exception as a RuntimeException.

stream(Collection)

Converts the given collection into a FailableStream.

stream(Stream)

Converts the given stream into a FailableStream.

test(FailableBiPredicate, Object, Object)

Tests a predicate and rethrows any exception as a RuntimeException.

test(FailablePredicate, Object)

Tests a predicate and rethrows any exception as a RuntimeException.

tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable[])

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

tryWithResources(FailableRunnable, FailableRunnable[])

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

public static <T, U, E extends Throwable> void accept(FailableBiConsumer<T, U, E> consumer, T object1, U object2)[source]

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters:
  • consumer – The consumer to accept, may be null for a noop.

  • object1 – The first object to consume by consumer

  • object2 – The second object to consume by consumer

Type parameters:
  • <T> – The type of the first argument the consumer accepts

  • <U> – The type of the second argument the consumer accepts

  • <E> – The type of checked exception the consumer may throw

public static <T, E extends Throwable> void accept(FailableConsumer<T, E> consumer, T object)[source]

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters:
  • consumer – The consumer to accept, may be null for a noop.

  • object – The object to consume by consumer

Type parameters:
  • <T> – The type the consumer accepts

  • <E> – The type of checked exception the consumer may throw

public static <E extends Throwable> void accept(FailableDoubleConsumer<E> consumer, double value)[source]

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters:
  • consumer – The consumer to accept, may be null for a noop.

  • value – The value to consume by consumer

Type parameters:

<E> – The type of checked exception the consumer may throw

public static <E extends Throwable> void accept(FailableIntConsumer<E> consumer, int value)[source]

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters:
  • consumer – The consumer to accept, may be null for a noop.

  • value – The value to consume by consumer

Type parameters:

<E> – The type of checked exception the consumer may throw

public static <E extends Throwable> void accept(FailableLongConsumer<E> consumer, long value)[source]

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters:
  • consumer – The consumer to accept, may be null for a noop.

  • value – The value to consume by consumer

Type parameters:

<E> – The type of checked exception the consumer may throw

public static <T, U, R, E extends Throwable> R apply(FailableBiFunction<T, U, R, E> function, T input1, U input2)[source]

Applies a function and rethrows any exception as a RuntimeException.

Parameters:
  • function – The function to apply

  • input1 – The first input to apply function on

  • input2 – The second input to apply function on

Type parameters:
  • <T> – The type of the first argument the function accepts

  • <U> – The type of the second argument the function accepts

  • <R> – The return type of the function

  • <E> – The type of checked exception the function may throw

Returns:

The value returned from the function

public static <T, R, E extends Throwable> R apply(FailableFunction<T, R, E> function, T input)[source]

Applies a function and rethrows any exception as a RuntimeException.

Parameters:
  • function – The function to apply

  • input – The input to apply function on

Type parameters:
  • <T> – The type of the argument the function accepts

  • <R> – The return type of the function

  • <E> – The type of checked exception the function may throw

Returns:

The value returned from the function

public static <E extends Throwable> double applyAsDouble(FailableDoubleBinaryOperator<E> function, double left, double right)[source]

Applies a function and rethrows any exception as a RuntimeException.

Parameters:
  • function – The function to apply

  • left – The first input to apply function on

  • right – The second input to apply function on

Type parameters:

<E> – The type of checked exception the function may throw

Returns:

The value returned from the function

public static <T, R, E extends Throwable> R applyNonNull(T value, FailableFunction<? super T, ? extends R, E> mapper) throws E[source]

Applies a value to a function if the value isn’t null, otherwise the method returns null. If the value isn’t null then return the result of the applying function.

Failable.applyNonNull("a", String::toUpperCase)  = "A"
Failable.applyNonNull(null, String::toUpperCase) = null
Failable.applyNonNull("a", s -> null)            = null

Useful when working with expressions that may return null as it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to using Optional that is shorter and has less allocation.

Type parameters:
  • <T> – The type of the input of this method and the function.

  • <R> – The type of the result of the function and this method.

  • <E> – The type of thrown exception or error.

Parameters:
  • value – The value to apply the function to, may be null.

  • mapper – The function to apply, must not be null.

Returns:

The result of the function (which may be null) or null if the input value is null.

Throws:

E – Thrown by the given function.

See also:
Since:

3.19.0

public static <T, U, R, E1 extends Throwable, E2 extends Throwable> R applyNonNull(T value1, FailableFunction<? super T, ? extends U, E1> mapper1, FailableFunction<? super U, ? extends R, E2> mapper2) throws E1, E2[source]

Applies values to a chain of functions, where a null can short-circuit each step. A function is only applied if the previous value is not null, otherwise this method returns null.

Failable.applyNonNull(" a ", String::toUpperCase, String::trim) = "A"
Failable.applyNonNull(null, String::toUpperCase, String::trim)  = null
Failable.applyNonNull(" a ", s -> null, String::trim)           = null
Failable.applyNonNull(" a ", String::toUpperCase, s -> null)    = null

Useful when working with expressions that may return null as it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to using Optional that is shorter and has less allocation.

Type parameters:
  • <T> – The type of the input of this method and the first function.

  • <U> – The type of the result of the first function and the input to the second function.

  • <R> – The type of the result of the second function and this method.

  • <E1> – The type of thrown exception or error by the first function.

  • <E2> – The type of thrown exception or error by the second function.

Parameters:
  • value1 – The value to apply the functions to, may be null.

  • mapper1 – The first function to apply, must not be null.

  • mapper2 – The second function to apply, must not be null.

Returns:

The result of the final function (which may be null) or null if the input value or any intermediate value is null.

Throws:
  • E1 – Thrown by the first function.

  • E2 – Thrown by the second function.

See also:
Since:

3.19.0

public static <T, U, V, R, E1 extends Throwable, E2 extends Throwable, E3 extends Throwable> R applyNonNull(T value1, FailableFunction<? super T, ? extends U, E1> mapper1, FailableFunction<? super U, ? extends V, E2> mapper2, FailableFunction<? super V, ? extends R, E3> mapper3) throws E1, E2, E3[source]

Applies values to a chain of functions, where a null can short-circuit each step. A function is only applied if the previous value is not null, otherwise this method returns null.

Failable.applyNonNull(" abc ", String::toUpperCase, String::trim, StringUtils::reverse) = "CBA"
Failable.applyNonNull(null, String::toUpperCase, String::trim, StringUtils::reverse)    = null
Failable.applyNonNull(" abc ", s -> null, String::trim, StringUtils::reverse)           = null
Failable.applyNonNull(" abc ", String::toUpperCase, s -> null, StringUtils::reverse)    = null
Failable.applyNonNull(" abc ", String::toUpperCase, String::trim, s -> null)            = null

Useful when working with expressions that may return null as it allows a single-line expression without using temporary local variables or evaluating expressions twice. Provides an alternative to using Optional that is shorter and has less allocation.

Type parameters:
  • <T> – The type of the input of this method and the first function.

  • <U> – The type of the result of the first function and the input to the second function.

  • <V> – The type of the result of the second function and the input to the third function.

  • <R> – The type of the result of the third function and this method.

  • <E1> – The type of thrown exception or error by the first function.

  • <E2> – The type of thrown exception or error by the second function.

  • <E3> – The type of thrown exception or error by the second function.

Parameters:
  • value1 – The value to apply the first function, may be null.

  • mapper1 – The first function to apply, must not be null.

  • mapper2 – The second function to apply, must not be null.

  • mapper3 – The third function to apply, must not be null.

Returns:

The result of the final function (which may be null) or null if the input value or any intermediate value is null.

Throws:
  • E1 – Thrown by the first function.

  • E2 – Thrown by the second function.

  • E3 – Thrown by the third function.

See also:
Since:

3.19.0

public static <T, U> BiConsumer<T, U> asBiConsumer(FailableBiConsumer<T, U, ?> consumer)[source]

Converts the given FailableBiConsumer into a standard BiConsumer.

Type parameters:
  • <T> – The type of the first argument of the consumers

  • <U> – The type of the second argument of the consumers

Parameters:

consumer – A failable BiConsumer

Returns:

A standard BiConsumer

public static <T, U, R> BiFunction<T, U, R> asBiFunction(FailableBiFunction<T, U, R, ?> function)[source]

Converts the given FailableBiFunction into a standard BiFunction.

Type parameters:
  • <T> – The type of the first argument of the input of the functions

  • <U> – The type of the second argument of the input of the functions

  • <R> – The type of the output of the functions

Parameters:

function – A FailableBiFunction

Returns:

A standard BiFunction

public static <T, U> BiPredicate<T, U> asBiPredicate(FailableBiPredicate<T, U, ?> predicate)[source]

Converts the given FailableBiPredicate into a standard BiPredicate.

Type parameters:
  • <T> – The type of the first argument used by the predicates

  • <U> – The type of the second argument used by the predicates

Parameters:

predicate – A FailableBiPredicate

Returns:

A standard BiPredicate

public static <V> Callable<V> asCallable(FailableCallable<V, ?> callable)[source]

Converts the given FailableCallable into a standard Callable.

Type parameters:

<V> – The type used by the callables

Parameters:

callable – A FailableCallable

Returns:

A standard Callable

public static <T> Consumer<T> asConsumer(FailableConsumer<T, ?> consumer)[source]

Converts the given FailableConsumer into a standard Consumer.

Type parameters:

<T> – The type used by the consumers

Parameters:

consumer – A FailableConsumer

Returns:

A standard Consumer

public static <T, R> Function<T, R> asFunction(FailableFunction<T, R, ?> function)[source]

Converts the given FailableFunction into a standard Function.

Type parameters:
  • <T> – The type of the input of the functions

  • <R> – The type of the output of the functions

Parameters:

function – A {code FailableFunction}

Returns:

A standard Function

public static <T> Predicate<T> asPredicate(FailablePredicate<T, ?> predicate)[source]

Converts the given FailablePredicate into a standard Predicate.

Type parameters:

<T> – The type used by the predicates

Parameters:

predicate – A FailablePredicate

Returns:

A standard Predicate

public static Runnable asRunnable(FailableRunnable<?> runnable)[source]

Converts the given FailableRunnable into a standard Runnable.

Parameters:

runnable – A FailableRunnable

Returns:

A standard Runnable

public static <T> Supplier<T> asSupplier(FailableSupplier<T, ?> supplier)[source]

Converts the given FailableSupplier into a standard Supplier.

Type parameters:

<T> – The type supplied by the suppliers

Parameters:

supplier – A FailableSupplier

Returns:

A standard Supplier

public static <V, E extends Throwable> V call(FailableCallable<V, E> callable)[source]

Calls a callable and rethrows any exception as a RuntimeException.

Parameters:

callable – The callable to call

Type parameters:
  • <V> – The return type of the callable

  • <E> – The type of checked exception the callable may throw

Returns:

The value returned from the callable

public static <T, E extends Throwable> T get(FailableSupplier<T, E> supplier)[source]

Gets the result of invoking the supplier.

Parameters:

supplier – The supplier to invoke.

Type parameters:
  • <T> – The supplier’s output type.

  • <E> – The type of checked exception, which the supplier can throw.

Returns:

The object, which has been created by the supplier

public static <E extends Throwable> boolean getAsBoolean(FailableBooleanSupplier<E> supplier)[source]

Gets the result of invoking the boolean supplier.

Parameters:

supplier – The boolean supplier to invoke.

Type parameters:

<E> – The type of checked exception, which the supplier can throw.

Returns:

The boolean, which has been created by the supplier

public static <E extends Throwable> double getAsDouble(FailableDoubleSupplier<E> supplier)[source]

Gets the result of invoking the double supplier.

Parameters:

supplier – The double supplier to invoke.

Type parameters:

<E> – The type of checked exception, which the supplier can throw.

Returns:

The double, which has been created by the supplier

public static <E extends Throwable> int getAsInt(FailableIntSupplier<E> supplier)[source]

Gets the result of invoking the int supplier.

Parameters:

supplier – The int supplier to invoke.

Type parameters:

<E> – The type of checked exception, which the supplier can throw.

Returns:

The int, which has been created by the supplier

public static <E extends Throwable> long getAsLong(FailableLongSupplier<E> supplier)[source]

Gets the result of invoking the long supplier.

Parameters:

supplier – The long supplier to invoke.

Type parameters:

<E> – The type of checked exception, which the supplier can throw.

Returns:

The long, which has been created by the supplier

public static <E extends Throwable> short getAsShort(FailableShortSupplier<E> supplier)[source]

Gets the result of invoking the short supplier.

Parameters:

supplier – The short supplier to invoke.

Type parameters:

<E> – The type of checked exception, which the supplier can throw.

Returns:

The short, which has been created by the supplier

public static RuntimeException rethrow(Throwable throwable)[source]

Rethrows a Throwable as an unchecked exception. If the argument is already unchecked, namely a RuntimeException or Error then the argument will be rethrown without modification. If the exception is IOException then it will be wrapped into a UncheckedIOException. In every other cases the exception will be wrapped into a UndeclaredThrowableException

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 possibly wrapped into an unchecked exception

Returns:

Never returns anything, this method never terminates normally.

public static <E extends Throwable> void run(FailableRunnable<E> runnable)[source]

Runs a runnable and rethrows any exception as a RuntimeException.

Parameters:

runnable – The runnable to run, may be null for a noop.

Type parameters:

<E> – The type of checked exception the runnable may throw.

public static <E> Streams.FailableStream<E> stream(Collection<E> collection)[source]

Converts the given collection into a FailableStream. The FailableStream consists of the collections elements. Shortcut for

Functions.stream(collection.stream());
Parameters:

collection – The collection, which is being converted into a FailableStream.

Type parameters:

<E> – The collections element type. (In turn, the result streams element type.)

Returns:

The created FailableStream.

public static <T> Streams.FailableStream<T> stream(Stream<T> stream)[source]

Converts the given stream into a FailableStream. The FailableStream consists of the same elements, than the input stream. However, failable lambdas, like FailablePredicate, FailableFunction, and FailableConsumer may be applied, rather than Predicate, Function, Consumer, etc.

Parameters:

stream – The stream, which is being converted into a FailableStream.

Type parameters:

<T> – The streams element type.

Returns:

The created FailableStream.

public static <T, U, E extends Throwable> boolean test(FailableBiPredicate<T, U, E> predicate, T object1, U object2)[source]

Tests a predicate and rethrows any exception as a RuntimeException.

Parameters:
  • predicate – The predicate to test

  • object1 – The first input to test by predicate

  • object2 – The second input to test by predicate

Type parameters:
  • <T> – The type of the first argument the predicate tests

  • <U> – The type of the second argument the predicate tests

  • <E> – The type of checked exception the predicate may throw

Returns:

The boolean value returned by the predicate

public static <T, E extends Throwable> boolean test(FailablePredicate<T, E> predicate, T object)[source]

Tests a predicate and rethrows any exception as a RuntimeException.

Parameters:
  • predicate – The predicate to test

  • object – The input to test by predicate

Type parameters:
  • <T> – The type of argument the predicate tests

  • <E> – The type of checked exception the predicate may throw

Returns:

The boolean value returned by the predicate

@SafeVarargs public static void tryWithResources(FailableRunnable<? extends Throwable> action, FailableConsumer<Throwable, ? extends Throwable> errorHandler, FailableRunnable<? extends Throwable>... resources)[source]

A simple try-with-resources implementation, that can be used, if your objects do not implement the AutoCloseable interface. The method executes the action. The method guarantees, that all the resources 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());
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 FailableRunnable, which will be executed.

See also:

#tryWithResources(FailableRunnable, FailableRunnable...)

@SafeVarargs public static void tryWithResources(FailableRunnable<? extends Throwable> action, FailableRunnable<? extends Throwable>... resources)[source]

A simple try-with-resources implementation, that can be used, if your objects do not implement the AutoCloseable interface. The method executes the action. The method guarantees, that all the resources 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());
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 FailableRunnable, which will be executed.

See also:

#tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)