Skip to content

Commit 45bec21

Browse files
committed
Add FailableShortSupplier, handy for JDBC APIs.
1 parent b5bb3e6 commit 45bec21

File tree

4 files changed

+252
-134
lines changed

4 files changed

+252
-134
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The <action> type attribute can be add,update,fix,remove.
8383
<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>
8484
<action type="add" dev="ggregory" due-to="Gary Gregory">Add fluent-style ArraySorter.</action>
8585
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use LocaleUtils.toLocale(Locale) to avoid NPEs.</action>
86+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add FailableShortSupplier, handy for JDBC APIs.</action>
8687
<!-- UPDATES -->
8788
<action type="update" dev="ggregory" due-to="Gary Gregory">Enable Dependabot #587.</action>
8889
<action type="update" dev="chtompki">Bump junit-jupiter from 5.6.2 to 5.7.0.</action>

src/main/java/org/apache/commons/lang3/function/Failable.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public static <E extends Throwable> boolean getAsBoolean(final FailableBooleanSu
324324
*
325325
* @param supplier The double supplier to invoke.
326326
* @param <E> The type of checked exception, which the supplier can throw.
327-
* @return The boolean, which has been created by the supplier
327+
* @return The double, which has been created by the supplier
328328
*/
329329
public static <E extends Throwable> double getAsDouble(final FailableDoubleSupplier<E> supplier) {
330330
try {
@@ -339,7 +339,7 @@ public static <E extends Throwable> double getAsDouble(final FailableDoubleSuppl
339339
*
340340
* @param supplier The int supplier to invoke.
341341
* @param <E> The type of checked exception, which the supplier can throw.
342-
* @return The boolean, which has been created by the supplier
342+
* @return The int, which has been created by the supplier
343343
*/
344344
public static <E extends Throwable> int getAsInt(final FailableIntSupplier<E> supplier) {
345345
try {
@@ -354,7 +354,7 @@ public static <E extends Throwable> int getAsInt(final FailableIntSupplier<E> su
354354
*
355355
* @param supplier The long supplier to invoke.
356356
* @param <E> The type of checked exception, which the supplier can throw.
357-
* @return The boolean, which has been created by the supplier
357+
* @return The long, which has been created by the supplier
358358
*/
359359
public static <E extends Throwable> long getAsLong(final FailableLongSupplier<E> supplier) {
360360
try {
@@ -364,6 +364,21 @@ public static <E extends Throwable> long getAsLong(final FailableLongSupplier<E>
364364
}
365365
}
366366

367+
/**
368+
* Invokes a short supplier, and returns the result.
369+
*
370+
* @param supplier The short supplier to invoke.
371+
* @param <E> The type of checked exception, which the supplier can throw.
372+
* @return The short, which has been created by the supplier
373+
*/
374+
public static <E extends Throwable> short getAsShort(final FailableShortSupplier<E> supplier) {
375+
try {
376+
return supplier.getAsShort();
377+
} catch (final Throwable t) {
378+
throw rethrow(t);
379+
}
380+
}
381+
367382
/**
368383
* <p>
369384
* Rethrows a {@link Throwable} as an unchecked exception. If the argument is already unchecked, namely a
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3.function;
19+
20+
import java.util.function.IntSupplier;
21+
22+
/**
23+
* A functional interface like {@link IntSupplier} but for {@code short} that declares a {@code Throwable}.
24+
*
25+
* @param <E> Thrown exception.
26+
* @since 3.12
27+
*/
28+
@FunctionalInterface
29+
public interface FailableShortSupplier<E extends Throwable> {
30+
31+
/**
32+
* Supplies an int.
33+
*
34+
* @return a result
35+
* @throws E if the supplier fails
36+
*/
37+
short getAsShort() throws E;
38+
}

0 commit comments

Comments
 (0)