From 4ab2c70c9d133e2cfc5a76cec6c9c64b58c13960 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 0896a632..dac6cfb5 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) { @SuppressWarnings("unchecked") CompletableFuture[] cfs = new CompletableFuture[actions.length]; @@ -147,10 +152,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)); } @@ -191,10 +194,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)); } @@ -248,10 +249,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)); } @@ -538,11 +537,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); } /** @@ -1098,10 +1093,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))); } @@ -1133,10 +1126,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))); } @@ -1166,10 +1157,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))); } @@ -1205,10 +1194,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))); } @@ -1254,10 +1241,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))); } @@ -1299,10 +1284,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))); } @@ -1355,10 +1338,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) )); @@ -1420,10 +1401,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); } /** @@ -1494,13 +1472,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); } /** @@ -1577,13 +1549,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); } /** @@ -1661,10 +1627,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); } /** @@ -1723,10 +1686,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); } /** @@ -1786,10 +1746,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); } /**