From 7f90306f909e6dba46be275c0f492c8de353b492 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 14 Jun 2020 19:27:23 -0400 Subject: [PATCH] [LANG-1568] More failable functional interfaces to match JRE functional interfaces. --- .../org/apache/commons/lang3/Functions.java | 476 ++++- .../apache/commons/lang3/FunctionsTest.java | 1616 ++++++++++++++--- 2 files changed, 1825 insertions(+), 267 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/Functions.java b/src/main/java/org/apache/commons/lang3/Functions.java index 0f701f498..0eeef4473 100644 --- a/src/main/java/org/apache/commons/lang3/Functions.java +++ b/src/main/java/org/apache/commons/lang3/Functions.java @@ -27,18 +27,36 @@ import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.BooleanSupplier; import java.util.function.Consumer; +import java.util.function.DoubleBinaryOperator; import java.util.function.DoubleConsumer; +import java.util.function.DoubleFunction; import java.util.function.DoubleSupplier; +import java.util.function.DoubleToIntFunction; +import java.util.function.DoubleToLongFunction; import java.util.function.Function; +import java.util.function.IntBinaryOperator; import java.util.function.IntConsumer; +import java.util.function.IntFunction; 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.LongSupplier; +import java.util.function.LongToDoubleFunction; +import java.util.function.LongToIntFunction; import java.util.function.ObjDoubleConsumer; import java.util.function.ObjIntConsumer; import java.util.function.ObjLongConsumer; import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.function.ToDoubleBiFunction; +import java.util.function.ToDoubleFunction; +import java.util.function.ToIntBiFunction; +import java.util.function.ToIntFunction; +import java.util.function.ToLongBiFunction; +import java.util.function.ToLongFunction; import java.util.stream.Stream; import org.apache.commons.lang3.Streams.FailableStream; @@ -79,7 +97,7 @@ import org.apache.commons.lang3.Streams.FailableStream; public class Functions { /** - * A functional interface like {@link BiConsumer} that declares a Throwable. + * A functional interface like {@link BiConsumer} that declares a {@code Throwable}. * * @param Consumed type 1. * @param Consumed type 2. @@ -99,15 +117,15 @@ public class Functions { } /** - * A functional interface like {@link BiFunction} that declares a Throwable. + * A functional interface like {@link BiFunction} that declares a {@code Throwable}. * - * @param Input type 1. - * @param Input type 2. + * @param Input type 1. + * @param Input type 2. * @param Return type. * @param Thrown exception. */ @FunctionalInterface - public interface FailableBiFunction { + public interface FailableBiFunction { /** * Applies this function. @@ -115,20 +133,20 @@ public class Functions { * @param input1 the first input for the function * @param input2 the second input for the function * @return the result of the function - * @throws T if the function fails + * @throws T Thrown when the function fails. */ - R apply(I1 input1, I2 input2) throws T; + R apply(O1 input1, O2 input2) throws T; } /** - * A functional interface like {@link BiPredicate} that declares a Throwable. + * A functional interface like {@link BiPredicate} that declares a {@code Throwable}. * - * @param Predicate type 1. - * @param Predicate type 2. + * @param Predicate type 1. + * @param Predicate type 2. * @param Thrown exception. */ @FunctionalInterface - public interface FailableBiPredicate { + public interface FailableBiPredicate { /** * Tests the predicate. @@ -138,11 +156,11 @@ public class Functions { * @return the predicate's evaluation * @throws T if the predicate fails */ - boolean test(I1 object1, I2 object2) throws T; + boolean test(O1 object1, O2 object2) throws T; } /** - * A functional interface like {@link BooleanSupplier} that declares a Throwable. + * A functional interface like {@link BooleanSupplier} that declares a {@code Throwable}. * * @param Thrown exception. * @since 3.11 @@ -160,7 +178,7 @@ public class Functions { } /** - * A functional interface like {@link java.util.concurrent.Callable} that declares a Throwable. + * A functional interface like {@link java.util.concurrent.Callable} that declares a {@code Throwable}. * * @param Return type. * @param Thrown exception. @@ -178,7 +196,7 @@ public class Functions { } /** - * A functional interface like {@link Consumer} that declares a Throwable. + * A functional interface like {@link Consumer} that declares a {@code Throwable}. * * @param Consumed type 1. * @param Thrown exception. @@ -196,7 +214,27 @@ public class Functions { } /** - * A functional interface like {@link DoubleConsumer} that declares a Throwable. + * A functional interface like {@link DoubleBinaryOperator} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableDoubleBinaryOperator { + + /** + * Applies this operator to the given operands. + * + * @param left the first operand + * @param right the second operand + * @return the operator result + * @throws T if the operation fails + */ + double applyAsDouble(double left, double right) throws T; + } + + /** + * A functional interface like {@link DoubleConsumer} that declares a {@code Throwable}. * * @param Thrown exception. * @since 3.11 @@ -214,7 +252,26 @@ public class Functions { } /** - * A functional interface like {@link DoubleSupplier} that declares a Throwable. + * A functional interface like {@link DoubleFunction} that declares a {@code Throwable}. + * + * @param Return type. + * @param Thrown exception. + */ + @FunctionalInterface + public interface FailableDoubleFunction { + + /** + * Applies this function. + * + * @param input the input for the function + * @return the result of the function + * @throws T Thrown when the function fails. + */ + R apply(double input) throws T; + } + + /** + * A functional interface like {@link DoubleSupplier} that declares a {@code Throwable}. * * @param Thrown exception. * @since 3.11 @@ -232,7 +289,45 @@ public class Functions { } /** - * A functional interface like {@link Function} that declares a Throwable. + * A functional interface like {@link DoubleToIntFunction} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableDoubleToIntFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + int applyAsInt(double value) throws T; + } + + /** + * A functional interface like {@link DoubleToLongFunction} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableDoubleToLongFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + * @throws T if the operation fails + */ + int applyAsLong(double value) throws T; + } + + /** + * A functional interface like {@link Function} that declares a {@code Throwable}. * * @param Input type 1. * @param Return type. @@ -246,13 +341,33 @@ public class Functions { * * @param input the input for the function * @return the result of the function - * @throws T if the function fails + * @throws T Thrown when the function fails. */ R apply(I input) throws T; } /** - * A functional interface like {@link IntConsumer} that declares a Throwable. + * A functional interface like {@link IntBinaryOperator} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableIntBinaryOperator { + + /** + * Applies this operator to the given operands. + * + * @param left the first operand + * @param right the second operand + * @return the operator result + * @throws T if the operation fails + */ + int applyAsInt(int left, int right) throws T; + } + + /** + * A functional interface like {@link IntConsumer} that declares a {@code Throwable}. * * @param Thrown exception. * @since 3.11 @@ -270,7 +385,26 @@ public class Functions { } /** - * A functional interface like {@link IntSupplier} that declares a Throwable. + * A functional interface like {@link IntFunction} that declares a {@code Throwable}. + * + * @param Return type. + * @param Thrown exception. + */ + @FunctionalInterface + public interface FailableIntFunction { + + /** + * Applies this function. + * + * @param input the input for the function + * @return the result of the function + * @throws T Thrown when the function fails. + */ + R apply(int input) throws T; + } + + /** + * A functional interface like {@link IntSupplier} that declares a {@code Throwable}. * * @param Thrown exception. * @since 3.11 @@ -288,7 +422,65 @@ public class Functions { } /** - * A functional interface like {@link LongConsumer} that declares a Throwable. + * A functional interface like {@link IntToDoubleFunction} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableIntToDoubleFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + double applyAsDouble(int value) throws T; + } + + /** + * A functional interface like {@link IntToLongFunction} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableIntToLongFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + long applyAsLong(int value) throws T; + } + + /** + * A functional interface like {@link LongBinaryOperator} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableLongBinaryOperator { + + /** + * Applies this operator to the given operands. + * + * @param left the first operand + * @param right the second operand + * @return the operator result + * @throws T if the operation fails + */ + long applyAsLong(long left, long right) throws T; + } + + /** + * A functional interface like {@link LongConsumer} that declares a {@code Throwable}. * * @param Thrown exception. * @since 3.11 @@ -306,7 +498,26 @@ public class Functions { } /** - * A functional interface like {@link LongSupplier} that declares a Throwable. + * A functional interface like {@link LongFunction} that declares a {@code Throwable}. + * + * @param Return type. + * @param Thrown exception. + */ + @FunctionalInterface + public interface FailableLongFunction { + + /** + * Applies this function. + * + * @param input the input for the function + * @return the result of the function + * @throws T Thrown when the function fails. + */ + R apply(long input) throws T; + } + + /** + * A functional interface like {@link LongSupplier} that declares a {@code Throwable}. * * @param Thrown exception. * @since 3.11 @@ -324,7 +535,45 @@ public class Functions { } /** - * A functional interface like {@link ObjDoubleConsumer} that declares a Throwable. + * A functional interface like {@link LongToDoubleFunction} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableLongToDoubleFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + double applyAsDouble(long value) throws T; + } + + /** + * A functional interface like {@link LongToIntFunction} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableLongToIntFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + int applyAsInt(long value) throws T; + } + + /** + * A functional interface like {@link ObjDoubleConsumer} that declares a {@code Throwable}. * * @param the type of the object argument to the operation. * @param Thrown exception. @@ -344,7 +593,7 @@ public class Functions { } /** - * A functional interface like {@link ObjIntConsumer} that declares a Throwable. + * A functional interface like {@link ObjIntConsumer} that declares a {@code Throwable}. * * @param the type of the object argument to the operation. * @param Thrown exception. @@ -364,7 +613,7 @@ public class Functions { } /** - * A functional interface like {@link ObjLongConsumer} that declares a Throwable. + * A functional interface like {@link ObjLongConsumer} that declares a {@code Throwable}. * * @param the type of the object argument to the operation. * @param Thrown exception. @@ -384,7 +633,7 @@ public class Functions { } /** - * A functional interface like {@link Predicate} that declares a Throwable. + * A functional interface like {@link Predicate} that declares a {@code Throwable}. * * @param Predicate type 1. * @param Thrown exception. @@ -403,7 +652,7 @@ public class Functions { } /** - * A functional interface like {@link Runnable} that declares a Throwable. + * A functional interface like {@link Runnable} that declares a {@code Throwable}. * * @param Thrown exception. */ @@ -413,13 +662,13 @@ public class Functions { /** * Runs the function. * - * @throws T if the function fails + * @throws T Thrown when the function fails. */ void run() throws T; } /** - * A functional interface like {@link Supplier} that declares a Throwable. + * A functional interface like {@link Supplier} that declares a {@code Throwable}. * * @param Return type. * @param Thrown exception. @@ -436,6 +685,132 @@ public class Functions { R get() throws T; } + /** + * A functional interface like {@link ToDoubleBiFunction} that declares a {@code Throwable}. + * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableToDoubleBiFunction { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + double applyAsDouble(O1 t, O2 u) throws T; + } + + /** + * A functional interface like {@link ToDoubleFunction} that declares a {@code Throwable}. + * + * @param the type of the first argument to the function + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableToDoubleFunction { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + double applyAsDouble(I t) throws T; + } + + /** + * A functional interface like {@link ToIntBiFunction} that declares a {@code Throwable}. + * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableToIntBiFunction { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + int applyAsInt(O1 t, O2 u) throws T; + } + + /** + * A functional interface like {@link ToIntFunction} that declares a {@code Throwable}. + * + * @param the type of the first argument to the function + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableToIntFunction { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + int applyAsInt(I t) throws T; + } + + /** + * A functional interface like {@link ToLongBiFunction} that declares a {@code Throwable}. + * + * @param the type of the first argument to the function + * @param the type of the second argument to the function + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableToLongBiFunction { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + long applyAsLong(O1 t, O2 u) throws T; + } + + /** + * A functional interface like {@link ToLongFunction} that declares a {@code Throwable}. + * + * @param the type of the first argument to the function + * @param Thrown exception. + * @since 3.11 + */ + @FunctionalInterface + public interface FailableToLongFunction { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @return the function result + * @throws T Thrown when the function fails. + */ + long applyAsLong(I t) throws T; + } + /** * Consumes a consumer and rethrows any exception as a {@link RuntimeException}. * @@ -505,14 +880,14 @@ public class Functions { * @param function the function to apply * @param input1 the first input to apply {@code function} on * @param input2 the second input to apply {@code function} on - * @param the type of the first argument the function accepts - * @param the type of the second argument the function accepts + * @param the type of the first argument the function accepts + * @param the type of the second argument the function accepts * @param the return type of the function * @param the type of checked exception the function may throw * @return the value returned from the function */ - public static O apply(final FailableBiFunction function, - final I1 input1, final I2 input2) { + public static O apply(final FailableBiFunction function, + final O1 input1, final O2 input2) { return get(() -> function.apply(input1, input2)); } @@ -530,43 +905,58 @@ public class Functions { return get(() -> function.apply(input)); } + /** + * Applies a function and rethrows any exception as a {@link RuntimeException}. + * + * @param function the function to apply + * @param left the first input to apply {@code function} on + * @param right the second input to apply {@code function} on + * @param the type of checked exception the function may throw + * @return the value returned from the function + * @since 3.11 + */ + public static double applyAsDouble(final FailableDoubleBinaryOperator function, + final double left, final double right) { + return getAsDouble(() -> function.applyAsDouble(left, right)); + } + /** * Converts the given {@link FailableBiConsumer} into a standard {@link BiConsumer}. * - * @param the type of the first argument of the consumers - * @param the type of the second argument of the consumers + * @param the type of the first argument of the consumers + * @param the type of the second argument of the consumers * @param consumer a failable {@code BiConsumer} * @return a standard {@code BiConsumer} * @since 3.10 */ - public static BiConsumer asBiConsumer(final FailableBiConsumer consumer) { + public static BiConsumer asBiConsumer(final FailableBiConsumer consumer) { return (input1, input2) -> accept(consumer, input1, input2); } /** * Converts the given {@link FailableBiFunction} into a standard {@link BiFunction}. * - * @param the type of the first argument of the input of the functions - * @param the type of the second argument of the input of the functions + * @param the type of the first argument of the input of the functions + * @param the type of the second argument of the input of the functions * @param the type of the output of the functions * @param function a {@code FailableBiFunction} * @return a standard {@code BiFunction} * @since 3.10 */ - public static BiFunction asBiFunction(final FailableBiFunction function) { + public static BiFunction asBiFunction(final FailableBiFunction function) { return (input1, input2) -> apply(function, input1, input2); } /** * Converts the given {@link FailableBiPredicate} into a standard {@link BiPredicate}. * - * @param the type of the first argument used by the predicates - * @param the type of the second argument used by the predicates + * @param the type of the first argument used by the predicates + * @param the type of the second argument used by the predicates * @param predicate a {@code FailableBiPredicate} * @return a standard {@code BiPredicate} * @since 3.10 */ - public static BiPredicate asBiPredicate(final FailableBiPredicate predicate) { + public static BiPredicate asBiPredicate(final FailableBiPredicate predicate) { return (input1, input2) -> test(predicate, input1, input2); } diff --git a/src/test/java/org/apache/commons/lang3/FunctionsTest.java b/src/test/java/org/apache/commons/lang3/FunctionsTest.java index 043f135fb..d114224f6 100644 --- a/src/test/java/org/apache/commons/lang3/FunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/FunctionsTest.java @@ -117,7 +117,8 @@ class FunctionsTest { public static class Testable { private T acceptedObject; - private P acceptedPrimitiveObject; + private P acceptedPrimitiveObject1; + private P acceptedPrimitiveObject2; private Throwable throwable; Testable(final Throwable throwable) { @@ -128,8 +129,12 @@ class FunctionsTest { return acceptedObject; } - public P getAcceptedPrimitiveObject() { - return acceptedPrimitiveObject; + public P getAcceptedPrimitiveObject1() { + return acceptedPrimitiveObject1; + } + + public P getAcceptedPrimitiveObject2() { + return acceptedPrimitiveObject2; } public void setThrowable(final Throwable throwable) { @@ -140,105 +145,117 @@ class FunctionsTest { test(throwable); } + public Object test(Object input1, Object input2) throws Throwable { + test(throwable); + return acceptedObject; + } + public void test(final Throwable throwable) throws Throwable { if (throwable != null) { throw throwable; } } - public boolean testBooleanPrimitive() throws Throwable { - return testBooleanPrimitive(throwable); + public boolean testAsBooleanPrimitive() throws Throwable { + return testAsBooleanPrimitive(throwable); } - public boolean testBooleanPrimitive(final Throwable throwable) throws Throwable { + public boolean testAsBooleanPrimitive(final Throwable throwable) throws Throwable { if (throwable != null) { throw throwable; } return false; } - public void testDouble(double i) throws Throwable { - test(throwable); - acceptedPrimitiveObject = (P) ((Double) i); + public double testAsDoublePrimitive() throws Throwable { + return testAsDoublePrimitive(throwable); } - public double testDoublePrimitive() throws Throwable { - return testDoublePrimitive(throwable); - } - - public double testDoublePrimitive(final Throwable throwable) throws Throwable { + public double testAsDoublePrimitive(final Throwable throwable) throws Throwable { if (throwable != null) { throw throwable; } return 0; } + public Integer testAsInteger() throws Throwable { + return testAsInteger(throwable); + } + + public Integer testAsInteger(final Throwable throwable) throws Throwable { + if (throwable != null) { + throw throwable; + } + return 0; + } + + public int testAsIntPrimitive() throws Throwable { + return testAsIntPrimitive(throwable); + } + + public int testAsIntPrimitive(final Throwable throwable) throws Throwable { + if (throwable != null) { + throw throwable; + } + return 0; + } + + public long testAsLongPrimitive() throws Throwable { + return testAsLongPrimitive(throwable); + } + + public long testAsLongPrimitive(final Throwable throwable) throws Throwable { + if (throwable != null) { + throw throwable; + } + return 0; + } + + public void testDouble(double i) throws Throwable { + test(throwable); + acceptedPrimitiveObject1 = (P) ((Double) i); + } + + public double testDoubleDouble(double i, double j) throws Throwable { + test(throwable); + acceptedPrimitiveObject1 = (P) ((Double) i); + acceptedPrimitiveObject2 = (P) ((Double) j); + return 3d; + } + public void testInt(int i) throws Throwable { test(throwable); - acceptedPrimitiveObject = (P) ((Integer) i); - } - - public Integer testInteger() throws Throwable { - return testInteger(throwable); - } - - public Integer testInteger(final Throwable throwable) throws Throwable { - if (throwable != null) { - throw throwable; - } - return 0; - } - - public int testIntPrimitive() throws Throwable { - return testIntPrimitive(throwable); - } - - public int testIntPrimitive(final Throwable throwable) throws Throwable { - if (throwable != null) { - throw throwable; - } - return 0; + acceptedPrimitiveObject1 = (P) ((Integer) i); } public void testLong(long i) throws Throwable { test(throwable); - acceptedPrimitiveObject = (P) ((Long) i); - } - - public long testLongPrimitive() throws Throwable { - return testLongPrimitive(throwable); - } - - public long testLongPrimitive(final Throwable throwable) throws Throwable { - if (throwable != null) { - throw throwable; - } - return 0; + acceptedPrimitiveObject1 = (P) ((Long) i); } public void testObjDouble(T object, double i) throws Throwable { test(throwable); acceptedObject = object; - acceptedPrimitiveObject = (P) ((Double) i); + acceptedPrimitiveObject1 = (P) ((Double) i); } public void testObjInt(T object, int i) throws Throwable { test(throwable); acceptedObject = object; - acceptedPrimitiveObject = (P) ((Integer) i); + acceptedPrimitiveObject1 = (P) ((Integer) i); } public void testObjLong(T object, long i) throws Throwable { test(throwable); acceptedObject = object; - acceptedPrimitiveObject = (P) ((Long) i); + acceptedPrimitiveObject1 = (P) ((Long) i); } } @Test void testAcceptBiConsumer() { final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable(null); + final Testable testable = new Testable<>(null); Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(Testable::test, testable, ise)); assertSame(ise, e); @@ -286,13 +303,13 @@ class FunctionsTest { final Testable testable = new Testable<>(ise); Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testDouble, 1d)); assertSame(ise, e); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final Error error = new OutOfMemoryError(); testable.setThrowable(error); e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testDouble, 1d)); assertSame(error, e); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); @@ -300,11 +317,11 @@ class FunctionsTest { final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); testable.setThrowable(null); Functions.accept(testable::testDouble, 1d); - assertEquals(1, testable.getAcceptedPrimitiveObject()); + assertEquals(1, testable.getAcceptedPrimitiveObject1()); } @Test @@ -313,13 +330,13 @@ class FunctionsTest { final Testable testable = new Testable<>(ise); Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testInt, 1)); assertSame(ise, e); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final Error error = new OutOfMemoryError(); testable.setThrowable(error); e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testInt, 1)); assertSame(error, e); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); @@ -327,11 +344,11 @@ class FunctionsTest { final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); testable.setThrowable(null); Functions.accept(testable::testInt, 1); - assertEquals(1, testable.getAcceptedPrimitiveObject()); + assertEquals(1, testable.getAcceptedPrimitiveObject1()); } @Test @@ -340,13 +357,13 @@ class FunctionsTest { final Testable testable = new Testable<>(ise); Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testLong, 1L)); assertSame(ise, e); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final Error error = new OutOfMemoryError(); testable.setThrowable(error); e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testLong, 1L)); assertSame(error, e); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); @@ -354,28 +371,29 @@ class FunctionsTest { final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); testable.setThrowable(null); Functions.accept(testable::testLong, 1L); - assertEquals(1, testable.getAcceptedPrimitiveObject()); + assertEquals(1, testable.getAcceptedPrimitiveObject1()); } @Test void testAcceptObjDoubleConsumer() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable<>(ise); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testObjDouble, "X", 1d)); + Throwable e = assertThrows(IllegalStateException.class, + () -> Functions.accept(testable::testObjDouble, "X", 1d)); assertSame(ise, e); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final Error error = new OutOfMemoryError(); testable.setThrowable(error); e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testObjDouble, "X", 1d)); assertSame(error, e); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); @@ -384,12 +402,12 @@ class FunctionsTest { assertNotNull(t); assertSame(ioe, t); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); testable.setThrowable(null); Functions.accept(testable::testObjDouble, "X", 1d); assertEquals("X", testable.getAcceptedObject()); - assertEquals(1d, testable.getAcceptedPrimitiveObject()); + assertEquals(1d, testable.getAcceptedPrimitiveObject1()); } @Test @@ -399,14 +417,14 @@ class FunctionsTest { Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testObjInt, "X", 1)); assertSame(ise, e); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final Error error = new OutOfMemoryError(); testable.setThrowable(error); e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testObjInt, "X", 1)); assertSame(error, e); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); @@ -415,12 +433,12 @@ class FunctionsTest { assertNotNull(t); assertSame(ioe, t); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); testable.setThrowable(null); Functions.accept(testable::testObjInt, "X", 1); assertEquals("X", testable.getAcceptedObject()); - assertEquals(1, testable.getAcceptedPrimitiveObject()); + assertEquals(1, testable.getAcceptedPrimitiveObject1()); } @Test @@ -430,14 +448,14 @@ class FunctionsTest { Throwable e = assertThrows(IllegalStateException.class, () -> Functions.accept(testable::testObjLong, "X", 1L)); assertSame(ise, e); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final Error error = new OutOfMemoryError(); testable.setThrowable(error); e = assertThrows(OutOfMemoryError.class, () -> Functions.accept(testable::testObjLong, "X", 1L)); assertSame(error, e); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); @@ -446,128 +464,76 @@ class FunctionsTest { assertNotNull(t); assertSame(ioe, t); assertNull(testable.getAcceptedObject()); - assertNull(testable.getAcceptedPrimitiveObject()); + assertNull(testable.getAcceptedPrimitiveObject1()); testable.setThrowable(null); Functions.accept(testable::testObjLong, "X", 1L); assertEquals("X", testable.getAcceptedObject()); - assertEquals(1L, testable.getAcceptedPrimitiveObject()); + assertEquals(1L, testable.getAcceptedPrimitiveObject1()); } @Test public void testApplyBiFunction() { final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable(null); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable, ise)); + final Testable testable = new Testable<>(null); + Throwable e = assertThrows(IllegalStateException.class, + () -> Functions.apply(Testable::testAsInteger, testable, ise)); assertSame(ise, e); final Error error = new OutOfMemoryError(); - e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable, error)); + e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testAsInteger, testable, error)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); - e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable, ioe)); + e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testAsInteger, testable, ioe)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); - final Integer i = Functions.apply(Testable::testInteger, testable, (Throwable) null); + final Integer i = Functions.apply(Testable::testAsInteger, testable, (Throwable) null); assertNotNull(i); assertEquals(0, i.intValue()); } + @Test + public void testApplyDoubleBinaryOperator() { + final IllegalStateException ise = new IllegalStateException(); + final Testable testable = new Testable<>(ise); + Throwable e = assertThrows(IllegalStateException.class, + () -> Functions.applyAsDouble(testable::testDoubleDouble, 1d, 2d)); + assertSame(ise, e); + + final Testable testable2 = new Testable<>(null); + final double i = Functions.applyAsDouble(testable2::testDoubleDouble, 1d, 2d); + assertEquals(3d, i); + } + @Test public void testApplyFunction() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable<>(ise); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.apply(Testable::testInteger, testable)); + Throwable e = assertThrows(IllegalStateException.class, + () -> Functions.apply(Testable::testAsInteger, testable)); assertSame(ise, e); final Error error = new OutOfMemoryError(); testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testInteger, testable)); + e = assertThrows(OutOfMemoryError.class, () -> Functions.apply(Testable::testAsInteger, testable)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testInteger, testable)); + e = assertThrows(UncheckedIOException.class, () -> Functions.apply(Testable::testAsInteger, testable)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); testable.setThrowable(null); - final Integer i = Functions.apply(Testable::testInteger, testable); + final Integer i = Functions.apply(Testable::testAsInteger, testable); assertNotNull(i); assertEquals(0, i.intValue()); } - @Test - void testAsBiConsumer() { - final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable(null); - final FailableBiConsumer failableBiConsumer = (t, th) -> { - t.setThrowable(th); t.test(); - }; - final BiConsumer consumer = Functions.asBiConsumer(failableBiConsumer); - Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable, ise)); - assertSame(ise, e); - - final Error error = new OutOfMemoryError(); - e = assertThrows(OutOfMemoryError.class, () -> consumer.accept(testable, error)); - assertSame(error, e); - - final IOException ioe = new IOException("Unknown I/O error"); - testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> consumer.accept(testable, ioe)); - final Throwable t = e.getCause(); - assertNotNull(t); - assertSame(ioe, t); - - consumer.accept(testable, null); - } - - @Test - public void testAsBiFunction() { - final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable<>(ise); - final FailableBiFunction failableBiFunction = (t, th) -> { - t.setThrowable(th); - return Integer.valueOf(t.testInteger()); - }; - final BiFunction biFunction = Functions.asBiFunction(failableBiFunction); - Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise)); - assertSame(ise, e); - - final Error error = new OutOfMemoryError(); - testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> biFunction.apply(testable, error)); - assertSame(error, e); - - final IOException ioe = new IOException("Unknown I/O error"); - testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> biFunction.apply(testable, ioe)); - final Throwable t = e.getCause(); - assertNotNull(t); - assertSame(ioe, t); - - assertEquals(0, biFunction.apply(testable, null).intValue()); - } - - @Test - @DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ") - public void testAsBiPredicate() { - FailureOnOddInvocations.invocations = 0; - final Functions.FailableBiPredicate failableBiPredicate = (t1, t2) -> FailureOnOddInvocations.failingBool(); - final BiPredicate predicate = Functions.asBiPredicate(failableBiPredicate); - final 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 void testAsCallable() { FailureOnOddInvocations.invocations = 0; @@ -591,7 +557,7 @@ class FunctionsTest { void testAsConsumer() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable<>(ise); - final Consumer consumer = Functions.asConsumer(Testable::test); + final Consumer> consumer = Functions.asConsumer(Testable::test); Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable)); assertSame(ise, e); @@ -611,48 +577,6 @@ class FunctionsTest { Functions.accept(Testable::test, testable); } - @Test - public void testAsFunction() { - final IllegalStateException ise = new IllegalStateException(); - final Testable testable = new Testable<>(ise); - final FailableFunction failableFunction = th -> { - testable.setThrowable(th); - return Integer.valueOf(testable.testInteger()); - }; - final Function function = Functions.asFunction(failableFunction); - Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise)); - assertSame(ise, e); - - final Error error = new OutOfMemoryError(); - testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> function.apply(error)); - assertSame(error, e); - - final IOException ioe = new IOException("Unknown I/O error"); - testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> function.apply(ioe)); - final Throwable t = e.getCause(); - assertNotNull(t); - assertSame(ioe, t); - - assertEquals(0, function.apply(null).intValue()); - } - - @Test - @DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ") - public void testAsPredicate() { - FailureOnOddInvocations.invocations = 0; - final Functions.FailablePredicate failablePredicate = t -> FailureOnOddInvocations.failingBool(); - final Predicate predicate = Functions.asPredicate(failablePredicate); - final 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 void testAsRunnable() { FailureOnOddInvocations.invocations = 0; @@ -680,10 +604,81 @@ class FunctionsTest { assertNotNull(supplier.get()); } + @Test + void testBiConsumer() { + final IllegalStateException ise = new IllegalStateException(); + final Testable testable = new Testable<>(null); + final FailableBiConsumer, Throwable, Throwable> failableBiConsumer = (t, th) -> { + t.setThrowable(th); + t.test(); + }; + final BiConsumer, Throwable> consumer = Functions.asBiConsumer(failableBiConsumer); + Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable, ise)); + assertSame(ise, e); + + final Error error = new OutOfMemoryError(); + e = assertThrows(OutOfMemoryError.class, () -> consumer.accept(testable, error)); + assertSame(error, e); + + final IOException ioe = new IOException("Unknown I/O error"); + testable.setThrowable(ioe); + e = assertThrows(UncheckedIOException.class, () -> consumer.accept(testable, ioe)); + final Throwable t = e.getCause(); + assertNotNull(t); + assertSame(ioe, t); + + consumer.accept(testable, null); + } + + @Test + public void testBiFunction() { + final IllegalStateException ise = new IllegalStateException(); + final Testable testable = new Testable<>(ise); + final FailableBiFunction, Throwable, Integer, Throwable> failableBiFunction = (t, th) -> { + t.setThrowable(th); + return Integer.valueOf(t.testAsInteger()); + }; + final BiFunction, Throwable, Integer> biFunction = Functions.asBiFunction(failableBiFunction); + Throwable e = assertThrows(IllegalStateException.class, () -> biFunction.apply(testable, ise)); + assertSame(ise, e); + + final Error error = new OutOfMemoryError(); + testable.setThrowable(error); + e = assertThrows(OutOfMemoryError.class, () -> biFunction.apply(testable, error)); + assertSame(error, e); + + final IOException ioe = new IOException("Unknown I/O error"); + testable.setThrowable(ioe); + e = assertThrows(UncheckedIOException.class, () -> biFunction.apply(testable, ioe)); + final Throwable t = e.getCause(); + assertNotNull(t); + assertSame(ioe, t); + + assertEquals(0, biFunction.apply(testable, null).intValue()); + } + + @Test + @DisplayName("Test that asPredicate(FailableBiPredicate) is converted to -> BiPredicate ") + public void testBiPredicate() { + FailureOnOddInvocations.invocations = 0; + final Functions.FailableBiPredicate failableBiPredicate = (t1, + t2) -> FailureOnOddInvocations.failingBool(); + final BiPredicate predicate = Functions.asBiPredicate(failableBiPredicate); + final 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 void testCallable() { FailureOnOddInvocations.invocations = 0; - final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new)); + final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, + () -> Functions.run(FailureOnOddInvocations::new)); final Throwable cause = e.getCause(); assertNotNull(cause); assertTrue(cause instanceof SomeException); @@ -699,72 +694,101 @@ class FunctionsTest { } @Test - public void testGetAsBooleanSupplier() { + public void testFunction() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable<>(ise); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive)); + final FailableFunction failableFunction = th -> { + testable.setThrowable(th); + return Integer.valueOf(testable.testAsInteger()); + }; + final Function function = Functions.asFunction(failableFunction); + Throwable e = assertThrows(IllegalStateException.class, () -> function.apply(ise)); assertSame(ise, e); final Error error = new OutOfMemoryError(); testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive)); + e = assertThrows(OutOfMemoryError.class, () -> function.apply(error)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> Functions.getAsBoolean(testable::testBooleanPrimitive)); + e = assertThrows(UncheckedIOException.class, () -> function.apply(ioe)); + final Throwable t = e.getCause(); + assertNotNull(t); + assertSame(ioe, t); + + assertEquals(0, function.apply(null).intValue()); + } + + @Test + public void testGetAsBooleanSupplier() { + final IllegalStateException ise = new IllegalStateException(); + final Testable testable = new Testable<>(ise); + Throwable e = assertThrows(IllegalStateException.class, + () -> Functions.getAsBoolean(testable::testAsBooleanPrimitive)); + assertSame(ise, e); + + final Error error = new OutOfMemoryError(); + testable.setThrowable(error); + e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsBoolean(testable::testAsBooleanPrimitive)); + assertSame(error, e); + + final IOException ioe = new IOException("Unknown I/O error"); + testable.setThrowable(ioe); + e = assertThrows(UncheckedIOException.class, () -> Functions.getAsBoolean(testable::testAsBooleanPrimitive)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); testable.setThrowable(null); - assertFalse(Functions.getAsBoolean(testable::testBooleanPrimitive)); + assertFalse(Functions.getAsBoolean(testable::testAsBooleanPrimitive)); } @Test public void testGetAsDoubleSupplier() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable<>(ise); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsDouble(testable::testDoublePrimitive)); + Throwable e = assertThrows(IllegalStateException.class, + () -> Functions.getAsDouble(testable::testAsDoublePrimitive)); assertSame(ise, e); final Error error = new OutOfMemoryError(); testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsDouble(testable::testDoublePrimitive)); + e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsDouble(testable::testAsDoublePrimitive)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> Functions.getAsDouble(testable::testDoublePrimitive)); + e = assertThrows(UncheckedIOException.class, () -> Functions.getAsDouble(testable::testAsDoublePrimitive)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); testable.setThrowable(null); - assertEquals(0, Functions.getAsDouble(testable::testDoublePrimitive)); + assertEquals(0, Functions.getAsDouble(testable::testAsDoublePrimitive)); } @Test public void testGetAsIntSupplier() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable<>(ise); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsInt(testable::testIntPrimitive)); + Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsInt(testable::testAsIntPrimitive)); assertSame(ise, e); final Error error = new OutOfMemoryError(); testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsInt(testable::testIntPrimitive)); + e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsInt(testable::testAsIntPrimitive)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> Functions.getAsInt(testable::testIntPrimitive)); + e = assertThrows(UncheckedIOException.class, () -> Functions.getAsInt(testable::testAsIntPrimitive)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); testable.setThrowable(null); - final int i = Functions.getAsInt(testable::testInteger); + final int i = Functions.getAsInt(testable::testAsInteger); assertEquals(0, i); } @@ -772,30 +796,32 @@ class FunctionsTest { public void testGetAsLongSupplier() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable<>(ise); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.getAsLong(testable::testLongPrimitive)); + Throwable e = assertThrows(IllegalStateException.class, + () -> Functions.getAsLong(testable::testAsLongPrimitive)); assertSame(ise, e); final Error error = new OutOfMemoryError(); testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsLong(testable::testLongPrimitive)); + e = assertThrows(OutOfMemoryError.class, () -> Functions.getAsLong(testable::testAsLongPrimitive)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> Functions.getAsLong(testable::testLongPrimitive)); + e = assertThrows(UncheckedIOException.class, () -> Functions.getAsLong(testable::testAsLongPrimitive)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); testable.setThrowable(null); - final long i = Functions.getAsLong(testable::testLongPrimitive); + final long i = Functions.getAsLong(testable::testAsLongPrimitive); assertEquals(0, i); } @Test public void testGetFromSupplier() { FailureOnOddInvocations.invocations = 0; - final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new)); + final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, + () -> Functions.run(FailureOnOddInvocations::new)); final Throwable cause = e.getCause(); assertNotNull(cause); assertTrue(cause instanceof SomeException); @@ -808,31 +834,49 @@ class FunctionsTest { public void testGetSupplier() { final IllegalStateException ise = new IllegalStateException(); final Testable testable = new Testable<>(ise); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.get(testable::testInteger)); + Throwable e = assertThrows(IllegalStateException.class, () -> Functions.get(testable::testAsInteger)); assertSame(ise, e); final Error error = new OutOfMemoryError(); testable.setThrowable(error); - e = assertThrows(OutOfMemoryError.class, () -> Functions.get(testable::testInteger)); + e = assertThrows(OutOfMemoryError.class, () -> Functions.get(testable::testAsInteger)); assertSame(error, e); final IOException ioe = new IOException("Unknown I/O error"); testable.setThrowable(ioe); - e = assertThrows(UncheckedIOException.class, () -> Functions.get(testable::testInteger)); + e = assertThrows(UncheckedIOException.class, () -> Functions.get(testable::testAsInteger)); final Throwable t = e.getCause(); assertNotNull(t); assertSame(ioe, t); testable.setThrowable(null); - final Integer i = Functions.apply(Testable::testInteger, testable); + final Integer i = Functions.apply(Testable::testAsInteger, testable); assertNotNull(i); assertEquals(0, i.intValue()); } + @Test + @DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ") + public void testPredicate() { + FailureOnOddInvocations.invocations = 0; + final Functions.FailablePredicate failablePredicate = t -> FailureOnOddInvocations + .failingBool(); + final Predicate predicate = Functions.asPredicate(failablePredicate); + final 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 void testRunnable() { FailureOnOddInvocations.invocations = 0; - final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> Functions.run(FailureOnOddInvocations::new)); + final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, + () -> Functions.run(FailureOnOddInvocations::new)); final Throwable cause = e.getCause(); assertNotNull(cause); assertTrue(cause instanceof SomeException); @@ -842,24 +886,1148 @@ class FunctionsTest { Functions.run(FailureOnOddInvocations::new); } + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableBiConsumer_Object_Throwable() { + new Functions.FailableBiConsumer() { + + @Override + public void accept(Object object1, Object object2) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableBiConsumer_String_IOException() { + new Functions.FailableBiConsumer() { + + @Override + public void accept(String object1, String object2) throws IOException { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableBiFunction_Object_Throwable() { + new Functions.FailableBiFunction() { + + @Override + public Object apply(Object input1, Object input2) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableBiFunction_String_IOException() { + new Functions.FailableBiFunction() { + + @Override + public String apply(String input1, String input2) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableBiPredicate_Object_Throwable() { + new Functions.FailableBiPredicate() { + + @Override + public boolean test(Object object1, Object object2) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableBiPredicate_String_IOException() { + new Functions.FailableBiPredicate() { + + @Override + public boolean test(String object1, String object2) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableBooleanSupplier_Object_Throwable() { + new Functions.FailableBooleanSupplier() { + + @Override + public boolean getAsBoolean() throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableBooleanSupplier_String_IOException() { + new Functions.FailableBooleanSupplier() { + + @Override + public boolean getAsBoolean() throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableCallable_Object_Throwable() { + new Functions.FailableCallable() { + + @Override + public Object call() throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableCallable_String_IOException() { + new Functions.FailableCallable() { + + @Override + public String call() throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableConsumer_Object_Throwable() { + new Functions.FailableConsumer() { + + @Override + public void accept(Object object) throws Throwable { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableConsumer_String_IOException() { + new Functions.FailableConsumer() { + + @Override + public void accept(String object) throws IOException { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableDoubleBinaryOperator_Object_Throwable() { + new Functions.FailableDoubleBinaryOperator() { + + @Override + public double applyAsDouble(double left, double right) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableDoubleBinaryOperator_String_IOException() { + new Functions.FailableDoubleBinaryOperator() { + + @Override + public double applyAsDouble(double left, double right) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableDoubleConsumer_Object_Throwable() { + new Functions.FailableDoubleConsumer() { + + @Override + public void accept(double value) throws Throwable { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableDoubleConsumer_String_IOException() { + new Functions.FailableDoubleConsumer() { + + @Override + public void accept(double value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableDoubleFunction_Object_Throwable() { + new Functions.FailableDoubleFunction() { + + @Override + public Object apply(double input) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableDoubleFunction_String_IOException() { + new Functions.FailableDoubleFunction() { + + @Override + public String apply(double input) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableDoubleSupplier_Object_Throwable() { + new Functions.FailableDoubleSupplier() { + + @Override + public double getAsDouble() throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableDoubleSupplier_String_IOException() { + new Functions.FailableDoubleSupplier() { + + @Override + public double getAsDouble() throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableDoubleToIntFunction_Object_Throwable() { + new Functions.FailableDoubleToIntFunction() { + + @Override + public int applyAsInt(double value) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableDoubleToIntFunction_String_IOException() { + new Functions.FailableDoubleToIntFunction() { + + @Override + public int applyAsInt(double value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableDoubleToLongFunction_Object_Throwable() { + new Functions.FailableDoubleToLongFunction() { + + @Override + public int applyAsLong(double value) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableDoubleToLongFunction_String_IOException() { + new Functions.FailableDoubleToLongFunction() { + + @Override + public int applyAsLong(double value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableFunction_Object_Throwable() { + new Functions.FailableFunction() { + + @Override + public Object apply(Object input) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableFunction_String_IOException() { + new Functions.FailableFunction() { + + @Override + public String apply(String input) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableIntBinaryOperator_Object_Throwable() { + new Functions.FailableIntBinaryOperator() { + + @Override + public int applyAsInt(int left, int right) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableIntBinaryOperator_String_IOException() { + new Functions.FailableIntBinaryOperator() { + + @Override + public int applyAsInt(int left, int right) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableIntConsumer_Object_Throwable() { + new Functions.FailableIntConsumer() { + + @Override + public void accept(int value) throws Throwable { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableIntConsumer_String_IOException() { + new Functions.FailableIntConsumer() { + + @Override + public void accept(int value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableIntFunction_Object_Throwable() { + new Functions.FailableIntFunction() { + + @Override + public Object apply(int input) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableIntFunction_String_IOException() { + new Functions.FailableIntFunction() { + + @Override + public String apply(int input) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableIntSupplier_Object_Throwable() { + new Functions.FailableIntSupplier() { + + @Override + public int getAsInt() throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableIntSupplier_String_IOException() { + new Functions.FailableIntSupplier() { + + @Override + public int getAsInt() throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableIntToDoubleFunction_Object_Throwable() { + new Functions.FailableIntToDoubleFunction() { + + @Override + public double applyAsDouble(int value) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableIntToDoubleFunction_String_IOException() { + new Functions.FailableIntToDoubleFunction() { + + @Override + public double applyAsDouble(int value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableIntToLongFunction_Object_Throwable() { + new Functions.FailableIntToLongFunction() { + + @Override + public long applyAsLong(int value) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableIntToLongFunction_String_IOException() { + new Functions.FailableIntToLongFunction() { + + @Override + public long applyAsLong(int value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableLongBinaryOperator_Object_Throwable() { + new Functions.FailableLongBinaryOperator() { + + @Override + public long applyAsLong(long left, long right) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableLongBinaryOperator_String_IOException() { + new Functions.FailableLongBinaryOperator() { + + @Override + public long applyAsLong(long left, long right) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableLongConsumer_Object_Throwable() { + new Functions.FailableLongConsumer() { + + @Override + public void accept(long object) throws Throwable { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableLongConsumer_String_IOException() { + new Functions.FailableLongConsumer() { + + @Override + public void accept(long object) throws IOException { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableLongFunction_Object_Throwable() { + new Functions.FailableLongFunction() { + + @Override + public Object apply(long input) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableLongFunction_String_IOException() { + new Functions.FailableLongFunction() { + + @Override + public String apply(long input) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableLongSupplier_Object_Throwable() { + new Functions.FailableLongSupplier() { + + @Override + public long getAsLong() throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableLongSupplier_String_IOException() { + new Functions.FailableLongSupplier() { + + @Override + public long getAsLong() throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableLongToDoubleFunction_Object_Throwable() { + new Functions.FailableLongToDoubleFunction() { + + @Override + public double applyAsDouble(long value) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableLongToDoubleFunction_String_IOException() { + new Functions.FailableLongToDoubleFunction() { + + @Override + public double applyAsDouble(long value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableLongToIntFunction_Object_Throwable() { + new Functions.FailableLongToIntFunction() { + + @Override + public int applyAsInt(long value) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableLongToIntFunction_String_IOException() { + new Functions.FailableLongToIntFunction() { + + @Override + public int applyAsInt(long value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableObjDoubleConsumer_Object_Throwable() { + new Functions.FailableObjDoubleConsumer() { + + @Override + public void accept(Object object, double value) throws Throwable { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableObjDoubleConsumer_String_IOException() { + new Functions.FailableObjDoubleConsumer() { + + @Override + public void accept(String object, double value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableObjIntConsumer_Object_Throwable() { + new Functions.FailableObjIntConsumer() { + + @Override + public void accept(Object object, int value) throws Throwable { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableObjIntConsumer_String_IOException() { + new Functions.FailableObjIntConsumer() { + + @Override + public void accept(String object, int value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableObjLongConsumer_Object_Throwable() { + new Functions.FailableObjLongConsumer() { + + @Override + public void accept(Object object, long value) throws Throwable { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableObjLongConsumer_String_IOException() { + new Functions.FailableObjLongConsumer() { + + @Override + public void accept(String object, long value) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailablePredicate_Object_Throwable() { + new Functions.FailablePredicate() { + + @Override + public boolean test(Object object) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailablePredicate_String_IOException() { + new Functions.FailablePredicate() { + + @Override + public boolean test(String object) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableRunnable_Object_Throwable() { + new Functions.FailableRunnable() { + + @Override + public void run() throws Throwable { + throw new IOException("test"); + + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableRunnable_String_IOException() { + new Functions.FailableRunnable() { + + @Override + public void run() throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableSupplier_Object_Throwable() { + new Functions.FailableSupplier() { + + @Override + public Object get() throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableSupplier_String_IOException() { + new Functions.FailableSupplier() { + + @Override + public String get() throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableToDoubleBiFunction_Object_Throwable() { + new Functions.FailableToDoubleBiFunction() { + + @Override + public double applyAsDouble(Object t, Object u) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableToDoubleBiFunction_String_IOException() { + new Functions.FailableToDoubleBiFunction() { + + @Override + public double applyAsDouble(String t, String u) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableToDoubleFunction_Object_Throwable() { + new Functions.FailableToDoubleFunction() { + + @Override + public double applyAsDouble(Object t) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableToDoubleFunction_String_IOException() { + new Functions.FailableToDoubleFunction() { + + @Override + public double applyAsDouble(String t) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableToIntBiFunction_Object_Throwable() { + new Functions.FailableToIntBiFunction() { + + @Override + public int applyAsInt(Object t, Object u) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableToIntBiFunction_String_IOException() { + new Functions.FailableToIntBiFunction() { + + @Override + public int applyAsInt(String t, String u) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableToIntFunction_Object_Throwable() { + new Functions.FailableToIntFunction() { + + @Override + public int applyAsInt(Object t) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableToIntFunction_String_IOException() { + new Functions.FailableToIntFunction() { + + @Override + public int applyAsInt(String t) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableToLongBiFunction_Object_Throwable() { + new Functions.FailableToLongBiFunction() { + + @Override + public long applyAsLong(Object t, Object u) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableToLongBiFunction_String_IOException() { + new Functions.FailableToLongBiFunction() { + + @Override + public long applyAsLong(String t, String u) throws IOException { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Object and Throwable. + */ + @Test + void testThrows_FailableToLongFunction_Object_Throwable() { + new Functions.FailableToLongFunction() { + + @Override + public long applyAsLong(Object t) throws Throwable { + throw new IOException("test"); + } + }; + } + + /** + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + void testThrows_FailableToLongFunction_String_IOException() { + new Functions.FailableToLongFunction() { + + @Override + public long applyAsLong(String t) throws IOException { + throw new IOException("test"); + } + }; + } + @Test public void testTryWithResources() { final CloseableObject co = new CloseableObject(); final FailableConsumer consumer = co::run; final IllegalStateException ise = new IllegalStateException(); - Throwable e = assertThrows(IllegalStateException.class, () -> Functions.tryWithResources(() -> consumer.accept(ise), co::close)); + Throwable e = assertThrows(IllegalStateException.class, + () -> Functions.tryWithResources(() -> consumer.accept(ise), co::close)); assertSame(ise, e); assertTrue(co.isClosed()); co.reset(); final Error error = new OutOfMemoryError(); - e = assertThrows(OutOfMemoryError.class, () -> Functions.tryWithResources(() -> consumer.accept(error), co::close)); + e = assertThrows(OutOfMemoryError.class, + () -> Functions.tryWithResources(() -> consumer.accept(error), co::close)); assertSame(error, e); assertTrue(co.isClosed()); co.reset(); final IOException ioe = new IOException("Unknown I/O error"); - final UncheckedIOException uioe = assertThrows(UncheckedIOException.class, () -> Functions.tryWithResources(() -> consumer.accept(ioe), co::close)); + final UncheckedIOException uioe = assertThrows(UncheckedIOException.class, + () -> Functions.tryWithResources(() -> consumer.accept(ioe), co::close)); final IOException cause = uioe.getCause(); assertSame(ioe, cause);