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

public Class Failable

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

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 second version.

Since
3.11

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the first argument the consumer accepts
T
,
the type of the second argument the consumer accepts
U
,
the type of checked exception the consumer may throw
E extends Throwable
>
void
accept(final FailableBiConsumer<T, U, E>
the consumer to accept, may be null for a noop.
consumer
,
final T
the first object to consume by consumer
object1
,
final U
the second object to consume by consumer
object2
)

Consumes a consumer and rethrows any exception as a RuntimeException.

public static <
the type the consumer accepts
T
,
the type of checked exception the consumer may throw
E extends Throwable
>
void
accept(final FailableConsumer<T, E>
the consumer to accept, may be null for a noop.
consumer
,
final T
the object to consume by consumer
object
)

Consumes a consumer and rethrows any exception as a RuntimeException.

public static <
the type of checked exception the consumer may throw
E extends Throwable
>
void
accept(final FailableDoubleConsumer<E>
the consumer to accept, may be null for a noop.
consumer
,
final double
the value to consume by consumer
value
)

Consumes a consumer and rethrows any exception as a RuntimeException.

public static <
the type of checked exception the consumer may throw
E extends Throwable
>
void
accept(final FailableIntConsumer<E>
the consumer to accept, may be null for a noop.
consumer
,
final int
the value to consume by consumer
value
)

Consumes a consumer and rethrows any exception as a RuntimeException.

public static <
the type of checked exception the consumer may throw
E extends Throwable
>
void
accept(final FailableLongConsumer<E>
the consumer to accept, may be null for a noop.
consumer
,
final long
the value to consume by consumer
value
)

Consumes a consumer and rethrows any exception as a RuntimeException.

public static <
the type of the first argument the function accepts
T
,
the type of the second argument the function accepts
U
,
the return type of the function
R
,
the type of checked exception the function may throw
E extends Throwable
>
R

Returns:

the value returned from the function
apply
(final FailableBiFunction<T, U, R, E>
the function to apply
function
,
final T
the first input to apply function on
input1
,
final U
the second input to apply function on
input2
)

Applies a function and rethrows any exception as a RuntimeException.

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

Returns:

the value returned from the function
apply
(final FailableFunction<T, R, E>
the function to apply
function
,
final T
the input to apply function on
input
)

Applies a function and rethrows any exception as a RuntimeException.

public static <
the type of checked exception the function may throw
E extends Throwable
>
double

Returns:

the value returned from the function
applyAsDouble
(final FailableDoubleBinaryOperator<E>
the function to apply
function
,
final double
the first input to apply function on
left
,
final double
the second input to apply function on
right
)

Applies a function and rethrows any exception as a RuntimeException.

public static <
The type of the input of this method and the function.
T
,
The type of the result of the function and this method.
R
,
The type of thrown exception or error.
E extends Throwable
>
R

Returns:

The result of the function (which may be null) or null if the input value is null.
applyNonNull
(final T
The value to apply the function to, may be null.
value
,
final FailableFunction<? super T, ? extends R, E>
The function to apply, must not be null.
mapper
)

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

public static <
The type of the input of this method and the first function.
T
,
The type of the result of the first function and the input to the second function.
U
,
The type of the result of the second function and this method.
R
,
The type of thrown exception or error by the first function.
E1 extends Throwable
,
The type of thrown exception or error by the second function.
E2 extends Throwable
>
R

Returns:

The result of the final function (which may be null) or null if the input value or any intermediate value is null.
applyNonNull
(final T
The value to apply the functions to, may be null.
value1
,
final FailableFunction<? super T, ? extends U, E1>
The first function to apply, must not be null.
mapper1
,
final FailableFunction<? super U, ? extends R, E2>
The second function to apply, must not be null.
mapper2
)

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

public static <
The type of the input of this method and the first function.
T
,
The type of the result of the first function and the input to the second function.
U
,
The type of the result of the second function and the input to the third function.
V
,
The type of the result of the third function and this method.
R
,
The type of thrown exception or error by the first function.
E1 extends Throwable
,
The type of thrown exception or error by the second function.
E2 extends Throwable
,
The type of thrown exception or error by the second function.
E3 extends Throwable
>
R

Returns:

The result of the final function (which may be null) or null if the input value or any intermediate value is null.
applyNonNull
(final T
The value to apply the first function, may be null.
value1
,
final FailableFunction<? super T, ? extends U, E1>
The first function to apply, must not be null.
mapper1
,
final FailableFunction<? super U, ? extends V, E2>
The second function to apply, must not be null.
mapper2
,
final FailableFunction<? super V, ? extends R, E3>
The third function to apply, must not be null.
mapper3
)

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

public static <
the type of the first argument of the consumers
T
,
the type of the second argument of the consumers
U
>
BiConsumer<T, U>

Returns:

a standard BiConsumer
asBiConsumer
(final FailableBiConsumer<T, U, ?>
a failable BiConsumer
consumer
)

Converts the given FailableBiConsumer into a standard BiConsumer.

public static <
the type of the first argument of the input of the functions
T
,
the type of the second argument of the input of the functions
U
,
the type of the output of the functions
R
>
BiFunction<T, U, R>

Returns:

a standard BiFunction
asBiFunction
(final FailableBiFunction<T, U, R, ?> function)

Converts the given FailableBiFunction into a standard BiFunction.

public static <
the type of the first argument used by the predicates
T
,
the type of the second argument used by the predicates
U
>
BiPredicate<T, U>

Returns:

a standard BiPredicate
asBiPredicate
(final FailableBiPredicate<T, U, ?> predicate)

Converts the given FailableBiPredicate into a standard BiPredicate.

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

Returns:

a standard Callable
asCallable
(final FailableCallable<V, ?> callable)

Converts the given FailableCallable into a standard Callable.

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

Returns:

a standard Consumer
asConsumer
(final FailableConsumer<T, ?> consumer)

Converts the given FailableConsumer into a standard Consumer.

public static <
the type of the input of the functions
T
,
the type of the output of the functions
R
>
Function<T, R>

Returns:

a standard Function
asFunction
(final FailableFunction<T, R, ?>
a {code FailableFunction}
function
)

Converts the given FailableFunction into a standard Function.

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

Returns:

a standard Predicate
asPredicate
(final FailablePredicate<T, ?> predicate)

Converts the given FailablePredicate into a standard Predicate.

public static Runnable

Returns:

a standard Runnable
asRunnable
(final FailableRunnable<?> runnable)

Converts the given FailableRunnable into a standard Runnable.

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

Returns:

a standard Supplier
asSupplier
(final FailableSupplier<T, ?> supplier)

Converts the given FailableSupplier into a standard Supplier.

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

Returns:

the value returned from the callable
call
(final FailableCallable<V, E>
the callable to call
callable
)

Calls a callable and rethrows any exception as a RuntimeException.

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

Returns:

The object, which has been created by the supplier
get
(final FailableSupplier<T, E>
The supplier to invoke.
supplier
)

Invokes a supplier, and returns the result.

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

Returns:

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

Invokes a boolean supplier, and returns the result.

public static <
The type of checked exception, which the supplier can throw.
E extends Throwable
>
double

Returns:

The double, which has been created by the supplier
getAsDouble
(final FailableDoubleSupplier<E>
The double supplier to invoke.
supplier
)

Invokes a double supplier, and returns the result.

public static <
The type of checked exception, which the supplier can throw.
E extends Throwable
>
int

Returns:

The int, which has been created by the supplier
getAsInt
(final FailableIntSupplier<E>
The int supplier to invoke.
supplier
)

Invokes an int supplier, and returns the result.

public static <
The type of checked exception, which the supplier can throw.
E extends Throwable
>
long

Returns:

The long, which has been created by the supplier
getAsLong
(final FailableLongSupplier<E>
The long supplier to invoke.
supplier
)

Invokes a long supplier, and returns the result.

public static <
The type of checked exception, which the supplier can throw.
E extends Throwable
>
short

Returns:

The short, which has been created by the supplier
getAsShort
(final FailableShortSupplier<E>
The short supplier to invoke.
supplier
)

Invokes a short 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.
E extends Throwable
>
void
run(final FailableRunnable<E>
The runnable to run, may be null for a noop.
runnable
)

Runs a runnable and rethrows any exception as a RuntimeException.

private static <E extends Throwable> void
run(final Object test, final FailableRunnable<E> runnable)

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

Returns:

The created FailableStream.
stream
(final Collection<E>
The collection, which is being converted into a FailableStream.
collection
)

Converts the given collection into a FailableStream.

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

Returns:

The created FailableStream.
stream
(final Stream<T>
The stream, which is being converted into a FailableStream.
stream
)

Converts the given stream into a FailableStream.

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

Returns:

the boolean value returned by the predicate
test
(final FailableBiPredicate<T, U, E>
the predicate to test
predicate
,
final T
the first input to test by predicate
object1
,
final U
the second input to test by predicate
object2
)

Tests a predicate and rethrows any exception as a RuntimeException.

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

Returns:

the boolean value returned by the predicate
test
(final FailablePredicate<T, E>
the predicate to test
predicate
,
final T
the input to test by predicate
object
)

Tests a predicate and rethrows any exception as a RuntimeException.

public static void
tryWithResources(final FailableRunnable<? extends Throwable>
The action to execute. This object will always be invoked.
action
,
final 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 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
)

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

public static void
tryWithResources(final FailableRunnable<? extends Throwable>
The action to execute. This object will always be invoked.
action
,
final 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
)

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

Constructor Detail

Failableback to summary
private Failable()

Method Detail

acceptback to summary
public static <T, U, E extends Throwable> void accept(final FailableBiConsumer<T, U, E> consumer, final T object1, final U object2)

Consumes a consumer and rethrows any exception as a RuntimeException.

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
consumer:FailableBiConsumer<T, U, E>

the consumer to accept, may be null for a noop.

object1:T

the first object to consume by consumer

object2:U

the second object to consume by consumer

acceptback to summary
public static <T, E extends Throwable> void accept(final FailableConsumer<T, E> consumer, final T object)

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters
<T>
the type the consumer accepts
<E>
the type of checked exception the consumer may throw
consumer:FailableConsumer<T, E>

the consumer to accept, may be null for a noop.

object:T

the object to consume by consumer

acceptback to summary
public static <E extends Throwable> void accept(final FailableDoubleConsumer<E> consumer, final double value)

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters
<E>
the type of checked exception the consumer may throw
consumer:FailableDoubleConsumer<E>

the consumer to accept, may be null for a noop.

value:double

the value to consume by consumer

acceptback to summary
public static <E extends Throwable> void accept(final FailableIntConsumer<E> consumer, final int value)

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters
<E>
the type of checked exception the consumer may throw
consumer:FailableIntConsumer<E>

the consumer to accept, may be null for a noop.

value:int

the value to consume by consumer

acceptback to summary
public static <E extends Throwable> void accept(final FailableLongConsumer<E> consumer, final long value)

Consumes a consumer and rethrows any exception as a RuntimeException.

Parameters
<E>
the type of checked exception the consumer may throw
consumer:FailableLongConsumer<E>

the consumer to accept, may be null for a noop.

value:long

the value to consume by consumer

applyback to summary
public static <T, U, R, E extends Throwable> R apply(final FailableBiFunction<T, U, R, E> function, final T input1, final U input2)

Applies a function and rethrows any exception as a RuntimeException.

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
function:FailableBiFunction<T, U, R, E>

the function to apply

input1:T

the first input to apply function on

input2:U

the second input to apply function on

Returns:R

the value returned from the function

applyback to summary
public static <T, R, E extends Throwable> R apply(final FailableFunction<T, R, E> function, final T input)

Applies a function and rethrows any exception as a RuntimeException.

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
function:FailableFunction<T, R, E>

the function to apply

input:T

the input to apply function on

Returns:R

the value returned from the function

applyAsDoubleback to summary
public static <E extends Throwable> double applyAsDouble(final FailableDoubleBinaryOperator<E> function, final double left, final double right)

Applies a function and rethrows any exception as a RuntimeException.

Parameters
<E>
the type of checked exception the function may throw
function:FailableDoubleBinaryOperator<E>

the function to apply

left:double

the first input to apply function on

right:double

the second input to apply function on

Returns:double

the value returned from the function

applyNonNullback to summary
public static <T, R, E extends Throwable> R applyNonNull(final T value, final FailableFunction<? super T, ? extends R, E> mapper) throws E

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.

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.
value:T

The value to apply the function to, may be null.

mapper:FailableFunction<? super T, ? extends R, E>

The function to apply, must not be null.

Returns:R

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

Exceptions
E:
Thrown by the given function.
Since
3.19.0
See Also
applyNonNull(Object, FailableFunction, FailableFunction), applyNonNull(Object, FailableFunction, FailableFunction, FailableFunction)
applyNonNullback to summary
public static <T, U, R, E1 extends Throwable, E2 extends Throwable> R applyNonNull(final T value1, final FailableFunction<? super T, ? extends U, E1> mapper1, final FailableFunction<? super U, ? extends R, E2> mapper2) throws FailableFunction-:E1, FailableFunction-:E2

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.

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.
value1:T

The value to apply the functions to, may be null.

mapper1:FailableFunction<? super T, ? extends U, E1>

The first function to apply, must not be null.

mapper2:FailableFunction<? super U, ? extends R, E2>

The second function to apply, must not be null.

Returns:R

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

Exceptions
FailableFunction-:E1:
Thrown by the first function.
FailableFunction-:E2:
Thrown by the second function.
Since
3.19.0
See Also
applyNonNull(Object, FailableFunction), applyNonNull(Object, FailableFunction, FailableFunction, FailableFunction)
applyNonNullback to summary
public static <T, U, V, R, E1 extends Throwable, E2 extends Throwable, E3 extends Throwable> R applyNonNull(final T value1, final FailableFunction<? super T, ? extends U, E1> mapper1, final FailableFunction<? super U, ? extends V, E2> mapper2, final FailableFunction<? super V, ? extends R, E3> mapper3) throws FailableFunction-:E1, FailableFunction-:E2, FailableFunction-:E3

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.

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.
value1:T

The value to apply the first function, may be null.

mapper1:FailableFunction<? super T, ? extends U, E1>

The first function to apply, must not be null.

mapper2:FailableFunction<? super U, ? extends V, E2>

The second function to apply, must not be null.

mapper3:FailableFunction<? super V, ? extends R, E3>

The third function to apply, must not be null.

Returns:R

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

Exceptions
FailableFunction-:E1:
Thrown by the first function.
FailableFunction-:E2:
Thrown by the second function.
FailableFunction-:E3:
Thrown by the third function.
Since
3.19.0
See Also
applyNonNull(Object, FailableFunction), applyNonNull(Object, FailableFunction, FailableFunction)
asBiConsumerback to summary
public static <T, U> BiConsumer<T, U> asBiConsumer(final FailableBiConsumer<T, U, ?> consumer)

Converts the given FailableBiConsumer into a standard BiConsumer.

Parameters
<T>
the type of the first argument of the consumers
<U>
the type of the second argument of the consumers
consumer:FailableBiConsumer<T, U, ?>

a failable BiConsumer

Returns:BiConsumer<T, U>

a standard BiConsumer

asBiFunctionback to summary
public static <T, U, R> BiFunction<T, U, R> asBiFunction(final FailableBiFunction<T, U, R, ?> function)

Converts the given FailableBiFunction into a standard BiFunction.

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
function:FailableBiFunction<T, U, R, ?>

a FailableBiFunction

Returns:BiFunction<T, U, R>

a standard BiFunction

asBiPredicateback to summary
public static <T, U> BiPredicate<T, U> asBiPredicate(final FailableBiPredicate<T, U, ?> predicate)

Converts the given FailableBiPredicate into a standard BiPredicate.

Parameters
<T>
the type of the first argument used by the predicates
<U>
the type of the second argument used by the predicates
predicate:FailableBiPredicate<T, U, ?>

a FailableBiPredicate

Returns:BiPredicate<T, U>

a standard BiPredicate

asCallableback to summary
public static <V> Callable<V> asCallable(final FailableCallable<V, ?> callable)

Converts the given FailableCallable into a standard Callable.

Parameters
<V>
the type used by the callables
callable:FailableCallable<V, ?>

a FailableCallable

Returns:Callable<V>

a standard Callable

asConsumerback to summary
public static <T> Consumer<T> asConsumer(final FailableConsumer<T, ?> consumer)

Converts the given FailableConsumer into a standard Consumer.

Parameters
<T>
the type used by the consumers
consumer:FailableConsumer<T, ?>

a FailableConsumer

Returns:Consumer<T>

a standard Consumer

asFunctionback to summary
public static <T, R> Function<T, R> asFunction(final FailableFunction<T, R, ?> function)

Converts the given FailableFunction into a standard Function.

Parameters
<T>
the type of the input of the functions
<R>
the type of the output of the functions
function:FailableFunction<T, R, ?>

a {code FailableFunction}

Returns:Function<T, R>

a standard Function

asPredicateback to summary
public static <T> Predicate<T> asPredicate(final FailablePredicate<T, ?> predicate)

Converts the given FailablePredicate into a standard Predicate.

Parameters
<T>
the type used by the predicates
predicate:FailablePredicate<T, ?>

a FailablePredicate

Returns:Predicate<T>

a standard Predicate

asRunnableback to summary
public static Runnable asRunnable(final FailableRunnable<?> runnable)

Converts the given FailableRunnable into a standard Runnable.

Parameters
runnable:FailableRunnable<?>

a FailableRunnable

Returns:Runnable

a standard Runnable

asSupplierback to summary
public static <T> Supplier<T> asSupplier(final FailableSupplier<T, ?> supplier)

Converts the given FailableSupplier into a standard Supplier.

Parameters
<T>
the type supplied by the suppliers
supplier:FailableSupplier<T, ?>

a FailableSupplier

Returns:Supplier<T>

a standard Supplier

callback to summary
public static <V, E extends Throwable> V call(final FailableCallable<V, E> callable)

Calls a callable and rethrows any exception as a RuntimeException.

Parameters
<V>
the return type of the callable
<E>
the type of checked exception the callable may throw
callable:FailableCallable<V, E>

the callable to call

Returns:V

the value returned from the callable

getback to summary
public static <T, E extends Throwable> T get(final FailableSupplier<T, E> supplier)

Invokes a supplier, and returns the result.

Parameters
<T>
The suppliers output type.
<E>
The type of checked exception, which the supplier can throw.
supplier:FailableSupplier<T, E>

The supplier to invoke.

Returns:T

The object, which has been created by the supplier

getAsBooleanback to summary
public static <E extends Throwable> boolean getAsBoolean(final FailableBooleanSupplier<E> supplier)

Invokes a boolean supplier, and returns the result.

Parameters
<E>
The type of checked exception, which the supplier can throw.
supplier:FailableBooleanSupplier<E>

The boolean supplier to invoke.

Returns:boolean

The boolean, which has been created by the supplier

getAsDoubleback to summary
public static <E extends Throwable> double getAsDouble(final FailableDoubleSupplier<E> supplier)

Invokes a double supplier, and returns the result.

Parameters
<E>
The type of checked exception, which the supplier can throw.
supplier:FailableDoubleSupplier<E>

The double supplier to invoke.

Returns:double

The double, which has been created by the supplier

getAsIntback to summary
public static <E extends Throwable> int getAsInt(final FailableIntSupplier<E> supplier)

Invokes an int supplier, and returns the result.

Parameters
<E>
The type of checked exception, which the supplier can throw.
supplier:FailableIntSupplier<E>

The int supplier to invoke.

Returns:int

The int, which has been created by the supplier

getAsLongback to summary
public static <E extends Throwable> long getAsLong(final FailableLongSupplier<E> supplier)

Invokes a long supplier, and returns the result.

Parameters
<E>
The type of checked exception, which the supplier can throw.
supplier:FailableLongSupplier<E>

The long supplier to invoke.

Returns:long

The long, which has been created by the supplier

getAsShortback to summary
public static <E extends Throwable> short getAsShort(final FailableShortSupplier<E> supplier)

Invokes a short supplier, and returns the result.

Parameters
<E>
The type of checked exception, which the supplier can throw.
supplier:FailableShortSupplier<E>

The short supplier to invoke.

Returns:short

The short, which has been created by the supplier

rethrowback to summary
public static RuntimeException rethrow(final Throwable throwable)

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:Throwable

The throwable to rethrow possibly wrapped into an unchecked exception

Returns:RuntimeException

Never returns anything, this method never terminates normally.

runback to summary
public static <E extends Throwable> void run(final FailableRunnable<E> runnable)

Runs a runnable and rethrows any exception as a RuntimeException.

Parameters
<E>
the type of checked exception the runnable may throw.
runnable:FailableRunnable<E>

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

runback to summary
private static <E extends Throwable> void run(final Object test, final FailableRunnable<E> runnable)
streamback to summary
public static <E> Streams.FailableStream<E> stream(final Collection<E> collection)

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

Functions.stream(collection.stream());
Parameters
<E>
The collections element type. (In turn, the result streams element type.)
collection:Collection<E>

The collection, which is being converted into a FailableStream.

Returns:Streams.FailableStream<E>

The created FailableStream.

streamback to summary
public static <T> Streams.FailableStream<T> stream(final Stream<T> stream)

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
<T>
The streams element type.
stream:Stream<T>

The stream, which is being converted into a FailableStream.

Returns:Streams.FailableStream<T>

The created FailableStream.

testback to summary
public static <T, U, E extends Throwable> boolean test(final FailableBiPredicate<T, U, E> predicate, final T object1, final U object2)

Tests a predicate and rethrows any exception as a RuntimeException.

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
predicate:FailableBiPredicate<T, U, E>

the predicate to test

object1:T

the first input to test by predicate

object2:U

the second input to test by predicate

Returns:boolean

the boolean value returned by the predicate

testback to summary
public static <T, E extends Throwable> boolean test(final FailablePredicate<T, E> predicate, final T object)

Tests a predicate and rethrows any exception as a RuntimeException.

Parameters
<T>
the type of argument the predicate tests
<E>
the type of checked exception the predicate may throw
predicate:FailablePredicate<T, E>

the predicate to test

object:T

the input to test by predicate

Returns:boolean

the boolean value returned by the predicate

tryWithResourcesback to summary
public static void tryWithResources(final FailableRunnable<? extends Throwable> action, final FailableConsumer<Throwable, ? extends Throwable> errorHandler, final FailableRunnable<? extends Throwable>... resources)

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:FailableRunnable<? extends Throwable>

The action to execute. This object will always be invoked.

errorHandler: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.

resources: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.

Annotations
@SafeVarargs
See Also
tryWithResources(FailableRunnable, FailableRunnable...)
tryWithResourcesback to summary
public static void tryWithResources(final FailableRunnable<? extends Throwable> action, final FailableRunnable<? extends Throwable>... resources)

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:FailableRunnable<? extends Throwable>

The action to execute. This object will always be invoked.

resources: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.

Annotations
@SafeVarargs
See Also
tryWithResources(FailableRunnable, FailableConsumer, FailableRunnable...)