diff --git a/src/main/java/org/apache/commons/lang3/Functions.java b/src/main/java/org/apache/commons/lang3/Functions.java index 0eeef4473..16e7a0c10 100644 --- a/src/main/java/org/apache/commons/lang3/Functions.java +++ b/src/main/java/org/apache/commons/lang3/Functions.java @@ -30,6 +30,7 @@ import java.util.function.Consumer; import java.util.function.DoubleBinaryOperator; import java.util.function.DoubleConsumer; import java.util.function.DoubleFunction; +import java.util.function.DoublePredicate; import java.util.function.DoubleSupplier; import java.util.function.DoubleToIntFunction; import java.util.function.DoubleToLongFunction; @@ -37,12 +38,14 @@ import java.util.function.Function; import java.util.function.IntBinaryOperator; import java.util.function.IntConsumer; import java.util.function.IntFunction; +import java.util.function.IntPredicate; import java.util.function.IntSupplier; import java.util.function.IntToDoubleFunction; import java.util.function.IntToLongFunction; import java.util.function.LongBinaryOperator; import java.util.function.LongConsumer; import java.util.function.LongFunction; +import java.util.function.LongPredicate; import java.util.function.LongSupplier; import java.util.function.LongToDoubleFunction; import java.util.function.LongToIntFunction; @@ -111,7 +114,7 @@ public class Functions { * * @param object1 the first parameter for the consumable to accept * @param object2 the second parameter for the consumable to accept - * @throws T if the consumer fails + * @throws T Thrown when the consumer fails. */ void accept(O1 object1, O2 object2) throws T; } @@ -208,7 +211,7 @@ public class Functions { * Accepts the consumer. * * @param object the parameter for the consumable to accept - * @throws T if the consumer fails + * @throws T Thrown when the consumer fails. */ void accept(O object) throws T; } @@ -246,7 +249,7 @@ public class Functions { * Accepts the consumer. * * @param value the parameter for the consumable to accept - * @throws T if the consumer fails + * @throws T Thrown when the consumer fails. */ void accept(double value) throws T; } @@ -270,6 +273,25 @@ public class Functions { R apply(double input) throws T; } + /** + * A functional interface like {@link DoublePredicate} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableDoublePredicate { + + /** + * Tests the predicate. + * + * @param value the parameter for the predicate to accept. + * @return {@code true} if the input argument matches the predicate, {@code false} otherwise. + * @throws T Thrown when the consumer fails. + */ + boolean test(double value) throws T; + } + /** * A functional interface like {@link DoubleSupplier} that declares a {@code Throwable}. * @@ -379,7 +401,7 @@ public class Functions { * Accepts the consumer. * * @param value the parameter for the consumable to accept - * @throws T if the consumer fails + * @throws T Thrown when the consumer fails. */ void accept(int value) throws T; } @@ -403,6 +425,25 @@ public class Functions { R apply(int input) throws T; } + /** + * A functional interface like {@link IntPredicate} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableIntPredicate { + + /** + * Tests the predicate. + * + * @param value the parameter for the predicate to accept. + * @return {@code true} if the input argument matches the predicate, {@code false} otherwise. + * @throws T Thrown when the consumer fails. + */ + boolean test(int value) throws T; + } + /** * A functional interface like {@link IntSupplier} that declares a {@code Throwable}. * @@ -492,7 +533,7 @@ public class Functions { * Accepts the consumer. * * @param object the parameter for the consumable to accept - * @throws T if the consumer fails + * @throws T Thrown when the consumer fails. */ void accept(long object) throws T; } @@ -516,6 +557,25 @@ public class Functions { R apply(long input) throws T; } + /** + * A functional interface like {@link LongPredicate} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableLongPredicate { + + /** + * Tests the predicate. + * + * @param value the parameter for the predicate to accept. + * @return {@code true} if the input argument matches the predicate, {@code false} otherwise. + * @throws T Thrown when the consumer fails. + */ + boolean test(long value) throws T; + } + /** * A functional interface like {@link LongSupplier} that declares a {@code Throwable}. * @@ -587,7 +647,7 @@ public class Functions { * * @param object the object parameter for the consumable to accept. * @param value the double parameter for the consumable to accept. - * @throws T if the consumer fails + * @throws T Thrown when the consumer fails. */ void accept(O object, double value) throws T; } @@ -607,7 +667,7 @@ public class Functions { * * @param object the object parameter for the consumable to accept. * @param value the int parameter for the consumable to accept. - * @throws T if the consumer fails + * @throws T Thrown when the consumer fails. */ void accept(O object, int value) throws T; } @@ -627,7 +687,7 @@ public class Functions { * * @param object the object parameter for the consumable to accept. * @param value the long parameter for the consumable to accept. - * @throws T if the consumer fails + * @throws T Thrown when the consumer fails. */ void accept(O object, long value) throws T; } diff --git a/src/test/java/org/apache/commons/lang3/FunctionsTest.java b/src/test/java/org/apache/commons/lang3/FunctionsTest.java index d114224f6..36ed6ca35 100644 --- a/src/test/java/org/apache/commons/lang3/FunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/FunctionsTest.java @@ -77,6 +77,21 @@ class FunctionsTest { return true; } + static boolean testDouble(double value) throws SomeException { + throwOnOdd(); + return true; + } + + static boolean testInt(int value) throws SomeException { + throwOnOdd(); + return true; + } + + static boolean testLong(long value) throws SomeException { + throwOnOdd(); + return true; + } + private static void throwOnOdd() throws SomeException { final int i = ++invocations; if (i % 2 == 1) { @@ -689,10 +704,20 @@ class FunctionsTest { @Test public void testConstructor() { - // We allow this, which must be an omission to make the ctor private. + // We allow this, which must have been an omission to make the ctor private. + // We could make the ctor private in 4.0. new Functions(); } + @Test + public void testDoublePredicate() throws Throwable { + FailureOnOddInvocations.invocations = 0; + final Functions.FailableDoublePredicate failablePredicate = t1 -> FailureOnOddInvocations + .testDouble(t1); + assertThrows(SomeException.class, () -> failablePredicate.test(1d)); + failablePredicate.test(1d); + } + @Test public void testFunction() { final IllegalStateException ise = new IllegalStateException(); @@ -855,6 +880,24 @@ class FunctionsTest { assertEquals(0, i.intValue()); } + @Test + public void testIntPredicate() throws Throwable { + FailureOnOddInvocations.invocations = 0; + final Functions.FailableIntPredicate failablePredicate = t1 -> FailureOnOddInvocations + .testInt(t1); + assertThrows(SomeException.class, () -> failablePredicate.test(1)); + failablePredicate.test(1); + } + + @Test + public void testLongPredicate() throws Throwable { + FailureOnOddInvocations.invocations = 0; + final Functions.FailableLongPredicate failablePredicate = t1 -> FailureOnOddInvocations + .testLong(t1); + assertThrows(SomeException.class, () -> failablePredicate.test(1l)); + failablePredicate.test(1l); + } + @Test @DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ") public void testPredicate() {