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 15, 2024
1 parent 8b6b5ab commit 9f74672
Showing 1 changed file with 40 additions and 83 deletions.
123 changes: 40 additions & 83 deletions cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ 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 +97,20 @@ 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 @@ -146,10 +151,8 @@ 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 @@ -190,10 +193,8 @@ public static <T> CompletableFuture<List<T>> mSupplyFastFailAsync(Supplier<? ext
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 @@ -247,10 +248,8 @@ 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 @@ -537,11 +536,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 @@ -1097,10 +1092,8 @@ 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 @@ -1132,10 +1125,8 @@ 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 @@ -1165,10 +1156,8 @@ 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 @@ -1204,10 +1193,8 @@ 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 @@ -1253,10 +1240,8 @@ 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 @@ -1298,10 +1283,8 @@ 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 @@ -1350,10 +1333,8 @@ 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 Expand Up @@ -1415,10 +1396,7 @@ public static CompletableFuture<Void> runAfterBothFastFail(
*/
public static CompletableFuture<Void> 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);
}

/**
Expand Down Expand Up @@ -1489,13 +1467,7 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFail(
public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(
CompletionStage<? extends T> cf1, CompletionStage<? extends U> cf2,
BiConsumer<? super T, ? super U> action) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(action, "action is null");

final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allOfFastFail(resultSetterCfs).thenRunAsync(() -> action.accept((T) result[0], (U) result[1]));
return thenAcceptBothFastFailAsync(cf1, cf2, action, AsyncPoolHolder.ASYNC_POOL);
}

/**
Expand Down Expand Up @@ -1572,13 +1544,7 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFail(
public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(
CompletionStage<? extends T> cf1, CompletionStage<? extends U> cf2,
BiFunction<? super T, ? super U, ? extends V> fn) {
final CompletionStage<?>[] css = requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(fn, "fn is null");

final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] 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);
}

/**
Expand Down Expand Up @@ -1656,10 +1622,7 @@ public static CompletableFuture<Void> runAfterEitherSuccess(
*/
public static CompletableFuture<Void> 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);
}

/**
Expand Down Expand Up @@ -1718,10 +1681,7 @@ public static <T> CompletableFuture<Void> acceptEitherSuccess(
*/
public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(
CompletionStage<? extends T> cf1, CompletionStage<? extends T> cf2, Consumer<? super T> action) {
final CompletionStage<? extends T>[] css = requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(action, "action is null");

return anyOfSuccess(css).thenAcceptAsync(action);
return acceptEitherSuccessAsync(cf1, cf2, action, AsyncPoolHolder.ASYNC_POOL);
}

/**
Expand Down Expand Up @@ -1781,10 +1741,7 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccess(
*/
public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(
CompletionStage<? extends T> cf1, CompletionStage<? extends T> cf2, Function<? super T, ? extends U> fn) {
final CompletionStage<? extends T>[] css = requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(fn, "fn is null");

return anyOfSuccess(css).thenApplyAsync(fn);
return applyToEitherSuccessAsync(cf1, cf2, fn, AsyncPoolHolder.ASYNC_POOL);
}

/**
Expand Down

0 comments on commit 9f74672

Please sign in to comment.