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 d4ccd25 [LANG-1568] More checked andThen() implementations to mirror the JRE functional interfaces. d4ccd25 is described below commit d4ccd259d95ed8f1e2a7c2526cdec64692ca0ac9 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Jun 24 20:10:46 2020 -0400 [LANG-1568] More checked andThen() implementations to mirror the JRE functional interfaces. --- .../commons/lang3/function/FailableBiConsumer.java | 2 +- .../commons/lang3/function/FailableBiFunction.java | 4 +- .../commons/lang3/function/FailableConsumer.java | 2 +- .../lang3/function/FailableDoubleConsumer.java | 31 ++++++++ .../lang3/function/FailableDoubleFunction.java | 15 ++++ .../function/FailableDoubleToIntFunction.java | 14 ++++ .../function/FailableDoubleToLongFunction.java | 14 ++++ .../commons/lang3/function/FailableFunction.java | 16 ++++ .../lang3/function/FailableIntConsumer.java | 32 ++++++++ .../lang3/function/FailableIntFunction.java | 15 ++++ .../function/FailableIntToDoubleFunction.java | 14 ++++ .../lang3/function/FailableIntToLongFunction.java | 14 ++++ .../lang3/function/FailableLongConsumer.java | 31 ++++++++ .../lang3/function/FailableLongFunction.java | 15 ++++ .../function/FailableLongToDoubleFunction.java | 14 ++++ .../lang3/function/FailableLongToIntFunction.java | 14 ++++ .../lang3/function/FailableObjDoubleConsumer.java | 16 +++- .../lang3/function/FailableObjIntConsumer.java | 14 ++++ .../lang3/function/FailableObjLongConsumer.java | 14 ++++ .../lang3/function/FailableToDoubleBiFunction.java | 16 ++++ .../lang3/function/FailableToDoubleFunction.java | 17 ++++- .../lang3/function/FailableToIntBiFunction.java | 16 ++++ .../lang3/function/FailableToIntFunction.java | 17 ++++- .../lang3/function/FailableToLongBiFunction.java | 16 ++++ .../lang3/function/FailableToLongFunction.java | 15 ++++ .../lang3/function/FailableFunctionsTest.java | 88 ++++++++++++++++++++-- 26 files changed, 462 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/function/FailableBiConsumer.java b/src/main/java/org/apache/commons/lang3/function/FailableBiConsumer.java index 6d5d99f..10c6d80 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableBiConsumer.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableBiConsumer.java @@ -61,8 +61,8 @@ public interface FailableBiConsumer<T, U, E extends Throwable> { * * @param after the operation to perform after this one. * @return a composed {@code FailableBiConsumer} like {@link BiConsumer#andThen(BiConsumer)}. + * @throws NullPointerException when {@code after} is null. * @throws E Thrown when a consumer fails. - * @throws NullPointerException if {@code after} is null. */ default FailableBiConsumer<T, U, E> andThen(final FailableBiConsumer<? super T, ? super U, E> after) throws E { Objects.requireNonNull(after); diff --git a/src/main/java/org/apache/commons/lang3/function/FailableBiFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableBiFunction.java index 3173c73..f6dc750 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableBiFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableBiFunction.java @@ -53,11 +53,11 @@ public interface FailableBiFunction<T, U, R, E extends Throwable> { /** * Returns a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}. * - * @param <V> the type of output of the {@code after} function, and of the composed function + * @param <V> the output type of the {@code after} function, and of the composed function. * @param after the operation to perform after this one. * @return a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}. + * @throws NullPointerException when {@code after} is null. * @throws E Thrown when a consumer fails. - * @throws NullPointerException if {@code after} is null. */ default <V> FailableBiFunction<T, U, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) throws E { Objects.requireNonNull(after); diff --git a/src/main/java/org/apache/commons/lang3/function/FailableConsumer.java b/src/main/java/org/apache/commons/lang3/function/FailableConsumer.java index c2f2de8..97b7e4f 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableConsumer.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableConsumer.java @@ -58,8 +58,8 @@ public interface FailableConsumer<T, E extends Throwable> { * * @param after the operation to perform after this operation * @return a composed {@code Consumer} like {@link Consumer#andThen(Consumer)}. + * @throws NullPointerException when {@code after} is null * @throws E Thrown when a consumer fails. - * @throws NullPointerException if {@code after} is null */ default FailableConsumer<T, E> andThen(final FailableConsumer<? super T, E> after) throws E { Objects.requireNonNull(after); diff --git a/src/main/java/org/apache/commons/lang3/function/FailableDoubleConsumer.java b/src/main/java/org/apache/commons/lang3/function/FailableDoubleConsumer.java index 9b70da8..f91a23b 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableDoubleConsumer.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableDoubleConsumer.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.function; +import java.util.Objects; import java.util.function.DoubleConsumer; /** @@ -28,6 +29,20 @@ import java.util.function.DoubleConsumer; @FunctionalInterface public interface FailableDoubleConsumer<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableDoubleConsumer NOP = t -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableDoubleConsumer<E> nop() { + return NOP; + } + /** * Accepts the consumer. * @@ -35,4 +50,20 @@ public interface FailableDoubleConsumer<E extends Throwable> { * @throws E Thrown when the consumer fails. */ void accept(double value) throws E; + + /** + * Returns a composed {@code FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}. + * + * @param after the operation to perform after this one. + * @return a composed {@code FailableDoubleConsumer} like {@link DoubleConsumer#andThen(DoubleConsumer)}. + * @throws NullPointerException when {@code after} is null. + * @throws E Thrown when a consumer fails. + */ + default FailableDoubleConsumer<E> andThen(final FailableDoubleConsumer<E> after) throws E { + Objects.requireNonNull(after); + return (final double t) -> { + accept(t); + after.accept(t); + }; + } } diff --git a/src/main/java/org/apache/commons/lang3/function/FailableDoubleFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableDoubleFunction.java index 175a05e..6eea25f 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableDoubleFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableDoubleFunction.java @@ -29,6 +29,21 @@ import java.util.function.DoubleFunction; @FunctionalInterface public interface FailableDoubleFunction<R, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableDoubleFunction NOP = t -> null; + + /** + * Returns The NOP singleton. + * + * @param <R> Return type. + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <R, E extends Throwable> FailableDoubleFunction<R, E> nop() { + return NOP; + } + /** * Applies this function. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableDoubleToIntFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableDoubleToIntFunction.java index eb9e940..a8cef78 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableDoubleToIntFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableDoubleToIntFunction.java @@ -28,6 +28,20 @@ import java.util.function.DoubleToIntFunction; @FunctionalInterface public interface FailableDoubleToIntFunction<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableDoubleToIntFunction NOP = t -> 0; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableDoubleToIntFunction<E> nop() { + return NOP; + } + /** * Applies this function to the given argument. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableDoubleToLongFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableDoubleToLongFunction.java index 2a5b0ff..003b2be 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableDoubleToLongFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableDoubleToLongFunction.java @@ -28,6 +28,20 @@ import java.util.function.DoubleToLongFunction; @FunctionalInterface public interface FailableDoubleToLongFunction<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableDoubleToLongFunction NOP = t -> 0; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableDoubleToLongFunction<E> nop() { + return NOP; + } + /** * Applies this function to the given argument. * 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 3d4b905..53e4314 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableFunction.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.function; +import java.util.Objects; import java.util.function.Function; /** @@ -47,6 +48,20 @@ public interface FailableFunction<T, R, E extends Throwable> { } /** + * Returns a composed {@code FailableFunction} like {@link Function#andThen(Function)}. + * + * @param <V> the output type of the {@code after} function, and of the composed function. + * @return a composed {@code FailableFunction} like {@link Function#andThen(Function)}. + * @param after the operation to perform after this one. + * @throws NullPointerException when {@code after} is null. + * @throws E Thrown when a consumer fails. + */ + default <V> FailableFunction<T, V, E> andThen(final FailableFunction<? super R, ? extends V, E> after) throws E { + Objects.requireNonNull(after); + return (final T t) -> after.apply(apply(t)); + } + + /** * Applies this function. * * @param input the input for the function @@ -54,4 +69,5 @@ public interface FailableFunction<T, R, E extends Throwable> { * @throws E Thrown when the function fails. */ R apply(T input) throws E; + } diff --git a/src/main/java/org/apache/commons/lang3/function/FailableIntConsumer.java b/src/main/java/org/apache/commons/lang3/function/FailableIntConsumer.java index 506cb67..4f508b9 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableIntConsumer.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableIntConsumer.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.function; +import java.util.Objects; import java.util.function.IntConsumer; /** @@ -28,6 +29,20 @@ import java.util.function.IntConsumer; @FunctionalInterface public interface FailableIntConsumer<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableIntConsumer NOP = t -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableIntConsumer<E> nop() { + return NOP; + } + /** * Accepts the consumer. * @@ -35,4 +50,21 @@ public interface FailableIntConsumer<E extends Throwable> { * @throws E Thrown when the consumer fails. */ void accept(int value) throws E; + + /** + * Returns a composed {@code FailableIntConsumer} like {@link IntConsumer#andThen(IntConsumer)}. + * + * @param after the operation to perform after this one. + * @return a composed {@code FailableLongConsumer} like {@link IntConsumer#andThen(IntConsumer)}. + * @throws NullPointerException if {@code after} is null + * @throws E Thrown when a consumer fails. + */ + default FailableIntConsumer<E> andThen(final FailableIntConsumer<E> after) throws E { + Objects.requireNonNull(after); + return (final int t) -> { + accept(t); + after.accept(t); + }; + } + } diff --git a/src/main/java/org/apache/commons/lang3/function/FailableIntFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableIntFunction.java index 7c2d0bc..a41bcbb 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableIntFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableIntFunction.java @@ -29,6 +29,21 @@ import java.util.function.IntFunction; @FunctionalInterface public interface FailableIntFunction<R, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableIntFunction NOP = t -> null; + + /** + * Returns The NOP singleton. + * + * @param <R> Return type. + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <R, E extends Throwable> FailableIntFunction<R, E> nop() { + return NOP; + } + /** * Applies this function. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableIntToDoubleFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableIntToDoubleFunction.java index 93afd89..6f6c8f4 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableIntToDoubleFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableIntToDoubleFunction.java @@ -28,6 +28,20 @@ import java.util.function.IntToDoubleFunction; @FunctionalInterface public interface FailableIntToDoubleFunction<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableIntToDoubleFunction NOP = t -> 0d; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableIntToDoubleFunction<E> nop() { + return NOP; + } + /** * Applies this function to the given argument. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableIntToLongFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableIntToLongFunction.java index de23ae7..2afd4a2 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableIntToLongFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableIntToLongFunction.java @@ -28,6 +28,20 @@ import java.util.function.IntToLongFunction; @FunctionalInterface public interface FailableIntToLongFunction<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableIntToLongFunction NOP = t -> 0L; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableIntToLongFunction<E> nop() { + return NOP; + } + /** * Applies this function to the given argument. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableLongConsumer.java b/src/main/java/org/apache/commons/lang3/function/FailableLongConsumer.java index 0565ff3..a185ff6 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableLongConsumer.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableLongConsumer.java @@ -17,6 +17,7 @@ package org.apache.commons.lang3.function; +import java.util.Objects; import java.util.function.LongConsumer; /** @@ -28,6 +29,20 @@ import java.util.function.LongConsumer; @FunctionalInterface public interface FailableLongConsumer<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableLongConsumer NOP = t -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableLongConsumer<E> nop() { + return NOP; + } + /** * Accepts the consumer. * @@ -35,4 +50,20 @@ public interface FailableLongConsumer<E extends Throwable> { * @throws E Thrown when the consumer fails. */ void accept(long object) throws E; + + /** + * Returns a composed {@code FailableLongConsumer} like {@link LongConsumer#andThen(LongConsumer)}. + * + * @param after the operation to perform after this one. + * @return a composed {@code FailableLongConsumer} like {@link LongConsumer#andThen(LongConsumer)}. + * @throws NullPointerException if {@code after} is null + * @throws E Thrown when a consumer fails. + */ + default FailableLongConsumer<E> andThen(final FailableLongConsumer<E> after) throws E { + Objects.requireNonNull(after); + return (final long t) -> { + accept(t); + after.accept(t); + }; + } } diff --git a/src/main/java/org/apache/commons/lang3/function/FailableLongFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableLongFunction.java index 2811e74..2d3afae 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableLongFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableLongFunction.java @@ -29,6 +29,21 @@ import java.util.function.LongFunction; @FunctionalInterface public interface FailableLongFunction<R, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableLongFunction NOP = t -> null; + + /** + * Returns The NOP singleton. + * + * @param <R> Return type. + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <R, E extends Throwable> FailableLongFunction<R, E> nop() { + return NOP; + } + /** * Applies this function. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableLongToDoubleFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableLongToDoubleFunction.java index b272c9d..13451fa 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableLongToDoubleFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableLongToDoubleFunction.java @@ -28,6 +28,20 @@ import java.util.function.LongToDoubleFunction; @FunctionalInterface public interface FailableLongToDoubleFunction<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableLongToDoubleFunction NOP = t -> 0d; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableLongToDoubleFunction<E> nop() { + return NOP; + } + /** * Applies this function to the given argument. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableLongToIntFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableLongToIntFunction.java index 1066c3c..9c9b604 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableLongToIntFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableLongToIntFunction.java @@ -28,6 +28,20 @@ import java.util.function.LongToIntFunction; @FunctionalInterface public interface FailableLongToIntFunction<E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableLongToIntFunction NOP = t -> 0; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <E extends Throwable> FailableLongToIntFunction<E> nop() { + return NOP; + } + /** * Applies this function to the given argument. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableObjDoubleConsumer.java b/src/main/java/org/apache/commons/lang3/function/FailableObjDoubleConsumer.java index 105c874..866ed0e 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableObjDoubleConsumer.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableObjDoubleConsumer.java @@ -29,11 +29,25 @@ import java.util.function.ObjDoubleConsumer; @FunctionalInterface public interface FailableObjDoubleConsumer<T, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableObjDoubleConsumer NOP = (t, u) -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, E extends Throwable> FailableObjDoubleConsumer<T, E> nop() { + return NOP; + } + /** * Accepts the consumer. * * @param object the object parameter for the consumable to accept. - * @param value the double parameter for the consumable to accept. + * @param value the double parameter for the consumable to accept. * @throws E Thrown when the consumer fails. */ void accept(T object, double value) throws E; diff --git a/src/main/java/org/apache/commons/lang3/function/FailableObjIntConsumer.java b/src/main/java/org/apache/commons/lang3/function/FailableObjIntConsumer.java index 71c8925..c485c6c 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableObjIntConsumer.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableObjIntConsumer.java @@ -29,6 +29,20 @@ import java.util.function.ObjIntConsumer; @FunctionalInterface public interface FailableObjIntConsumer<T, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableObjIntConsumer NOP = (t, u) -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, E extends Throwable> FailableObjIntConsumer<T, E> nop() { + return NOP; + } + /** * Accepts the consumer. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableObjLongConsumer.java b/src/main/java/org/apache/commons/lang3/function/FailableObjLongConsumer.java index b84b4d7..bbfa2f8 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableObjLongConsumer.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableObjLongConsumer.java @@ -29,6 +29,20 @@ import java.util.function.ObjLongConsumer; @FunctionalInterface public interface FailableObjLongConsumer<T, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableObjLongConsumer NOP = (t, u) -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, E extends Throwable> FailableObjLongConsumer<T, E> nop() { + return NOP; + } + /** * Accepts the consumer. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableToDoubleBiFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableToDoubleBiFunction.java index 4ac14bd..6da46b4 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableToDoubleBiFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableToDoubleBiFunction.java @@ -30,6 +30,22 @@ import java.util.function.ToDoubleBiFunction; @FunctionalInterface public interface FailableToDoubleBiFunction<T, U, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToDoubleBiFunction NOP = (t, u) -> 0d; + + /** + * Returns The NOP singleton. + * + * @param <T> the type of the first argument to the function + * @param <U> the type of the second argument to the function + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, U, E extends Throwable> FailableToDoubleBiFunction<T, U, E> nop() { + return NOP; + } + /** * Applies this function to the given arguments. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableToDoubleFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableToDoubleFunction.java index 43feba5..2548423 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableToDoubleFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableToDoubleFunction.java @@ -22,13 +22,28 @@ import java.util.function.ToDoubleFunction; /** * A functional interface like {@link ToDoubleFunction} that declares a {@code Throwable}. * - * @param <T> the type of the first argument to the function + * @param <T> the type of the argument to the function * @param <E> Thrown exception. * @since 3.11 */ @FunctionalInterface public interface FailableToDoubleFunction<T, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToDoubleFunction NOP = t -> 0d; + + /** + * Returns The NOP singleton. + * + * @param <T> the type of the argument to the function + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, E extends Throwable> FailableToDoubleFunction<T, E> nop() { + return NOP; + } + /** * Applies this function to the given arguments. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableToIntBiFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableToIntBiFunction.java index 1b7c3e2..650a7f6 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableToIntBiFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableToIntBiFunction.java @@ -30,6 +30,22 @@ import java.util.function.ToIntBiFunction; @FunctionalInterface public interface FailableToIntBiFunction<T, U, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToIntBiFunction NOP = (t, u) -> 0; + + /** + * Returns The NOP singleton. + * + * @param <T> the type of the first argument to the function + * @param <U> the type of the second argument to the function + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, U, E extends Throwable> FailableToIntBiFunction<T, U, E> nop() { + return NOP; + } + /** * Applies this function to the given arguments. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableToIntFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableToIntFunction.java index 1bba1c9..1e51e45 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableToIntFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableToIntFunction.java @@ -22,13 +22,28 @@ import java.util.function.ToIntFunction; /** * A functional interface like {@link ToIntFunction} that declares a {@code Throwable}. * - * @param <T> the type of the first argument to the function + * @param <T> the type of the argument to the function * @param <E> Thrown exception. * @since 3.11 */ @FunctionalInterface public interface FailableToIntFunction<T, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToIntFunction NOP = t -> 0; + + /** + * Returns The NOP singleton. + * + * @param <T> the type of the argument to the function + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, E extends Throwable> FailableToIntFunction<T, E> nop() { + return NOP; + } + /** * Applies this function to the given arguments. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableToLongBiFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableToLongBiFunction.java index e49347f..62eca3c 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableToLongBiFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableToLongBiFunction.java @@ -30,6 +30,22 @@ import java.util.function.ToLongBiFunction; @FunctionalInterface public interface FailableToLongBiFunction<T, U, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToLongBiFunction NOP = (t, u) -> 0; + + /** + * Returns The NOP singleton. + * + * @param <T> the type of the first argument to the function + * @param <U> the type of the second argument to the function + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, U, E extends Throwable> FailableToLongBiFunction<T, U, E> nop() { + return NOP; + } + /** * Applies this function to the given arguments. * diff --git a/src/main/java/org/apache/commons/lang3/function/FailableToLongFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableToLongFunction.java index 5b790af..af90c93 100644 --- a/src/main/java/org/apache/commons/lang3/function/FailableToLongFunction.java +++ b/src/main/java/org/apache/commons/lang3/function/FailableToLongFunction.java @@ -29,6 +29,21 @@ import java.util.function.ToLongFunction; @FunctionalInterface public interface FailableToLongFunction<T, E extends Throwable> { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToLongFunction NOP = t -> 0L; + + /** + * Returns The NOP singleton. + * + * @param <T> the type of the argument to the function + * @param <E> Thrown exception. + * @return The NOP singleton. + */ + static <T, E extends Throwable> FailableToLongFunction<T, E> nop() { + return NOP; + } + /** * Applies this function to the given arguments. * 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 24a1054..da36ebd 100644 --- a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java @@ -66,6 +66,7 @@ public class FailableFunctionsTest { } } } + public static class FailureOnOddInvocations { private static int invocations; @@ -629,18 +630,19 @@ public class FailableFunctionsTest { @Test public void testBiConsumerAndThen() throws Throwable { final Testable<?, ?> testable = new Testable<>(null); - final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> failableBiConsumer = (t, th) -> { + final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> failing = (t, th) -> { t.setThrowable(th); t.test(); }; final FailableBiConsumer<Testable<?, ?>, Throwable, Throwable> nop = FailableBiConsumer.nop(); - final Throwable e = assertThrows(OutOfMemoryError.class, - () -> nop.andThen(failableBiConsumer).accept(testable, ERROR)); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).accept(testable, ERROR)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).accept(testable, ERROR)); assertSame(ERROR, e); // Does not throw nop.andThen(nop); // Documented in Javadoc edge-case. - assertThrows(NullPointerException.class, () -> failableBiConsumer.andThen(null)); + assertThrows(NullPointerException.class, () -> failing.andThen(null)); } @Test @@ -680,7 +682,8 @@ public class FailableFunctionsTest { throw new IOException(); }; final FailableFunction<Object, Integer, IOException> failingFunction = (t) -> { throw new IOException(); }; - final FailableBiFunction<Object, Integer, Integer, IOException> nopFailableBiFunction = FailableBiFunction.nop(); + final FailableBiFunction<Object, Integer, Integer, IOException> nopFailableBiFunction = FailableBiFunction + .nop(); final FailableFunction<Object, Integer, IOException> nopFailableFunction = FailableFunction.nop(); // assertThrows(IOException.class, () -> failingBiFunctionTest.andThen(failingFunction).apply(null, null)); @@ -737,8 +740,7 @@ public class FailableFunctionsTest { testable.test(); }; final FailableConsumer<Throwable, Throwable> nop = FailableConsumer.nop(); - final Throwable e = assertThrows(OutOfMemoryError.class, - () -> nop.andThen(failableConsumer).accept(ERROR)); + final Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failableConsumer).accept(ERROR)); assertSame(ERROR, e); // Does not throw nop.andThen(nop); @@ -747,6 +749,24 @@ public class FailableFunctionsTest { } @Test + public void testDoubleConsumerAndThen() throws Throwable { + final Testable<?, ?> testable = new Testable<>(null); + final FailableDoubleConsumer<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + }; + final FailableDoubleConsumer<Throwable> nop = FailableDoubleConsumer.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).accept(0d)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).accept(0d)); + assertSame(ERROR, e); + // Does not throw + nop.andThen(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.andThen(null)); + } + + @Test public void testDoublePredicate() throws Throwable { FailureOnOddInvocations.invocations = 0; final FailableDoublePredicate<Throwable> failablePredicate = t1 -> FailureOnOddInvocations.testDouble(t1); @@ -780,6 +800,24 @@ public class FailableFunctionsTest { } @Test + public void testFunctionAndThen() throws IOException { + // Unchecked usage pattern in JRE + final Function<Object, Integer> nopFunction = (t) -> null; + nopFunction.andThen(nopFunction); + // Checked usage pattern + final FailableFunction<Object, Integer, IOException> failingFunction = (t) -> { throw new IOException(); }; + final FailableFunction<Object, Integer, IOException> nopFailableFunction = FailableFunction.nop(); + // + assertThrows(IOException.class, () -> failingFunction.andThen(failingFunction).apply(null)); + assertThrows(IOException.class, () -> failingFunction.andThen(nopFailableFunction).apply(null)); + // + assertThrows(IOException.class, () -> nopFailableFunction.andThen(failingFunction).apply(null)); + nopFailableFunction.andThen(nopFailableFunction).apply(null); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failingFunction.andThen(null)); + } + + @Test public void testGetAsBooleanSupplier() { final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION); Throwable e = assertThrows(IllegalStateException.class, @@ -905,6 +943,24 @@ public class FailableFunctionsTest { } @Test + public void testIntConsumerAndThen() throws Throwable { + final Testable<?, ?> testable = new Testable<>(null); + final FailableIntConsumer<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + }; + final FailableIntConsumer<Throwable> nop = FailableIntConsumer.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).accept(0)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).accept(0)); + assertSame(ERROR, e); + // Does not throw + nop.andThen(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.andThen(null)); + } + + @Test public void testIntPredicate() throws Throwable { FailureOnOddInvocations.invocations = 0; final FailableIntPredicate<Throwable> failablePredicate = t1 -> FailureOnOddInvocations.testInt(t1); @@ -913,6 +969,24 @@ public class FailableFunctionsTest { } @Test + public void testLongConsumerAndThen() throws Throwable { + final Testable<?, ?> testable = new Testable<>(null); + final FailableLongConsumer<Throwable> failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + }; + final FailableLongConsumer<Throwable> nop = FailableLongConsumer.nop(); + Throwable e = assertThrows(OutOfMemoryError.class, () -> nop.andThen(failing).accept(0L)); + assertSame(ERROR, e); + e = assertThrows(OutOfMemoryError.class, () -> failing.andThen(nop).accept(0L)); + assertSame(ERROR, e); + // Does not throw + nop.andThen(nop); + // Documented in Javadoc edge-case. + assertThrows(NullPointerException.class, () -> failing.andThen(null)); + } + + @Test public void testLongPredicate() throws Throwable { FailureOnOddInvocations.invocations = 0; final FailableLongPredicate<Throwable> failablePredicate = t1 -> FailureOnOddInvocations.testLong(t1);