Skip to content

Commit

Permalink
refactor: extract internal helper method requireArrayAndEleNonNull 🔧
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jun 14, 2024
1 parent a065c2c commit 7d68a16
Showing 1 changed file with 22 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ public static CompletableFuture<Void> mRunAsync(Runnable... actions) {
*/
public static CompletableFuture<Void> 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));
}

Expand Down Expand Up @@ -99,13 +96,19 @@ public static CompletableFuture<Void> mRunFastFailAsync(Runnable... actions) {
*/
public static CompletableFuture<Void> 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> 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++) {
Expand Down Expand Up @@ -144,10 +147,7 @@ public static <T> CompletableFuture<List<T>> mSupplyAsync(Supplier<? extends T>.
@SafeVarargs
public static <T> CompletableFuture<List<T>> mSupplyAsync(Executor executor, Supplier<? extends T>... 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));
}

Expand Down Expand Up @@ -185,10 +185,7 @@ public static <T> CompletableFuture<List<T>> mSupplyFastFailAsync(Supplier<? ext
@SafeVarargs
public static <T> CompletableFuture<List<T>> mSupplyFastFailAsync(Executor executor, Supplier<? extends T>... 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));
}

Expand Down Expand Up @@ -239,10 +236,7 @@ public static <T> CompletableFuture<List<T>> mSupplyMostSuccessAsync(
@Nullable T valueIfNotSuccess, Supplier<? extends T>... 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));
}

Expand Down Expand Up @@ -529,11 +523,7 @@ private static <T> T[] MGetSuccessNow0(@Nullable Object valueIfNotSuccess, Compl

@SafeVarargs
private static <S extends CompletionStage<?>> 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);
}

/**
Expand Down Expand Up @@ -1089,10 +1079,7 @@ public static CompletableFuture<Void> thenMRunAsync(CompletionStage<?> cf, Runna
public static CompletableFuture<Void> 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)));
}

Expand Down Expand Up @@ -1124,10 +1111,7 @@ public static CompletableFuture<Void> 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)));
}

Expand Down Expand Up @@ -1157,10 +1141,7 @@ public static <T> CompletableFuture<Void> thenMAcceptAsync(
CompletionStage<? extends T> cf, Executor executor, Consumer<? super T>... 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)));
}

Expand Down Expand Up @@ -1196,10 +1177,7 @@ public static <T> CompletableFuture<Void> thenMAcceptFastFailAsync(
CompletionStage<? extends T> cf, Executor executor, Consumer<? super T>... 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)));
}

Expand Down Expand Up @@ -1240,10 +1218,7 @@ public static <T, U> CompletableFuture<List<U>> thenMApplyAsync(
CompletionStage<? extends T> cf, Executor executor, Function<? super T, ? extends U>... 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)));
}

Expand Down Expand Up @@ -1281,10 +1256,7 @@ public static <T, U> CompletableFuture<List<U>> thenMApplyFastFailAsync(
CompletionStage<? extends T> cf, Executor executor, Function<? super T, ? extends U>... 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)));
}

Expand Down Expand Up @@ -1333,10 +1305,7 @@ public static <T, U> CompletableFuture<List<U>> thenMApplyMostSuccessAsync(
@Nullable U valueIfNotSuccess, Function<? super T, ? extends U>... 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)
));
Expand Down

0 comments on commit 7d68a16

Please sign in to comment.