From 78caa9741d8f903295d63d6975d32301f28e4d7b Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 29 Jun 2024 20:03:53 +0800 Subject: [PATCH] refactor: improve `NPE` message for `this/other` methods --- .../cffu/CompletableFutureUtils.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java index 2e7fcd75..a67dc677 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -2370,7 +2370,7 @@ public static CompletableFuture CompletableFuture thenCombineFastFail( CompletableFuture cfThis, CompletionStage other, BiFunction fn) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(fn, "fn is null"); final Object[] result = new Object[css.length]; @@ -2411,7 +2411,7 @@ public static CompletableFuture thenCombineFastFailAsync( public static CompletableFuture thenCombineFastFailAsync( CompletableFuture cfThis, CompletionStage other, BiFunction fn, Executor executor) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(fn, "fn is null"); requireNonNull(executor, "executor is null"); @@ -2422,6 +2422,12 @@ public static CompletableFuture thenCombineFastFailAsync( .thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1]), executor); } + @SuppressWarnings("unchecked") + private static CompletionStage[] requireThisAndOtherNonNull( + CompletionStage cfThis, CompletionStage other) { + return new CompletionStage[]{requireNonNull(cfThis, "cfThis is null"), requireNonNull(other, "other is null")}; + } + /** * Returns a new CompletableFuture that, when tow given stage both complete normally, * is executed with the two results as arguments to the supplied action. @@ -2436,7 +2442,7 @@ public static CompletableFuture thenCombineFastFailAsync( public static CompletableFuture thenAcceptBothFastFail( CompletableFuture cfThis, CompletionStage other, BiConsumer action) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(action, "action is null"); final Object[] result = new Object[css.length]; @@ -2477,7 +2483,7 @@ public static CompletableFuture thenAcceptBothFastFailAsync( public static CompletableFuture thenAcceptBothFastFailAsync( CompletableFuture cfThis, CompletionStage other, BiConsumer action, Executor executor) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(action, "action is null"); requireNonNull(executor, "executor is null"); @@ -2498,7 +2504,7 @@ public static CompletableFuture thenAcceptBothFastFailAsync( */ public static CompletableFuture runAfterBothFastFail( CompletableFuture cfThis, CompletionStage other, Runnable action) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(action, "action is null"); return allFastFailOf(css).thenRun(action); @@ -2531,7 +2537,7 @@ public static CompletableFuture runAfterBothFastFailAsync( */ public static CompletableFuture runAfterBothFastFailAsync( CompletableFuture cfThis, CompletionStage other, Runnable action, Executor executor) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(action, "action is null"); requireNonNull(executor, "executor is null"); @@ -2557,7 +2563,7 @@ public static CompletableFuture runAfterBothFastFailAsync( */ public static CompletableFuture applyToEitherSuccess( CompletableFuture cfThis, CompletionStage other, Function fn) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(fn, "fn is null"); return anySuccessOf(css).thenApply(fn); @@ -2589,7 +2595,7 @@ public static CompletableFuture applyToEitherSuccessAsync( public static CompletableFuture applyToEitherSuccessAsync( CompletableFuture cfThis, CompletionStage other, Function fn, Executor executor) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(fn, "fn is null"); requireNonNull(executor, "executor is null"); @@ -2605,7 +2611,7 @@ public static CompletableFuture applyToEitherSuccessAsync( */ public static CompletableFuture acceptEitherSuccess( CompletableFuture cfThis, CompletionStage other, Consumer action) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(action, "action is null"); return anySuccessOf(css).thenAccept(action); @@ -2635,7 +2641,7 @@ public static CompletableFuture acceptEitherSuccessAsync( public static CompletableFuture acceptEitherSuccessAsync( CompletableFuture cfThis, CompletionStage other, Consumer action, Executor executor) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(action, "action is null"); requireNonNull(executor, "executor is null"); @@ -2653,7 +2659,7 @@ public static CompletableFuture acceptEitherSuccessAsync( */ public static CompletableFuture runAfterEitherSuccess( CompletableFuture cfThis, CompletionStage other, Runnable action) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(action, "action is null"); return anySuccessOf(css).thenRun(action); @@ -2686,7 +2692,7 @@ public static CompletableFuture runAfterEitherSuccessAsync( */ public static CompletableFuture runAfterEitherSuccessAsync( CompletableFuture cfThis, CompletionStage other, Runnable action, Executor executor) { - final CompletionStage[] css = requireCfsAndEleNonNull(cfThis, other); + final CompletionStage[] css = requireThisAndOtherNonNull(cfThis, other); requireNonNull(action, "action is null"); requireNonNull(executor, "executor is null");