From d4ccd259d95ed8f1e2a7c2526cdec64692ca0ac9 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 24 Jun 2020 20:10:46 -0400 Subject: [PATCH] [LANG-1568] More checked andThen() implementations to mirror the JRE functional interfaces. --- .../lang3/function/FailableBiConsumer.java | 2 +- .../lang3/function/FailableBiFunction.java | 4 +- .../lang3/function/FailableConsumer.java | 2 +- .../function/FailableDoubleConsumer.java | 31 +++++++ .../function/FailableDoubleFunction.java | 15 ++++ .../function/FailableDoubleToIntFunction.java | 14 +++ .../FailableDoubleToLongFunction.java | 14 +++ .../lang3/function/FailableFunction.java | 16 ++++ .../lang3/function/FailableIntConsumer.java | 32 +++++++ .../lang3/function/FailableIntFunction.java | 15 ++++ .../function/FailableIntToDoubleFunction.java | 14 +++ .../function/FailableIntToLongFunction.java | 14 +++ .../lang3/function/FailableLongConsumer.java | 31 +++++++ .../lang3/function/FailableLongFunction.java | 15 ++++ .../FailableLongToDoubleFunction.java | 14 +++ .../function/FailableLongToIntFunction.java | 14 +++ .../function/FailableObjDoubleConsumer.java | 16 +++- .../function/FailableObjIntConsumer.java | 14 +++ .../function/FailableObjLongConsumer.java | 14 +++ .../function/FailableToDoubleBiFunction.java | 16 ++++ .../function/FailableToDoubleFunction.java | 17 +++- .../function/FailableToIntBiFunction.java | 16 ++++ .../lang3/function/FailableToIntFunction.java | 17 +++- .../function/FailableToLongBiFunction.java | 16 ++++ .../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 6d5d99f5f..10c6d808b 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 @@ static FailableBiConsumer nop() { * * @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 andThen(final FailableBiConsumer 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 3173c73b7..f6dc750d9 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 @@ static FailableBiFunction nop() { /** * Returns a composed {@code FailableBiFunction} that like {@link BiFunction#andThen(Function)}. * - * @param the type of output of the {@code after} function, and of the composed function + * @param 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 FailableBiFunction andThen(final FailableFunction 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 c2f2de8a9..97b7e4fdb 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 @@ static FailableConsumer nop() { * * @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 andThen(final FailableConsumer 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 9b70da82a..f91a23bd7 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 @@ @FunctionalInterface public interface FailableDoubleConsumer { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableDoubleConsumer NOP = t -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableDoubleConsumer nop() { + return NOP; + } + /** * Accepts the consumer. * @@ -35,4 +50,20 @@ public interface FailableDoubleConsumer { * @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 andThen(final FailableDoubleConsumer 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 175a05e85..6eea25f71 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 @@ @FunctionalInterface public interface FailableDoubleFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableDoubleFunction NOP = t -> null; + + /** + * Returns The NOP singleton. + * + * @param Return type. + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableDoubleFunction 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 eb9e9405f..a8cef785b 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 @@ @FunctionalInterface public interface FailableDoubleToIntFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableDoubleToIntFunction NOP = t -> 0; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableDoubleToIntFunction 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 2a5b0ff15..003b2be65 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 @@ @FunctionalInterface public interface FailableDoubleToLongFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableDoubleToLongFunction NOP = t -> 0; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableDoubleToLongFunction 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 3d4b905b6..53e4314ca 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; /** @@ -46,6 +47,20 @@ static FailableFunction nop() { return NOP; } + /** + * Returns a composed {@code FailableFunction} like {@link Function#andThen(Function)}. + * + * @param 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 FailableFunction andThen(final FailableFunction after) throws E { + Objects.requireNonNull(after); + return (final T t) -> after.apply(apply(t)); + } + /** * Applies this function. * @@ -54,4 +69,5 @@ static FailableFunction nop() { * @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 506cb67e6..4f508b9c2 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 @@ @FunctionalInterface public interface FailableIntConsumer { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableIntConsumer NOP = t -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableIntConsumer nop() { + return NOP; + } + /** * Accepts the consumer. * @@ -35,4 +50,21 @@ public interface FailableIntConsumer { * @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 andThen(final FailableIntConsumer 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 7c2d0bcab..a41bcbbd5 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 @@ @FunctionalInterface public interface FailableIntFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableIntFunction NOP = t -> null; + + /** + * Returns The NOP singleton. + * + * @param Return type. + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableIntFunction 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 93afd8979..6f6c8f455 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 @@ @FunctionalInterface public interface FailableIntToDoubleFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableIntToDoubleFunction NOP = t -> 0d; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableIntToDoubleFunction 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 de23ae738..2afd4a2ce 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 @@ @FunctionalInterface public interface FailableIntToLongFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableIntToLongFunction NOP = t -> 0L; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableIntToLongFunction 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 0565ff3b7..a185ff6b5 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 @@ @FunctionalInterface public interface FailableLongConsumer { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableLongConsumer NOP = t -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableLongConsumer nop() { + return NOP; + } + /** * Accepts the consumer. * @@ -35,4 +50,20 @@ public interface FailableLongConsumer { * @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 andThen(final FailableLongConsumer 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 2811e7411..2d3afae93 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 @@ @FunctionalInterface public interface FailableLongFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableLongFunction NOP = t -> null; + + /** + * Returns The NOP singleton. + * + * @param Return type. + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableLongFunction 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 b272c9d0a..13451fa09 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 @@ @FunctionalInterface public interface FailableLongToDoubleFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableLongToDoubleFunction NOP = t -> 0d; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableLongToDoubleFunction 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 1066c3c2c..9c9b60418 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 @@ @FunctionalInterface public interface FailableLongToIntFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableLongToIntFunction NOP = t -> 0; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableLongToIntFunction 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 105c87462..866ed0ed4 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 @@ @FunctionalInterface public interface FailableObjDoubleConsumer { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableObjDoubleConsumer NOP = (t, u) -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableObjDoubleConsumer 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 71c89252a..c485c6ceb 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 @@ @FunctionalInterface public interface FailableObjIntConsumer { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableObjIntConsumer NOP = (t, u) -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableObjIntConsumer 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 b84b4d7a3..bbfa2f8ea 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 @@ @FunctionalInterface public interface FailableObjLongConsumer { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableObjLongConsumer NOP = (t, u) -> {/* NOP */}; + + /** + * Returns The NOP singleton. + * + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableObjLongConsumer 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 4ac14bd57..6da46b46c 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 @@ @FunctionalInterface public interface FailableToDoubleBiFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToDoubleBiFunction NOP = (t, u) -> 0d; + + /** + * Returns The NOP singleton. + * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableToDoubleBiFunction 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 43feba590..254842375 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 @@ /** * A functional interface like {@link ToDoubleFunction} that declares a {@code Throwable}. * - * @param the type of the first argument to the function + * @param the type of the argument to the function * @param Thrown exception. * @since 3.11 */ @FunctionalInterface public interface FailableToDoubleFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToDoubleFunction NOP = t -> 0d; + + /** + * Returns The NOP singleton. + * + * @param the type of the argument to the function + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableToDoubleFunction 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 1b7c3e2df..650a7f6fe 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 @@ @FunctionalInterface public interface FailableToIntBiFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToIntBiFunction NOP = (t, u) -> 0; + + /** + * Returns The NOP singleton. + * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableToIntBiFunction 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 1bba1c922..1e51e45ae 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 @@ /** * A functional interface like {@link ToIntFunction} that declares a {@code Throwable}. * - * @param the type of the first argument to the function + * @param the type of the argument to the function * @param Thrown exception. * @since 3.11 */ @FunctionalInterface public interface FailableToIntFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToIntFunction NOP = t -> 0; + + /** + * Returns The NOP singleton. + * + * @param the type of the argument to the function + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableToIntFunction 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 e49347ffc..62eca3c16 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 @@ @FunctionalInterface public interface FailableToLongBiFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToLongBiFunction NOP = (t, u) -> 0; + + /** + * Returns The NOP singleton. + * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableToLongBiFunction 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 5b790af5b..af90c9372 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 @@ @FunctionalInterface public interface FailableToLongFunction { + /** NOP singleton */ + @SuppressWarnings("rawtypes") + final FailableToLongFunction NOP = t -> 0L; + + /** + * Returns The NOP singleton. + * + * @param the type of the argument to the function + * @param Thrown exception. + * @return The NOP singleton. + */ + static FailableToLongFunction 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 24a1054cd..da36ebda7 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 void run(final Throwable pTh) throws Throwable { } } } + public static class FailureOnOddInvocations { private static int invocations; @@ -629,18 +630,19 @@ public void testBiConsumer() throws Throwable { @Test public void testBiConsumerAndThen() throws Throwable { final Testable testable = new Testable<>(null); - final FailableBiConsumer, Throwable, Throwable> failableBiConsumer = (t, th) -> { + final FailableBiConsumer, Throwable, Throwable> failing = (t, th) -> { t.setThrowable(th); t.test(); }; final FailableBiConsumer, 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 void testBiFunctionAndThen() throws IOException { throw new IOException(); }; final FailableFunction failingFunction = (t) -> { throw new IOException(); }; - final FailableBiFunction nopFailableBiFunction = FailableBiFunction.nop(); + final FailableBiFunction nopFailableBiFunction = FailableBiFunction + .nop(); final FailableFunction nopFailableFunction = FailableFunction.nop(); // assertThrows(IOException.class, () -> failingBiFunctionTest.andThen(failingFunction).apply(null, null)); @@ -737,8 +740,7 @@ public void testConsumerAndThen() throws Throwable { testable.test(); }; final FailableConsumer 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); @@ -746,6 +748,24 @@ public void testConsumerAndThen() throws Throwable { assertThrows(NullPointerException.class, () -> failableConsumer.andThen(null)); } + @Test + public void testDoubleConsumerAndThen() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableDoubleConsumer failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + }; + final FailableDoubleConsumer 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; @@ -779,6 +799,24 @@ public void testFunction() { assertEquals(0, function.apply(null).intValue()); } + @Test + public void testFunctionAndThen() throws IOException { + // Unchecked usage pattern in JRE + final Function nopFunction = (t) -> null; + nopFunction.andThen(nopFunction); + // Checked usage pattern + final FailableFunction failingFunction = (t) -> { throw new IOException(); }; + final FailableFunction 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); @@ -904,6 +942,24 @@ public void testGetSupplier() { assertEquals(0, i.intValue()); } + @Test + public void testIntConsumerAndThen() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableIntConsumer failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + }; + final FailableIntConsumer 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; @@ -912,6 +968,24 @@ public void testIntPredicate() throws Throwable { failablePredicate.test(1); } + @Test + public void testLongConsumerAndThen() throws Throwable { + final Testable testable = new Testable<>(null); + final FailableLongConsumer failing = t -> { + testable.setThrowable(ERROR); + testable.test(); + }; + final FailableLongConsumer 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;