You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/10/12 19:12:26 UTC

[commons-lang] branch master updated: Code refactor to simplify Functions and new tests (#463)

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 a32c188  Code refactor to simplify Functions and new tests (#463)
a32c188 is described below

commit a32c188c32fd0c4a8149bcda524d704dd609da6e
Author: Peter Verhas <pe...@verhas.com>
AuthorDate: Sat Oct 12 21:12:17 2019 +0200

    Code refactor to simplify Functions and new tests (#463)
    
    * more tests
    
    * formatting change as per requested by checkstyle
    
    * DisplayName was added to the tests
    
    * throw on odd refactored to eliminate copy paste
---
 .../java/org/apache/commons/lang3/Functions.java   | 72 +++-------------------
 .../org/apache/commons/lang3/FunctionsTest.java    | 63 +++++++++++++++----
 2 files changed, 60 insertions(+), 75 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/Functions.java b/src/main/java/org/apache/commons/lang3/Functions.java
index 058568c..74efd9b 100644
--- a/src/main/java/org/apache/commons/lang3/Functions.java
+++ b/src/main/java/org/apache/commons/lang3/Functions.java
@@ -158,13 +158,7 @@ public class Functions {
      * @return a standard {@code Runnable}
      */
     public static Runnable asRunnable(FailableRunnable<?> pRunnable) {
-        return () -> {
-            try {
-                pRunnable.run();
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return () -> run(pRunnable);
     }
 
     /**
@@ -175,13 +169,7 @@ public class Functions {
      * @return a standard {@code Consumer}
      */
     public static <I> Consumer<I> asConsumer(FailableConsumer<I, ?> pConsumer) {
-        return (pInput) -> {
-            try {
-                pConsumer.accept(pInput);
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return (pInput) -> accept(pConsumer, pInput);
     }
 
     /**
@@ -192,13 +180,7 @@ public class Functions {
      * @return a standard {@code Callable}
      */
     public static <O> Callable<O> asCallable(FailableCallable<O, ?> pCallable) {
-        return () -> {
-            try {
-                return pCallable.call();
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return () -> call(pCallable);
     }
 
     /**
@@ -210,13 +192,7 @@ public class Functions {
      * @return a standard {@code BiConsumer}
      */
     public static <I1, I2> BiConsumer<I1, I2> asBiConsumer(FailableBiConsumer<I1, I2, ?> pConsumer) {
-        return (pInput1, pInput2) -> {
-            try {
-                pConsumer.accept(pInput1, pInput2);
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return (pInput1, pInput2) -> accept(pConsumer, pInput1, pInput2);
     }
 
     /**
@@ -228,13 +204,7 @@ public class Functions {
      * @return a standard {@code Function}
      */
     public static <I, O> Function<I, O> asFunction(FailableFunction<I, O, ?> pFunction) {
-        return (pInput) -> {
-            try {
-                return pFunction.apply(pInput);
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return (pInput) -> apply(pFunction, pInput);
     }
 
     /**
@@ -247,13 +217,7 @@ public class Functions {
      * @return a standard {@code BiFunction}
      */
     public static <I1, I2, O> BiFunction<I1, I2, O> asBiFunction(FailableBiFunction<I1, I2, O, ?> pFunction) {
-        return (pInput1, pInput2) -> {
-            try {
-                return pFunction.apply(pInput1, pInput2);
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return (pInput1, pInput2) -> apply(pFunction, pInput1, pInput2);
     }
 
     /**
@@ -264,13 +228,7 @@ public class Functions {
      * @return a standard {@code Predicate}
      */
     public static <I> Predicate<I> asPredicate(FailablePredicate<I, ?> pPredicate) {
-        return (pInput) -> {
-            try {
-                return pPredicate.test(pInput);
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return (pInput) -> test(pPredicate, pInput);
     }
 
     /**
@@ -282,13 +240,7 @@ public class Functions {
      * @return a standard {@code BiPredicate}
      */
     public static <I1, I2> BiPredicate<I1, I2> asBiPredicate(FailableBiPredicate<I1, I2, ?> pPredicate) {
-        return (pInput1, pInput2) -> {
-            try {
-                return pPredicate.test(pInput1, pInput2);
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return (pInput1, pInput2) -> test(pPredicate, pInput1, pInput2);
     }
 
     /**
@@ -299,13 +251,7 @@ public class Functions {
      * @return a standard {@code Supplier}
      */
     public static <O> Supplier<O> asSupplier(FailableSupplier<O, ?> pSupplier) {
-        return () -> {
-            try {
-                return pSupplier.get();
-            } catch (Throwable t) {
-                throw rethrow(t);
-            }
-        };
+        return () -> get(pSupplier);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/lang3/FunctionsTest.java b/src/test/java/org/apache/commons/lang3/FunctionsTest.java
index 9a45122..ae6bc69 100644
--- a/src/test/java/org/apache/commons/lang3/FunctionsTest.java
+++ b/src/test/java/org/apache/commons/lang3/FunctionsTest.java
@@ -16,24 +16,27 @@
  */
 package org.apache.commons.lang3;
 
+import org.apache.commons.lang3.Functions.FailableBiConsumer;
+import org.apache.commons.lang3.Functions.FailableBiFunction;
+import org.apache.commons.lang3.Functions.FailableCallable;
+import org.apache.commons.lang3.Functions.FailableConsumer;
+import org.apache.commons.lang3.Functions.FailableFunction;
+import org.apache.commons.lang3.Functions.FailableSupplier;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
 import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.lang.reflect.UndeclaredThrowableException;
 import java.util.concurrent.Callable;
 import java.util.function.BiConsumer;
 import java.util.function.BiFunction;
+import java.util.function.BiPredicate;
 import java.util.function.Consumer;
 import java.util.function.Function;
+import java.util.function.Predicate;
 import java.util.function.Supplier;
 
-import org.apache.commons.lang3.Functions.FailableBiConsumer;
-import org.apache.commons.lang3.Functions.FailableBiFunction;
-import org.apache.commons.lang3.Functions.FailableCallable;
-import org.apache.commons.lang3.Functions.FailableConsumer;
-import org.apache.commons.lang3.Functions.FailableFunction;
-import org.apache.commons.lang3.Functions.FailableSupplier;
-import org.junit.jupiter.api.Test;
-
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertSame;
@@ -95,12 +98,20 @@ class FunctionsTest {
 
     public static class FailureOnOddInvocations {
         private static int invocation;
-        FailureOnOddInvocations() throws SomeException {
+
+        private static void throwOnOdd() throws SomeException {
             final int i = ++invocation;
             if (i % 2 == 1) {
                 throw new SomeException("Odd Invocation: " + i);
             }
         }
+        static boolean failingBool() throws SomeException {
+            throwOnOdd();
+            return true;
+        }
+        FailureOnOddInvocations() throws SomeException {
+            throwOnOdd();
+        }
     }
 
     public static class CloseableObject {
@@ -393,11 +404,39 @@ class FunctionsTest {
     }
 
     @Test
+    @DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
+    public void testAsPredicate() {
+        FailureOnOddInvocations.invocation = 0;
+        final Functions.FailablePredicate<Object, Throwable> failablePredicate = (t) -> FailureOnOddInvocations.failingBool();
+        final Predicate<?> predicate = Functions.asPredicate(failablePredicate);
+        UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
+        final Throwable cause = e.getCause();
+        assertNotNull(cause);
+        assertTrue(cause instanceof SomeException);
+        assertEquals("Odd Invocation: 1", cause.getMessage());
+        final boolean instance = predicate.test(null);
+        assertNotNull(instance);
+    }
+
+    @Test
+    @DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ")
+    public void testAsBiPredicate() {
+        FailureOnOddInvocations.invocation = 0;
+        final Functions.FailableBiPredicate<Object, Object, Throwable> failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool();
+        final BiPredicate<?, ?> predicate = Functions.asBiPredicate(failableBiPredicate);
+        UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null, null));
+        final Throwable cause = e.getCause();
+        assertNotNull(cause);
+        assertTrue(cause instanceof SomeException);
+        assertEquals("Odd Invocation: 1", cause.getMessage());
+        final boolean instance = predicate.test(null, null);
+        assertNotNull(instance);
+    }
+
+    @Test
     public void testAsSupplier() {
         FailureOnOddInvocations.invocation = 0;
-        final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = () -> {
-            return new FailureOnOddInvocations();
-        };
+        final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = () -> new FailureOnOddInvocations();
         final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier);
         UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () ->  supplier.get());
         final Throwable cause = e.getCause();