From 9f74672c20d782b8b5ca105bb57a854460b16a34 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Mon, 10 Jun 2024 19:41:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20extract=20internal=20helper=20metho?= =?UTF-8?q?d=20`requireArrayAndEleNonNull`=20=F0=9F=94=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cffu/CompletableFutureUtils.java | 123 ++++++------------ 1 file changed, 40 insertions(+), 83 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 dbc951d4..28867fdc 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -62,10 +62,8 @@ public static CompletableFuture mRunAsync(Runnable... actions) { */ public static CompletableFuture mRunAsync(Executor executor, Runnable... actions) { requireNonNull(executor, "executor is null"); - requireNonNull(actions, "actions is null"); - for (int i = 0; i < actions.length; i++) { - requireNonNull(actions[i], "action" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("action", actions); + return CompletableFuture.allOf(wrapActions(executor, actions)); } @@ -99,13 +97,20 @@ public static CompletableFuture mRunFastFailAsync(Runnable... actions) { */ public static CompletableFuture mRunFastFailAsync(Executor executor, Runnable... actions) { requireNonNull(executor, "executor is null"); - requireNonNull(actions, "actions is null"); - for (int i = 0; i < actions.length; i++) { - requireNonNull(actions[i], "action" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("action", actions); + return allOfFastFail(wrapActions(executor, actions)); } + @SafeVarargs + private static T[] requireArrayAndEleNonNull(String varName, T... array) { + requireNonNull(array, varName + "s is null"); + for (int i = 0; i < array.length; i++) { + requireNonNull(array[i], varName + (i + 1) + " is null"); + } + return array; + } + private static CompletableFuture[] wrapActions(Executor executor, Runnable[] actions) { CompletableFuture[] cfs = new CompletableFuture[actions.length]; for (int i = 0; i < actions.length; i++) { @@ -146,10 +151,8 @@ public static CompletableFuture> mSupplyAsync(Supplier. @SafeVarargs public static CompletableFuture> mSupplyAsync(Executor executor, Supplier... suppliers) { requireNonNull(executor, "executor is null"); - requireNonNull(suppliers, "suppliers is null"); - for (int i = 0; i < suppliers.length; i++) { - requireNonNull(suppliers[i], "supplier" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("supplier", suppliers); + return allResultsOf(wrapSuppliers(executor, suppliers)); } @@ -190,10 +193,8 @@ public static CompletableFuture> mSupplyFastFailAsync(Supplier CompletableFuture> mSupplyFastFailAsync( Executor executor, Supplier... suppliers) { requireNonNull(executor, "executor is null"); - requireNonNull(suppliers, "suppliers is null"); - for (int i = 0; i < suppliers.length; i++) { - requireNonNull(suppliers[i], "supplier" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("supplier", suppliers); + return allResultsOfFastFail(wrapSuppliers(executor, suppliers)); } @@ -247,10 +248,8 @@ public static CompletableFuture> mSupplyMostSuccessAsync( @Nullable T valueIfNotSuccess, Supplier... suppliers) { requireNonNull(executor, "executor is null"); requireNonNull(unit, "unit is null"); - requireNonNull(suppliers, "suppliers is null"); - for (int i = 0; i < suppliers.length; i++) { - requireNonNull(suppliers[i], "supplier" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("supplier", suppliers); + return mostResultsOfSuccess(executor, timeout, unit, valueIfNotSuccess, wrapSuppliers(executor, suppliers)); } @@ -537,11 +536,7 @@ private static T[] MGetSuccessNow0(@Nullable Object valueIfNotSuccess, Compl @SafeVarargs private static > S[] requireCfsAndEleNonNull(S... css) { - requireNonNull(css, "cfs is null"); - for (int i = 0; i < css.length; i++) { - requireNonNull(css[i], "cf" + (i + 1) + " is null"); - } - return css; + return requireArrayAndEleNonNull("cf", css); } /** @@ -1097,10 +1092,8 @@ public static CompletableFuture thenMRunAsync(CompletionStage cf, Runna public static CompletableFuture thenMRunAsync(CompletionStage cf, Executor executor, Runnable... actions) { requireNonNull(cf, "cf is null"); requireNonNull(executor, "executor is null"); - requireNonNull(actions, "actions is null"); - for (int i = 0; i < actions.length; i++) { - requireNonNull(actions[i], "action" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("action", actions); + return toNonMinCf(cf).thenCompose(unused -> CompletableFuture.allOf(wrapActions(executor, actions))); } @@ -1132,10 +1125,8 @@ public static CompletableFuture thenMRunFastFailAsync( CompletionStage cf, Executor executor, Runnable... actions) { requireNonNull(cf, "cf is null"); requireNonNull(executor, "executor is null"); - requireNonNull(actions, "actions is null"); - for (int i = 0; i < actions.length; i++) { - requireNonNull(actions[i], "action" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("action", actions); + return toNonMinCf(cf).thenCompose(unused -> allOfFastFail(wrapActions(executor, actions))); } @@ -1165,10 +1156,8 @@ public static CompletableFuture thenMAcceptAsync( CompletionStage cf, Executor executor, Consumer... actions) { requireNonNull(cf, "cf is null"); requireNonNull(executor, "executor is null"); - requireNonNull(actions, "actions is null"); - for (int i = 0; i < actions.length; i++) { - requireNonNull(actions[i], "action" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("action", actions); + return toNonMinCf(cf).thenCompose(v -> CompletableFuture.allOf(wrapConsumers(executor, v, actions))); } @@ -1204,10 +1193,8 @@ public static CompletableFuture thenMAcceptFastFailAsync( CompletionStage cf, Executor executor, Consumer... actions) { requireNonNull(cf, "cf is null"); requireNonNull(executor, "executor is null"); - requireNonNull(actions, "actions is null"); - for (int i = 0; i < actions.length; i++) { - requireNonNull(actions[i], "action" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("action", actions); + return toNonMinCf(cf).thenCompose(v -> allOfFastFail(wrapConsumers(executor, v, actions))); } @@ -1253,10 +1240,8 @@ public static CompletableFuture> thenMApplyAsync( CompletionStage cf, Executor executor, Function... fns) { requireNonNull(cf, "cf is null"); requireNonNull(executor, "executor is null"); - requireNonNull(fns, "fns is null"); - for (int i = 0; i < fns.length; i++) { - requireNonNull(fns[i], "fn" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("fn", fns); + return toNonMinCf(cf).thenCompose(v -> allResultsOf(wrapFunctions(executor, v, fns))); } @@ -1298,10 +1283,8 @@ public static CompletableFuture> thenMApplyFastFailAsync( CompletionStage cf, Executor executor, Function... fns) { requireNonNull(cf, "cf is null"); requireNonNull(executor, "executor is null"); - requireNonNull(fns, "fns is null"); - for (int i = 0; i < fns.length; i++) { - requireNonNull(fns[i], "fn" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("fn", fns); + return toNonMinCf(cf).thenCompose(v -> allResultsOfFastFail(wrapFunctions(executor, v, fns))); } @@ -1350,10 +1333,8 @@ public static CompletableFuture> thenMApplyMostSuccessAsync( @Nullable U valueIfNotSuccess, Function... fns) { requireNonNull(executor, "executor is null"); requireNonNull(unit, "unit is null"); - requireNonNull(fns, "fns is null"); - for (int i = 0; i < fns.length; i++) { - requireNonNull(fns[i], "fn" + (i + 1) + " is null"); - } + requireArrayAndEleNonNull("fn", fns); + return toNonMinCf(cf).thenCompose(v -> mostResultsOfSuccess( executor, timeout, unit, valueIfNotSuccess, wrapFunctions(executor, v, fns) )); @@ -1415,10 +1396,7 @@ public static CompletableFuture runAfterBothFastFail( */ public static CompletableFuture runAfterBothFastFailAsync( CompletionStage cf1, CompletionStage cf2, Runnable action) { - final CompletionStage[] css = requireCfsAndEleNonNull(cf1, cf2); - requireNonNull(action, "action is null"); - - return allOfFastFail(css).thenRunAsync(action); + return runAfterBothFastFailAsync(cf1, cf2, action, AsyncPoolHolder.ASYNC_POOL); } /** @@ -1489,13 +1467,7 @@ public static CompletableFuture thenAcceptBothFastFail( public static CompletableFuture thenAcceptBothFastFailAsync( CompletionStage cf1, CompletionStage cf2, BiConsumer action) { - final CompletionStage[] css = requireCfsAndEleNonNull(cf1, cf2); - requireNonNull(action, "action is null"); - - final Object[] result = new Object[css.length]; - final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - - return allOfFastFail(resultSetterCfs).thenRunAsync(() -> action.accept((T) result[0], (U) result[1])); + return thenAcceptBothFastFailAsync(cf1, cf2, action, AsyncPoolHolder.ASYNC_POOL); } /** @@ -1572,13 +1544,7 @@ public static CompletableFuture thenCombineFastFail( public static CompletableFuture thenCombineFastFailAsync( CompletionStage cf1, CompletionStage cf2, BiFunction fn) { - final CompletionStage[] css = requireCfsAndEleNonNull(cf1, cf2); - requireNonNull(fn, "fn is null"); - - final Object[] result = new Object[css.length]; - final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - - return allOfFastFail(resultSetterCfs).thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1])); + return thenCombineFastFailAsync(cf1, cf2, fn, AsyncPoolHolder.ASYNC_POOL); } /** @@ -1656,10 +1622,7 @@ public static CompletableFuture runAfterEitherSuccess( */ public static CompletableFuture runAfterEitherSuccessAsync( CompletionStage cf1, CompletionStage cf2, Runnable action) { - final CompletionStage[] css = requireCfsAndEleNonNull(cf1, cf2); - requireNonNull(action, "action is null"); - - return anyOfSuccess(css).thenRunAsync(action); + return runAfterEitherSuccessAsync(cf1, cf2, action, AsyncPoolHolder.ASYNC_POOL); } /** @@ -1718,10 +1681,7 @@ public static CompletableFuture acceptEitherSuccess( */ public static CompletableFuture acceptEitherSuccessAsync( CompletionStage cf1, CompletionStage cf2, Consumer action) { - final CompletionStage[] css = requireCfsAndEleNonNull(cf1, cf2); - requireNonNull(action, "action is null"); - - return anyOfSuccess(css).thenAcceptAsync(action); + return acceptEitherSuccessAsync(cf1, cf2, action, AsyncPoolHolder.ASYNC_POOL); } /** @@ -1781,10 +1741,7 @@ public static CompletableFuture applyToEitherSuccess( */ public static CompletableFuture applyToEitherSuccessAsync( CompletionStage cf1, CompletionStage cf2, Function fn) { - final CompletionStage[] css = requireCfsAndEleNonNull(cf1, cf2); - requireNonNull(fn, "fn is null"); - - return anyOfSuccess(css).thenApplyAsync(fn); + return applyToEitherSuccessAsync(cf1, cf2, fn, AsyncPoolHolder.ASYNC_POOL); } /**