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 36111ba  [LANG-1568] More failable functional interfaces to match JRE 
functional interfaces.
36111ba is described below

commit 36111ba5829bdd4249c7418c1aec17c149ef86a3
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Jun 17 09:30:21 2020 -0400

    [LANG-1568] More failable functional interfaces to match JRE functional
    interfaces.
---
 .../java/org/apache/commons/lang3/Functions.java   | 76 +++++++++++++++++++---
 .../org/apache/commons/lang3/FunctionsTest.java    | 45 ++++++++++++-
 2 files changed, 112 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/Functions.java 
b/src/main/java/org/apache/commons/lang3/Functions.java
index 0eeef44..16e7a0c 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;
     }
@@ -271,6 +274,25 @@ public class Functions {
     }
 
     /**
+     * A functional interface like {@link DoublePredicate} that declares a 
{@code Throwable}.
+     *
+     * @param <T> Thrown exception.
+     * @since 3.11
+     */
+    @FunctionalInterface
+    public interface FailableDoublePredicate<T extends Throwable> {
+
+        /**
+         * 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}.
      *
      * @param <T> Thrown exception.
@@ -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;
     }
@@ -404,6 +426,25 @@ public class Functions {
     }
 
     /**
+     * A functional interface like {@link IntPredicate} that declares a {@code 
Throwable}.
+     *
+     * @param <T> Thrown exception.
+     * @since 3.11
+     */
+    @FunctionalInterface
+    public interface FailableIntPredicate<T extends Throwable> {
+
+        /**
+         * 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}.
      *
      * @param <T> Thrown exception.
@@ -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;
     }
@@ -517,6 +558,25 @@ public class Functions {
     }
 
     /**
+     * A functional interface like {@link LongPredicate} that declares a 
{@code Throwable}.
+     *
+     * @param <T> Thrown exception.
+     * @since 3.11
+     */
+    @FunctionalInterface
+    public interface FailableLongPredicate<T extends Throwable> {
+
+        /**
+         * 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}.
      *
      * @param <T> Thrown exception.
@@ -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 d114224..36ed6ca 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,11 +704,21 @@ 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<Throwable> failablePredicate = 
t1 -> FailureOnOddInvocations
+            .testDouble(t1);
+        assertThrows(SomeException.class, () -> failablePredicate.test(1d));
+        failablePredicate.test(1d);
+    }
+
+    @Test
     public void testFunction() {
         final IllegalStateException ise = new IllegalStateException();
         final Testable<?, ?> testable = new Testable<>(ise);
@@ -856,6 +881,24 @@ class FunctionsTest {
     }
 
     @Test
+    public void testIntPredicate() throws Throwable {
+        FailureOnOddInvocations.invocations = 0;
+        final Functions.FailableIntPredicate<Throwable> 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<Throwable> 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() {
         FailureOnOddInvocations.invocations = 0;

Reply via email to