This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit 7895a1a8d07eb2ddba89db4574d3184d380ab466 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Dec 1 09:38:40 2024 -0500 Make Failable.accept(*) null-safe --- src/changes/changes.xml | 1 + .../lang3/concurrent/locks/LockingVisitors.java | 10 ++------ .../apache/commons/lang3/function/Failable.java | 30 ++++++++++++++-------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d8d2266d0..a0e878d07 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -66,6 +66,7 @@ The <action> type attribute can be add,update,fix,remove. <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> <action issue="LANG-1511" type="fix" dev="ggregory" due-to="david cogen, Gary Gregory, Bruno P. Kinoshita">SystemUtils is missing important documentation.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Make Failable.run(FailableRunnable) null-safe.</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Make Failable.accept(*) null-safe.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action> <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> diff --git a/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java b/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java index 379febf44..5f01f7eda 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/locks/LockingVisitors.java @@ -269,11 +269,7 @@ public class LockingVisitors { final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock"); lock.lock(); try { - if (consumer != null) { - consumer.accept(object); - } - } catch (final Throwable t) { - throw Failable.rethrow(t); + Failable.accept(consumer, object); } finally { lock.unlock(); } @@ -298,9 +294,7 @@ public class LockingVisitors { final Lock lock = Objects.requireNonNull(Suppliers.get(lockSupplier), "lock"); lock.lock(); try { - return function.apply(object); - } catch (final Throwable t) { - throw Failable.rethrow(t); + return Failable.apply(function, object); } finally { lock.unlock(); } 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 a2814e107..cf3fc143f 100644 --- a/src/main/java/org/apache/commons/lang3/function/Failable.java +++ b/src/main/java/org/apache/commons/lang3/function/Failable.java @@ -74,7 +74,7 @@ public class Failable { /** * Consumes a consumer and rethrows any exception as a {@link RuntimeException}. * - * @param consumer the consumer to consume + * @param consumer the consumer to accept, may be null for a noop. * @param object1 the first object to consume by {@code consumer} * @param object2 the second object to consume by {@code consumer} * @param <T> the type of the first argument the consumer accepts @@ -83,52 +83,52 @@ public class Failable { */ public static <T, U, E extends Throwable> void accept(final FailableBiConsumer<T, U, E> consumer, final T object1, final U object2) { - run(() -> consumer.accept(object1, object2)); + run(consumer, () -> consumer.accept(object1, object2)); } /** * Consumes a consumer and rethrows any exception as a {@link RuntimeException}. * - * @param consumer the consumer to consume + * @param consumer the consumer to accept, may be null for a noop. * @param object the object to consume by {@code consumer} * @param <T> the type the consumer accepts * @param <E> the type of checked exception the consumer may throw */ public static <T, E extends Throwable> void accept(final FailableConsumer<T, E> consumer, final T object) { - run(() -> consumer.accept(object)); + run(consumer, () -> consumer.accept(object)); } /** * Consumes a consumer and rethrows any exception as a {@link RuntimeException}. * - * @param consumer the consumer to consume + * @param consumer the consumer to accept, may be null for a noop. * @param value the value to consume by {@code consumer} * @param <E> the type of checked exception the consumer may throw */ public static <E extends Throwable> void accept(final FailableDoubleConsumer<E> consumer, final double value) { - run(() -> consumer.accept(value)); + run(consumer, () -> consumer.accept(value)); } /** * Consumes a consumer and rethrows any exception as a {@link RuntimeException}. * - * @param consumer the consumer to consume + * @param consumer the consumer to accept, may be null for a noop. * @param value the value to consume by {@code consumer} * @param <E> the type of checked exception the consumer may throw */ public static <E extends Throwable> void accept(final FailableIntConsumer<E> consumer, final int value) { - run(() -> consumer.accept(value)); + run(consumer, () -> consumer.accept(value)); } /** * Consumes a consumer and rethrows any exception as a {@link RuntimeException}. * - * @param consumer the consumer to consume + * @param consumer the consumer to accept, may be null for a noop. * @param value the value to consume by {@code consumer} * @param <E> the type of checked exception the consumer may throw */ public static <E extends Throwable> void accept(final FailableLongConsumer<E> consumer, final long value) { - run(() -> consumer.accept(value)); + run(consumer, () -> consumer.accept(value)); } /** @@ -432,6 +432,16 @@ public class Failable { } } + private static <E extends Throwable> void run(final Object test, final FailableRunnable<E> runnable) { + if (runnable != null && test != null) { + try { + runnable.run(); + } catch (final Throwable t) { + throw rethrow(t); + } + } + } + /** * Converts the given collection into a {@link FailableStream}. The {@link FailableStream} consists of the * collections elements. Shortcut for