diff --git a/src/main/java/org/apache/commons/lang3/Functions.java b/src/main/java/org/apache/commons/lang3/Functions.java index 058568c7f..74efd9bb8 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 Consumer asConsumer(FailableConsumer 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 Callable asCallable(FailableCallable 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 BiConsumer asBiConsumer(FailableBiConsumer 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 Function asFunction(FailableFunction 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 BiFunction asBiFunction(FailableBiFunction 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 Predicate asPredicate(FailablePredicate 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 BiPredicate asBiPredicate(FailableBiPredicate 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 Supplier asSupplier(FailableSupplier 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 9a451221a..ae6bc69d5 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 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.Consumer; -import java.util.function.Function; -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.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 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 { @@ -392,12 +403,40 @@ class FunctionsTest { assertNotNull(instance); } + @Test + @DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ") + public void testAsPredicate() { + FailureOnOddInvocations.invocation = 0; + final Functions.FailablePredicate 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 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 failableSupplier = () -> { - return new FailureOnOddInvocations(); - }; + final FailableSupplier failableSupplier = () -> new FailureOnOddInvocations(); final Supplier supplier = Functions.asSupplier(failableSupplier); UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get()); final Throwable cause = e.getCause();