Add FailableShortSupplier, handy for JDBC APIs.

This commit is contained in:
Gary Gregory 2020-12-24 10:02:37 -05:00
parent b5bb3e6d3a
commit 45bec219d2
4 changed files with 262 additions and 144 deletions

View File

@ -83,6 +83,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1596" type="update" dev="aherbert" due-to="Richard Eckart de Castilho">ArrayUtils.toPrimitive(Object) does not support boolean and other types #607.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add fluent-style ArraySorter.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use LocaleUtils.toLocale(Locale) to avoid NPEs.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableShortSupplier, handy for JDBC APIs.</action>
<!-- UPDATES -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Enable Dependabot #587.</action>
<action type="update" dev="chtompki">Bump junit-jupiter from 5.6.2 to 5.7.0.</action>

View File

@ -324,7 +324,7 @@ public static <E extends Throwable> boolean getAsBoolean(final FailableBooleanSu
*
* @param supplier The double supplier to invoke.
* @param <E> 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 <E extends Throwable> double getAsDouble(final FailableDoubleSupplier<E> supplier) {
try {
@ -339,7 +339,7 @@ public static <E extends Throwable> double getAsDouble(final FailableDoubleSuppl
*
* @param supplier The int supplier to invoke.
* @param <E> 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 <E extends Throwable> int getAsInt(final FailableIntSupplier<E> supplier) {
try {
@ -354,7 +354,7 @@ public static <E extends Throwable> int getAsInt(final FailableIntSupplier<E> su
*
* @param supplier The long supplier to invoke.
* @param <E> 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 <E extends Throwable> long getAsLong(final FailableLongSupplier<E> supplier) {
try {
@ -364,6 +364,21 @@ public static <E extends Throwable> long getAsLong(final FailableLongSupplier<E>
}
}
/**
* Invokes a short supplier, and returns the result.
*
* @param supplier The short supplier to invoke.
* @param <E> The type of checked exception, which the supplier can throw.
* @return The short, which has been created by the supplier
*/
public static <E extends Throwable> short getAsShort(final FailableShortSupplier<E> supplier) {
try {
return supplier.getAsShort();
} catch (final Throwable t) {
throw rethrow(t);
}
}
/**
* <p>
* Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a

View File

@ -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 <E> Thrown exception.
* @since 3.12
*/
@FunctionalInterface
public interface FailableShortSupplier<E extends Throwable> {
/**
* Supplies an int.
*
* @return a result
* @throws E if the supplier fails
*/
short getAsShort() throws E;
}

View File

@ -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<Throwable>() {
@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<Throwable>() {
@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<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableDoubleBinaryOperator_Throwable() {
new FailableDoubleBinaryOperator<Throwable>() {
@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<Object, Throwable>() {
public void testThrows_FailableDoubleConsumer_Throwable() {
new FailableDoubleConsumer<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableDoubleFunction_Throwable() {
new FailableDoubleFunction<Object, Throwable>() {
@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<Throwable>() {
public void testThrows_FailableDoubleSupplier_Throwable() {
new FailableDoubleSupplier<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableDoubleToIntFunction_Throwable() {
new FailableDoubleToIntFunction<Throwable>() {
@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<Throwable>() {
@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<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableIntBinaryOperator_Throwable() {
new FailableIntBinaryOperator<Throwable>() {
@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<Throwable>() {
@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<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableIntSupplier_Throwable() {
new FailableIntSupplier<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableIntToDoubleFunction_Throwable() {
new FailableIntToDoubleFunction<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableIntToLongFunction_Throwable() {
new FailableIntToLongFunction<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableLongBinaryOperator_Throwable() {
new FailableLongBinaryOperator<Throwable>() {
@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<Object, Throwable>() {
public void testThrows_FailableLongConsumer_Throwable() {
new FailableLongConsumer<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableLongFunction_Throwable() {
new FailableLongFunction<Object, Throwable>() {
@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<Throwable>() {
public void testThrows_FailableLongSupplier_Throwable() {
new FailableLongSupplier<Throwable>() {
@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<Throwable>() {
public void testThrows_FailableLongToDoubleFunction_Throwable() {
new FailableLongToDoubleFunction<Throwable>() {
@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<Throwable>() {
@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<IOException>() {
@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<IOException>() {
public void testThrows_FailableShortSupplier_IOException() {
new FailableShortSupplier<IOException>() {
@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<Throwable>() {
@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