This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push: new 00c8096 [LANG-1568] Complete FailableFunction. 00c8096 is described below commit 00c8096cbf6ca89b17685488eddb5d0782fa18e4 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Jun 25 09:44:22 2020 -0400 [LANG-1568] Complete FailableFunction. --- .../commons/lang3/function/FailableFunction.java | 25 +++++++++++++++++++ .../lang3/function/FailableFunctionsTest.java | 28 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) 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 cb100a4..49af31c 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)); + } } diff --git a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java index ce8b567..ca3ccd4 100644 --- a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java @@ -860,6 +860,34 @@ public class FailableFunctionsTest { } @Test + public void testFunctionCompose() throws Throwable { + final Testable<?, ?> testable = new Testable<>(null); + final FailableFunction<Object, Integer, Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + return 0; + }; + final FailableFunction<Object, Integer, Throwable> nop = FailableFunction.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.compose(failing).apply(0)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.compose(nop).apply(0)); + assertSame(ERROR, e); + // Does not throw + nop.compose(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.compose(null)); + } + + @Test + public void testFunctionIdentity() throws Throwable { + final FailableFunction<Integer, Integer, Throwable> nop = FailableFunction.identity(); + // Does not throw + nop.compose(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> nop.compose(null)); + } + + @Test public void testGetAsBooleanSupplier() { final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION); Throwable e = assertThrows(IllegalStateException.class,