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 79d1970f..3277f2aa 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,7 @@ 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 +96,19 @@ 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++) { @@ -144,10 +147,7 @@ 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)); } @@ -185,10 +185,7 @@ 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)); } @@ -239,10 +236,7 @@ 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)); } @@ -529,11 +523,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); } /** @@ -1089,10 +1079,7 @@ 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))); } @@ -1124,10 +1111,7 @@ 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))); } @@ -1157,10 +1141,7 @@ 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))); } @@ -1196,10 +1177,7 @@ 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))); } @@ -1240,10 +1218,7 @@ 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))); } @@ -1281,10 +1256,7 @@ 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))); } @@ -1333,10 +1305,7 @@ 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) ));