diff options
author | Gary Gregory <garydgregory@gmail.com> | 2020-06-25 09:44:22 -0400 |
---|---|---|
committer | Gary Gregory <garydgregory@gmail.com> | 2020-06-25 09:44:22 -0400 |
commit | 00c8096cbf6ca89b17685488eddb5d0782fa18e4 (patch) | |
tree | adabcc156af77e3048f95ee4550be3a1f3e1be39 /src/main/java/org/apache/commons/lang3/function | |
parent | 9273b557b7d30cf52ebd09a98aa0f9e221d5b502 (diff) | |
download | apache-commons-lang-00c8096cbf6ca89b17685488eddb5d0782fa18e4.tar.gz |
[LANG-1568] Complete FailableFunction.
Diffstat (limited to 'src/main/java/org/apache/commons/lang3/function')
-rw-r--r-- | src/main/java/org/apache/commons/lang3/function/FailableFunction.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/lang3/function/FailableFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableFunction.java index cb100a4b2..49af31c39 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableFunction.java @@ -36,6 +36,17 @@ public interface FailableFunction<T, R, E extends Throwable> { FailableFunction NOP = t -> null; /** + * Returns a function that always returns its input argument. + * + * @param <T> the type of the input and output objects to the function + * @param <E> Thrown exception. + * @return a function that always returns its input argument + */ + static <T, E extends Throwable> FailableFunction<T, T, E> identity() { + return t -> t; + } + + /** * Returns The NOP singleton. * * @param <T> Consumed type 1. @@ -70,4 +81,18 @@ public interface FailableFunction<T, R, E extends Throwable> { */ R apply(T input) throws E; + /** + * Returns a composed {@code FailableFunction} like {@link Function#compose(Function)}. + * + * @param <V> the input type to the {@code before} function, and to the composed function. + * @param before the operator to apply before this one. + * @return a a composed {@code FailableFunction} like {@link Function#compose(Function)}. + * @throws NullPointerException if before is null. + * @throws E Thrown when a consumer fails. + * @see #andThen(FailableFunction) + */ + default <V> FailableFunction<V, R, E> compose(final FailableFunction<? super V, ? extends T, E> before) throws E { + Objects.requireNonNull(before); + return (final V v) -> apply(before.apply(v)); + } } |