From 45bec219d2e039e85e71711e01d4543a85c1756b Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 24 Dec 2020 10:02:37 -0500 Subject: [PATCH] Add FailableShortSupplier, handy for JDBC APIs. --- src/changes/changes.xml | 1 + .../commons/lang3/function/Failable.java | 21 +- .../lang3/function/FailableShortSupplier.java | 38 ++ .../lang3/function/FailableFunctionsTest.java | 346 +++++++++++------- 4 files changed, 262 insertions(+), 144 deletions(-) create mode 100644 src/main/java/org/apache/commons/lang3/function/FailableShortSupplier.java diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6898081e4..94abb8d85 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -83,6 +83,7 @@ The type attribute can be add,update,fix,remove. ArrayUtils.toPrimitive(Object) does not support boolean and other types #607. Add fluent-style ArraySorter. Add and use LocaleUtils.toLocale(Locale) to avoid NPEs. + Add FailableShortSupplier, handy for JDBC APIs. Enable Dependabot #587. Bump junit-jupiter from 5.6.2 to 5.7.0. diff --git a/src/main/java/org/apache/commons/lang3/function/Failable.java b/src/main/java/org/apache/commons/lang3/function/Failable.java index 04e128e40..164f6aad9 100644 --- a/src/main/java/org/apache/commons/lang3/function/Failable.java +++ b/src/main/java/org/apache/commons/lang3/function/Failable.java @@ -324,7 +324,7 @@ public static boolean getAsBoolean(final FailableBooleanSu * * @param supplier The double supplier to invoke. * @param The type of checked exception, which the supplier can throw. - * @return The boolean, which has been created by the supplier + * @return The double, which has been created by the supplier */ public static double getAsDouble(final FailableDoubleSupplier supplier) { try { @@ -339,7 +339,7 @@ public static double getAsDouble(final FailableDoubleSuppl * * @param supplier The int supplier to invoke. * @param The type of checked exception, which the supplier can throw. - * @return The boolean, which has been created by the supplier + * @return The int, which has been created by the supplier */ public static int getAsInt(final FailableIntSupplier supplier) { try { @@ -354,7 +354,7 @@ public static int getAsInt(final FailableIntSupplier su * * @param supplier The long supplier to invoke. * @param The type of checked exception, which the supplier can throw. - * @return The boolean, which has been created by the supplier + * @return The long, which has been created by the supplier */ public static long getAsLong(final FailableLongSupplier supplier) { try { @@ -364,6 +364,21 @@ public static long getAsLong(final FailableLongSupplier } } + /** + * Invokes a short supplier, and returns the result. + * + * @param supplier The short supplier to invoke. + * @param The type of checked exception, which the supplier can throw. + * @return The short, which has been created by the supplier + */ + public static short getAsShort(final FailableShortSupplier supplier) { + try { + return supplier.getAsShort(); + } catch (final Throwable t) { + throw rethrow(t); + } + } + /** *

* Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a diff --git a/src/main/java/org/apache/commons/lang3/function/FailableShortSupplier.java b/src/main/java/org/apache/commons/lang3/function/FailableShortSupplier.java new file mode 100644 index 000000000..f27cf3430 --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/function/FailableShortSupplier.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.lang3.function; + +import java.util.function.IntSupplier; + +/** + * A functional interface like {@link IntSupplier} but for {@code short} that declares a {@code Throwable}. + * + * @param Thrown exception. + * @since 3.12 + */ +@FunctionalInterface +public interface FailableShortSupplier { + + /** + * Supplies an int. + * + * @return a result + * @throws E if the supplier fails + */ + short getAsShort() throws E; +} diff --git a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java index d5874bfef..0d57ab2f9 100644 --- a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java +++ b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java @@ -223,6 +223,17 @@ public long testAsLongPrimitive(final Throwable throwable) throws Throwable { return 0; } + public short testAsShortPrimitive() throws Throwable { + return testAsShortPrimitive(throwable); + } + + public short testAsShortPrimitive(final Throwable throwable) throws Throwable { + if (throwable != null) { + throw throwable; + } + return 0; + } + public void testDouble(final double i) throws Throwable { test(throwable); acceptedPrimitiveObject1 = (P) ((Double) i); @@ -1044,6 +1055,29 @@ public void testGetAsLongSupplier() { assertEquals(0, i); } + @Test + public void testGetAsShortSupplier() { + final Testable testable = new Testable<>(ILLEGAL_STATE_EXCEPTION); + Throwable e = assertThrows(IllegalStateException.class, + () -> Failable.getAsShort(testable::testAsShortPrimitive)); + assertSame(ILLEGAL_STATE_EXCEPTION, e); + + testable.setThrowable(ERROR); + e = assertThrows(OutOfMemoryError.class, () -> Failable.getAsShort(testable::testAsShortPrimitive)); + assertSame(ERROR, e); + + final IOException ioe = new IOException("Unknown I/O error"); + testable.setThrowable(ioe); + e = assertThrows(UncheckedIOException.class, () -> Failable.getAsShort(testable::testAsShortPrimitive)); + final Throwable t = e.getCause(); + assertNotNull(t); + assertSame(ioe, t); + + testable.setThrowable(null); + final short i = Failable.getAsShort(testable::testAsShortPrimitive); + assertEquals(0, i); + } + @Test public void testGetFromSupplier() { FailureOnOddInvocations.invocations = 0; @@ -1340,7 +1374,7 @@ public void testRunnable() { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -1371,7 +1405,7 @@ public void accept(final String object1, final String object2) throws IOExceptio } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -1401,7 +1435,7 @@ public String apply(final String input1, final String input2) throws IOException } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -1430,21 +1464,6 @@ public boolean test(final String object1, final String object2) throws IOExcepti }; } - /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types - * Object and Throwable. - */ - @Test - public void testThrows_FailableBooleanSupplier_Throwable() { - new 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. @@ -1461,7 +1480,22 @@ public boolean getAsBoolean() throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types + * Object and Throwable. + */ + @Test + public void testThrows_FailableBooleanSupplier_Throwable() { + new FailableBooleanSupplier() { + + @Override + public boolean getAsBoolean() throws Throwable { + 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 @@ -1491,7 +1525,7 @@ public String call() throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -1522,21 +1556,6 @@ public void accept(final String object) throws IOException { }; } - /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types - * Object and Throwable. - */ - @Test - public void testThrows_FailableDoubleBinaryOperator_Throwable() { - new FailableDoubleBinaryOperator() { - - @Override - public double applyAsDouble(final double left, final 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. @@ -1553,17 +1572,16 @@ public double applyAsDouble(final double left, final double right) throws IOExce } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableDoubleConsumer_Throwable() { - new FailableDoubleConsumer() { + public void testThrows_FailableDoubleBinaryOperator_Throwable() { + new FailableDoubleBinaryOperator() { @Override - public void accept(final double value) throws Throwable { + public double applyAsDouble(final double left, final double right) throws Throwable { throw new IOException("test"); - } }; } @@ -1584,16 +1602,17 @@ public void accept(final double value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableDoubleFunction_Throwable() { - new FailableDoubleFunction() { + public void testThrows_FailableDoubleConsumer_Throwable() { + new FailableDoubleConsumer() { @Override - public Object apply(final double input) throws Throwable { + public void accept(final double value) throws Throwable { throw new IOException("test"); + } }; } @@ -1614,15 +1633,15 @@ public String apply(final double input) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableDoubleSupplier_Throwable() { - new FailableDoubleSupplier() { + public void testThrows_FailableDoubleFunction_Throwable() { + new FailableDoubleFunction() { @Override - public double getAsDouble() throws Throwable { + public Object apply(final double input) throws Throwable { throw new IOException("test"); } }; @@ -1644,15 +1663,15 @@ public double getAsDouble() throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableDoubleToIntFunction_Throwable() { - new FailableDoubleToIntFunction() { + public void testThrows_FailableDoubleSupplier_Throwable() { + new FailableDoubleSupplier() { @Override - public int applyAsInt(final double value) throws Throwable { + public double getAsDouble() throws Throwable { throw new IOException("test"); } }; @@ -1674,15 +1693,15 @@ public int applyAsInt(final double value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableDoubleToLongFunction_Throwable() { - new FailableDoubleToLongFunction() { + public void testThrows_FailableDoubleToIntFunction_Throwable() { + new FailableDoubleToIntFunction() { @Override - public int applyAsLong(final double value) throws Throwable { + public int applyAsInt(final double value) throws Throwable { throw new IOException("test"); } }; @@ -1704,7 +1723,22 @@ public int applyAsLong(final double value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types + * Object and Throwable. + */ + @Test + public void testThrows_FailableDoubleToLongFunction_Throwable() { + new FailableDoubleToLongFunction() { + + @Override + public int applyAsLong(final double value) throws Throwable { + 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 @@ -1733,21 +1767,6 @@ public String apply(final String input) throws IOException { }; } - /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types - * Object and Throwable. - */ - @Test - public void testThrows_FailableIntBinaryOperator_Throwable() { - new FailableIntBinaryOperator() { - - @Override - public int applyAsInt(final int left, final 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. @@ -1764,17 +1783,16 @@ public int applyAsInt(final int left, final int right) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableIntConsumer_Throwable() { - new FailableIntConsumer() { + public void testThrows_FailableIntBinaryOperator_Throwable() { + new FailableIntBinaryOperator() { @Override - public void accept(final int value) throws Throwable { + public int applyAsInt(final int left, final int right) throws Throwable { throw new IOException("test"); - } }; } @@ -1795,7 +1813,23 @@ public void accept(final int value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types + * Object and Throwable. + */ + @Test + public void testThrows_FailableIntConsumer_Throwable() { + new FailableIntConsumer() { + + @Override + public void accept(final int value) throws Throwable { + 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 @@ -1824,21 +1858,6 @@ public String apply(final int input) throws IOException { }; } - /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types - * Object and Throwable. - */ - @Test - public void testThrows_FailableIntSupplier_Throwable() { - new 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. @@ -1855,15 +1874,15 @@ public int getAsInt() throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableIntToDoubleFunction_Throwable() { - new FailableIntToDoubleFunction() { + public void testThrows_FailableIntSupplier_Throwable() { + new FailableIntSupplier() { @Override - public double applyAsDouble(final int value) throws Throwable { + public int getAsInt() throws Throwable { throw new IOException("test"); } }; @@ -1885,15 +1904,15 @@ public double applyAsDouble(final int value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableIntToLongFunction_Throwable() { - new FailableIntToLongFunction() { + public void testThrows_FailableIntToDoubleFunction_Throwable() { + new FailableIntToDoubleFunction() { @Override - public long applyAsLong(final int value) throws Throwable { + public double applyAsDouble(final int value) throws Throwable { throw new IOException("test"); } }; @@ -1915,15 +1934,15 @@ public long applyAsLong(final int value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableLongBinaryOperator_Throwable() { - new FailableLongBinaryOperator() { + public void testThrows_FailableIntToLongFunction_Throwable() { + new FailableIntToLongFunction() { @Override - public long applyAsLong(final long left, final long right) throws Throwable { + public long applyAsLong(final int value) throws Throwable { throw new IOException("test"); } }; @@ -1945,17 +1964,16 @@ public long applyAsLong(final long left, final long right) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableLongConsumer_Throwable() { - new FailableLongConsumer() { + public void testThrows_FailableLongBinaryOperator_Throwable() { + new FailableLongBinaryOperator() { @Override - public void accept(final long object) throws Throwable { + public long applyAsLong(final long left, final long right) throws Throwable { throw new IOException("test"); - } }; } @@ -1977,16 +1995,17 @@ public void accept(final long object) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableLongFunction_Throwable() { - new FailableLongFunction() { + public void testThrows_FailableLongConsumer_Throwable() { + new FailableLongConsumer() { @Override - public Object apply(final long input) throws Throwable { + public void accept(final long object) throws Throwable { throw new IOException("test"); + } }; } @@ -2007,15 +2026,15 @@ public String apply(final long input) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableLongSupplier_Throwable() { - new FailableLongSupplier() { + public void testThrows_FailableLongFunction_Throwable() { + new FailableLongFunction() { @Override - public long getAsLong() throws Throwable { + public Object apply(final long input) throws Throwable { throw new IOException("test"); } }; @@ -2037,15 +2056,15 @@ public long getAsLong() throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableLongToDoubleFunction_Throwable() { - new FailableLongToDoubleFunction() { + public void testThrows_FailableLongSupplier_Throwable() { + new FailableLongSupplier() { @Override - public double applyAsDouble(final long value) throws Throwable { + public long getAsLong() throws Throwable { throw new IOException("test"); } }; @@ -2067,15 +2086,15 @@ public double applyAsDouble(final long value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test - public void testThrows_FailableLongToIntFunction_Throwable() { - new FailableLongToIntFunction() { + public void testThrows_FailableLongToDoubleFunction_Throwable() { + new FailableLongToDoubleFunction() { @Override - public int applyAsInt(final long value) throws Throwable { + public double applyAsDouble(final long value) throws Throwable { throw new IOException("test"); } }; @@ -2097,7 +2116,22 @@ public int applyAsInt(final long value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types + * Object and Throwable. + */ + @Test + public void testThrows_FailableLongToIntFunction_Throwable() { + new FailableLongToIntFunction() { + + @Override + public int applyAsInt(final long value) throws Throwable { + 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 @@ -2128,7 +2162,7 @@ public void accept(final String object, final double value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -2159,7 +2193,7 @@ public void accept(final String object, final int value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -2190,7 +2224,7 @@ public void accept(final String object, final long value) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -2220,7 +2254,22 @@ public boolean test(final String object) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using String and IOExceptions as + * generic test types. + */ + @Test + public void testThrows_FailableRunnable_IOException() { + new 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 @@ -2240,18 +2289,33 @@ public void run() throws Throwable { * generic test types. */ @Test - public void testThrows_FailableRunnable_IOException() { - new FailableRunnable() { + public void testThrows_FailableShortSupplier_IOException() { + new FailableShortSupplier() { @Override - public void run() throws IOException { + public short getAsShort() throws IOException { throw new IOException("test"); } }; } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types + * Object and Throwable. + */ + @Test + public void testThrows_FailableShortSupplier_Throwable() { + new FailableShortSupplier() { + + @Override + public short getAsShort() throws Throwable { + 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 @@ -2281,7 +2345,7 @@ public String get() throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -2311,7 +2375,7 @@ public double applyAsDouble(final String t, final String u) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -2341,7 +2405,7 @@ public double applyAsDouble(final String t) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -2371,7 +2435,7 @@ public int applyAsInt(final String t, final String u) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -2401,7 +2465,7 @@ public int applyAsInt(final String t) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test @@ -2431,7 +2495,7 @@ public long applyAsLong(final String t, final String u) throws IOException { } /** - * Tests that our failable interface is properly defined to throw any exception. using the top level generic types + * Tests that our failable interface is properly defined to throw any exception using the top level generic types * Object and Throwable. */ @Test