Skip to content

Commit

Permalink
refactor: cleanup toCompletableFuture() invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Apr 28, 2024
1 parent 184060d commit f2af962
Showing 1 changed file with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static <T> CompletableFuture<List<T>> allResultsOf(CompletionStage<? exte
final CompletableFuture<?>[] collectResultCfs = new CompletableFuture[size];
for (int i = 0; i < size; i++) {
final int index = i;
collectResultCfs[index] = cfs[index].thenAccept(v -> result[index] = v).toCompletableFuture();
collectResultCfs[index] = cfs[index].toCompletableFuture().thenAccept(v -> result[index] = v);
}

return CompletableFuture.allOf(collectResultCfs)
Expand Down Expand Up @@ -90,7 +90,7 @@ public static CompletableFuture<Void> allOfFastFail(CompletionStage<?>... cfs) {
requireCfsAndEleNonNull(cfs);
final int size = cfs.length;
if (size == 0) return CompletableFuture.completedFuture(null);
if (size == 1) return (CompletableFuture) cfs[0].thenApply(v -> null).toCompletableFuture();
if (size == 1) return cfs[0].toCompletableFuture().thenApply(v -> null);

final CompletableFuture[] successOrBeIncomplete = new CompletableFuture[size];
// NOTE: fill ONE MORE element of failedOrBeIncomplete LATER
Expand Down Expand Up @@ -154,16 +154,16 @@ private static void requireCfsAndEleNonNull(CompletionStage<?>... cfs) {
* Returns normal array list instead of unmodifiable or fixed-size list.
* Safer for application code which may reuse the returned list as normal collection.
*/
@SafeVarargs
@SuppressWarnings("unchecked")
private static <T> List<T> arrayList(T... elements) {
List<T> ret = new ArrayList<>(elements.length);
ret.addAll(Arrays.asList(elements));
return ret;
}

@SuppressWarnings({"unchecked", "rawtypes"})
private static <T> CompletableFuture<List<T>> csToListCf(CompletionStage<? extends T> s) {
return (CompletableFuture) s.thenApply(CompletableFutureUtils::arrayList).toCompletableFuture();
return s.toCompletableFuture().thenApply(CompletableFutureUtils::arrayList);
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand All @@ -175,11 +175,11 @@ private static void fill(CompletionStage[] cfs,
for (int i = 0; i < cfs.length; i++) {
final CompletionStage cf = cfs[i];

successOrBeIncomplete[i] = cf.handle((v, ex) -> ex == null ? cf : incomplete)
.thenCompose(Function.identity()).toCompletableFuture();
successOrBeIncomplete[i] = cf.toCompletableFuture()
.handle((v, ex) -> ex == null ? cf : incomplete).thenCompose(Function.identity());

failedOrBeIncomplete[i] = cf.handle((v, ex) -> ex == null ? incomplete : cf)
.thenCompose(Function.identity()).toCompletableFuture();
failedOrBeIncomplete[i] = cf.toCompletableFuture()
.handle((v, ex) -> ex == null ? incomplete : cf).thenCompose(Function.identity());
}
}

Expand Down Expand Up @@ -271,8 +271,8 @@ public static <T1, T2> CompletableFuture<Tuple2<T1, T2>> allTupleOf(

final Object[] result = new Object[2];
return CompletableFuture.allOf(
cf1.thenAccept(t1 -> result[0] = t1).toCompletableFuture(),
cf2.thenAccept(t2 -> result[1] = t2).toCompletableFuture()
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2)
).thenApply(unused ->
Tuple2.of((T1) result[0], (T2) result[1])
);
Expand Down Expand Up @@ -325,9 +325,9 @@ public static <T1, T2, T3> CompletableFuture<Tuple3<T1, T2, T3>> allTupleOf(

final Object[] result = new Object[3];
return CompletableFuture.allOf(
cf1.thenAccept(t1 -> result[0] = t1).toCompletableFuture(),
cf2.thenAccept(t2 -> result[1] = t2).toCompletableFuture(),
cf3.thenAccept(t3 -> result[2] = t3).toCompletableFuture()
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2),
cf3.toCompletableFuture().thenAccept(t3 -> result[2] = t3)
).thenApply(unused ->
Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2])
);
Expand Down Expand Up @@ -382,10 +382,10 @@ public static <T1, T2, T3, T4> CompletableFuture<Tuple4<T1, T2, T3, T4>> allTupl

final Object[] result = new Object[4];
return CompletableFuture.allOf(
cf1.thenAccept(t1 -> result[0] = t1).toCompletableFuture(),
cf2.thenAccept(t2 -> result[1] = t2).toCompletableFuture(),
cf3.thenAccept(t3 -> result[2] = t3).toCompletableFuture(),
cf4.thenAccept(t4 -> result[3] = t4).toCompletableFuture()
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2),
cf3.toCompletableFuture().thenAccept(t3 -> result[2] = t3),
cf4.toCompletableFuture().thenAccept(t4 -> result[3] = t4)
).thenApply(unused ->
Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3])
);
Expand Down Expand Up @@ -442,11 +442,11 @@ public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1, T2, T3, T4, T5>>

final Object[] result = new Object[5];
return CompletableFuture.allOf(
cf1.thenAccept(t1 -> result[0] = t1).toCompletableFuture(),
cf2.thenAccept(t2 -> result[1] = t2).toCompletableFuture(),
cf3.thenAccept(t3 -> result[2] = t3).toCompletableFuture(),
cf4.thenAccept(t4 -> result[3] = t4).toCompletableFuture(),
cf5.thenAccept(t5 -> result[4] = t5).toCompletableFuture()
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2),
cf3.toCompletableFuture().thenAccept(t3 -> result[2] = t3),
cf4.toCompletableFuture().thenAccept(t4 -> result[3] = t4),
cf5.toCompletableFuture().thenAccept(t5 -> result[4] = t5)
).thenApply(unused ->
Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4])
);
Expand Down Expand Up @@ -511,7 +511,7 @@ public static CompletableFuture<Void> runAfterBothFastFail(
requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(action, "action is null");

return allOfFastFail(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenRun(action);
return allOfFastFail(cf1, cf2).thenRun(action);
}

/**
Expand All @@ -533,7 +533,7 @@ public static CompletableFuture<Void> runAfterBothFastFailAsync(
requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(action, "action is null");

return allOfFastFail(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenRunAsync(action);
return allOfFastFail(cf1, cf2).thenRunAsync(action);
}

/**
Expand All @@ -556,7 +556,7 @@ public static CompletableFuture<Void> runAfterBothFastFailAsync(
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

return allOfFastFail(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenRunAsync(action, executor);
return allOfFastFail(cf1, cf2).thenRunAsync(action, executor);
}

/**
Expand All @@ -582,8 +582,8 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFail(

final Object[] result = new Object[2];
return allOfFastFail(
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2)
cf1.thenAccept(t1 -> result[0] = t1),
cf2.thenAccept(t2 -> result[1] = t2)
).thenAccept(unused -> action.accept((T) result[0], (U) result[1]));
}

Expand Down Expand Up @@ -611,8 +611,8 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(

final Object[] result = new Object[2];
return allOfFastFail(
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2)
cf1.thenAccept(t1 -> result[0] = t1),
cf2.thenAccept(t2 -> result[1] = t2)
).thenAcceptAsync(unused -> action.accept((T) result[0], (U) result[1]));
}

Expand Down Expand Up @@ -641,8 +641,8 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(

final Object[] result = new Object[2];
return allOfFastFail(
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2)
cf1.thenAccept(t1 -> result[0] = t1),
cf2.thenAccept(t2 -> result[1] = t2)
).thenAcceptAsync(unused -> action.accept((T) result[0], (U) result[1]), executor);
}

Expand All @@ -669,8 +669,8 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFail(

final Object[] result = new Object[2];
return allOfFastFail(
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2)
cf1.thenAccept(t1 -> result[0] = t1),
cf2.thenAccept(t2 -> result[1] = t2)
).thenApply(unused -> fn.apply((T) result[0], (U) result[1]));
}

Expand Down Expand Up @@ -699,8 +699,8 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(

final Object[] result = new Object[2];
return allOfFastFail(
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2)
cf1.thenAccept(t1 -> result[0] = t1),
cf2.thenAccept(t2 -> result[1] = t2)
).thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1]));
}

Expand Down Expand Up @@ -730,8 +730,8 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(

final Object[] result = new Object[2];
return allOfFastFail(
cf1.toCompletableFuture().thenAccept(t1 -> result[0] = t1),
cf2.toCompletableFuture().thenAccept(t2 -> result[1] = t2)
cf1.thenAccept(t1 -> result[0] = t1),
cf2.thenAccept(t2 -> result[1] = t2)
).thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1]), executor);
}

Expand Down Expand Up @@ -761,7 +761,7 @@ public static CompletableFuture<Void> runAfterEitherSuccess(
requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(action, "action is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenRun(action);
return anyOfSuccess(cf1, cf2).thenRun(action);
}

/**
Expand All @@ -783,7 +783,7 @@ public static CompletableFuture<Void> runAfterEitherSuccessAsync(
requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(action, "action is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenRunAsync(action);
return anyOfSuccess(cf1, cf2).thenRunAsync(action);
}

/**
Expand All @@ -806,7 +806,7 @@ public static CompletableFuture<Void> runAfterEitherSuccessAsync(
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenRunAsync(action, executor);
return anyOfSuccess(cf1, cf2).thenRunAsync(action, executor);
}

/**
Expand All @@ -825,7 +825,7 @@ public static <T> CompletableFuture<Void> acceptEitherSuccess(
requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(action, "action is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenAccept(action);
return anyOfSuccess(cf1, cf2).thenAccept(action);
}

/**
Expand All @@ -845,7 +845,7 @@ public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(
requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(action, "action is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenAcceptAsync(action);
return anyOfSuccess(cf1, cf2).thenAcceptAsync(action);
}

/**
Expand All @@ -867,7 +867,7 @@ public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenAcceptAsync(action, executor);
return anyOfSuccess(cf1, cf2).thenAcceptAsync(action, executor);
}

/**
Expand All @@ -887,7 +887,7 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccess(
requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(fn, "fn is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenApply(fn);
return anyOfSuccess(cf1, cf2).thenApply(fn);
}

/**
Expand All @@ -908,7 +908,7 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(
requireCfsAndEleNonNull(cf1, cf2);
requireNonNull(fn, "fn is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenApplyAsync(fn);
return anyOfSuccess(cf1, cf2).thenApplyAsync(fn);
}

/**
Expand All @@ -931,7 +931,7 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(
requireNonNull(fn, "fn is null");
requireNonNull(executor, "executor is null");

return anyOfSuccess(cf1.toCompletableFuture(), cf2.toCompletableFuture()).thenApplyAsync(fn, executor);
return anyOfSuccess(cf1, cf2).thenApplyAsync(fn, executor);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit f2af962

Please sign in to comment.