Skip to content

Commit 7895a1a

Browse files
committed
Make Failable.accept(*) null-safe
1 parent 096b05e commit 7895a1a

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ The <action> type attribute can be add,update,fix,remove.
6666
<action issue="LANG-1698" type="fix" dev="ggregory" due-to="Jan Arne Sparka, Gary Gregory">Fix StackOverflowError in TypeUtils.typeVariableToString(TypeVariable), TypeUtils.toString(Type) on Java 17 and up.</action>
6767
<action issue="LANG-1511" type="fix" dev="ggregory" due-to="david cogen, Gary Gregory, Bruno P. Kinoshita">SystemUtils is missing important documentation.</action>
6868
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make Failable.run(FailableRunnable) null-safe.</action>
69+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make Failable.accept(*) null-safe.</action>
6970
<!-- ADD -->
7071
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
7172
<action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>

src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,7 @@ protected void lockAcceptUnlock(final Supplier<Lock> lockSupplier, final Failabl
269269
final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock");
270270
lock.lock();
271271
try {
272-
if (consumer != null) {
273-
consumer.accept(object);
274-
}
275-
} catch (final Throwable t) {
276-
throw Failable.rethrow(t);
272+
Failable.accept(consumer, object);
277273
} finally {
278274
lock.unlock();
279275
}
@@ -298,9 +294,7 @@ protected <T> T lockApplyUnlock(final Supplier<Lock> lockSupplier, final Failabl
298294
final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock");
299295
lock.lock();
300296
try {
301-
return function.apply(object);
302-
} catch (final Throwable t) {
303-
throw Failable.rethrow(t);
297+
return Failable.apply(function, object);
304298
} finally {
305299
lock.unlock();
306300
}

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class Failable {
7474
/**
7575
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
7676
*
77-
* @param consumer the consumer to consume
77+
* @param consumer the consumer to accept, may be null for a noop.
7878
* @param object1 the first object to consume by {@code consumer}
7979
* @param object2 the second object to consume by {@code consumer}
8080
* @param <T> the type of the first argument the consumer accepts
@@ -83,52 +83,52 @@ public class Failable {
8383
*/
8484
public static <T, U, E extends Throwable> void accept(final FailableBiConsumer<T, U, E> consumer, final T object1,
8585
final U object2) {
86-
run(() -> consumer.accept(object1, object2));
86+
run(consumer, () -> consumer.accept(object1, object2));
8787
}
8888

8989
/**
9090
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
9191
*
92-
* @param consumer the consumer to consume
92+
* @param consumer the consumer to accept, may be null for a noop.
9393
* @param object the object to consume by {@code consumer}
9494
* @param <T> the type the consumer accepts
9595
* @param <E> the type of checked exception the consumer may throw
9696
*/
9797
public static <T, E extends Throwable> void accept(final FailableConsumer<T, E> consumer, final T object) {
98-
run(() -> consumer.accept(object));
98+
run(consumer, () -> consumer.accept(object));
9999
}
100100

101101
/**
102102
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
103103
*
104-
* @param consumer the consumer to consume
104+
* @param consumer the consumer to accept, may be null for a noop.
105105
* @param value the value to consume by {@code consumer}
106106
* @param <E> the type of checked exception the consumer may throw
107107
*/
108108
public static <E extends Throwable> void accept(final FailableDoubleConsumer<E> consumer, final double value) {
109-
run(() -> consumer.accept(value));
109+
run(consumer, () -> consumer.accept(value));
110110
}
111111

112112
/**
113113
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
114114
*
115-
* @param consumer the consumer to consume
115+
* @param consumer the consumer to accept, may be null for a noop.
116116
* @param value the value to consume by {@code consumer}
117117
* @param <E> the type of checked exception the consumer may throw
118118
*/
119119
public static <E extends Throwable> void accept(final FailableIntConsumer<E> consumer, final int value) {
120-
run(() -> consumer.accept(value));
120+
run(consumer, () -> consumer.accept(value));
121121
}
122122

123123
/**
124124
* Consumes a consumer and rethrows any exception as a {@link RuntimeException}.
125125
*
126-
* @param consumer the consumer to consume
126+
* @param consumer the consumer to accept, may be null for a noop.
127127
* @param value the value to consume by {@code consumer}
128128
* @param <E> the type of checked exception the consumer may throw
129129
*/
130130
public static <E extends Throwable> void accept(final FailableLongConsumer<E> consumer, final long value) {
131-
run(() -> consumer.accept(value));
131+
run(consumer, () -> consumer.accept(value));
132132
}
133133

134134
/**
@@ -432,6 +432,16 @@ public static <E extends Throwable> void run(final FailableRunnable<E> runnable)
432432
}
433433
}
434434

435+
private static <E extends Throwable> void run(final Object test, final FailableRunnable<E> runnable) {
436+
if (runnable != null && test != null) {
437+
try {
438+
runnable.run();
439+
} catch (final Throwable t) {
440+
throw rethrow(t);
441+
}
442+
}
443+
}
444+
435445
/**
436446
* Converts the given collection into a {@link FailableStream}. The {@link FailableStream} consists of the
437447
* collections elements. Shortcut for

0 commit comments

Comments
 (0)