Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jul 1, 2024
1 parent 78caa97 commit 89d99a6
Showing 1 changed file with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2376,7 +2376,9 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFail(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allFastFailOf(resultSetterCfs).thenApply(unused -> fn.apply((T) result[0], (U) result[1]));
CompletableFuture<V> ret = allFastFailOf(resultSetterCfs)
.thenApply(unused -> fn.apply((T) result[0], (U) result[1]));
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand Down Expand Up @@ -2418,8 +2420,9 @@ public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allFastFailOf(resultSetterCfs)
CompletableFuture<V> ret = allFastFailOf(resultSetterCfs)
.thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1]), executor);
return composeTypeAndResult0(cfThis, ret);
}

@SuppressWarnings("unchecked")
Expand All @@ -2428,6 +2431,19 @@ private static <T> CompletionStage<? extends T>[] requireThisAndOtherNonNull(
return new CompletionStage[]{requireNonNull(cfThis, "cfThis is null"), requireNonNull(other, "other is null")};
}

/**
* Returns a new CompletableFuture that its runtime type(including minimal-stage)
* is same as parameter {@code cfType} and its result is same as parameter {@code cfResult}.
*
* <strong>Implementation Note:</strong> Calling this method is necessary
* because {@link Cffu} internal use type CompletableFuture to represent minimal-stage(NOT type safe).
*/
@SuppressWarnings("unchecked")
private static <T, U> CompletableFuture<U> composeTypeAndResult0(
CompletableFuture<? extends T> cfType, CompletionStage<? extends U> cfResult) {
return cfType.handle((v, ex) -> null).thenCompose(v -> (CompletionStage<U>) cfResult);
}

/**
* Returns a new CompletableFuture that, when tow given stage both complete normally,
* is executed with the two results as arguments to the supplied action.
Expand All @@ -2448,7 +2464,9 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFail(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allFastFailOf(resultSetterCfs).thenRun(() -> action.accept((T) result[0], (U) result[1]));
CompletableFuture<Void> ret = allFastFailOf(resultSetterCfs)
.thenRun(() -> action.accept((T) result[0], (U) result[1]));
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand Down Expand Up @@ -2490,7 +2508,9 @@ public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(
final Object[] result = new Object[css.length];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(css, result);

return allFastFailOf(resultSetterCfs).thenRunAsync(() -> action.accept((T) result[0], (U) result[1]), executor);
CompletableFuture<Void> ret = allFastFailOf(resultSetterCfs)
.thenRunAsync(() -> action.accept((T) result[0], (U) result[1]), executor);
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand All @@ -2507,7 +2527,8 @@ public static CompletableFuture<Void> runAfterBothFastFail(
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");

return allFastFailOf(css).thenRun(action);
CompletableFuture<Void> ret = allFastFailOf(css).thenRun(action);
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand Down Expand Up @@ -2541,7 +2562,8 @@ public static CompletableFuture<Void> runAfterBothFastFailAsync(
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

return allFastFailOf(css).thenRunAsync(action, executor);
CompletableFuture<Void> ret = allFastFailOf(css).thenRunAsync(action, executor);
return composeTypeAndResult0(cfThis, ret);
}

// endregion
Expand All @@ -2566,7 +2588,8 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccess(
final CompletionStage<? extends T>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(fn, "fn is null");

return anySuccessOf(css).thenApply(fn);
CompletableFuture<U> ret = anySuccessOf(css).thenApply(fn);
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand Down Expand Up @@ -2599,7 +2622,8 @@ public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(
requireNonNull(fn, "fn is null");
requireNonNull(executor, "executor is null");

return anySuccessOf(css).thenApplyAsync(fn, executor);
CompletableFuture<U> ret = anySuccessOf(css).thenApplyAsync(fn, executor);
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand All @@ -2614,7 +2638,8 @@ public static <T> CompletableFuture<Void> acceptEitherSuccess(
final CompletionStage<? extends T>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");

return anySuccessOf(css).thenAccept(action);
CompletableFuture<Void> ret = anySuccessOf(css).thenAccept(action);
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand Down Expand Up @@ -2645,7 +2670,8 @@ public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

return anySuccessOf(css).thenAcceptAsync(action, executor);
CompletableFuture<Void> ret = anySuccessOf(css).thenAcceptAsync(action, executor);
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand All @@ -2662,7 +2688,8 @@ public static CompletableFuture<Void> runAfterEitherSuccess(
final CompletionStage<?>[] css = requireThisAndOtherNonNull(cfThis, other);
requireNonNull(action, "action is null");

return anySuccessOf(css).thenRun(action);
CompletableFuture<Void> ret = anySuccessOf(css).thenRun(action);
return composeTypeAndResult0(cfThis, ret);
}

/**
Expand Down Expand Up @@ -2696,7 +2723,8 @@ public static CompletableFuture<Void> runAfterEitherSuccessAsync(
requireNonNull(action, "action is null");
requireNonNull(executor, "executor is null");

return anySuccessOf(css).thenRunAsync(action, executor);
CompletableFuture<Void> ret = anySuccessOf(css).thenRunAsync(action, executor);
return composeTypeAndResult0(cfThis, ret);
}

// endregion
Expand Down

0 comments on commit 89d99a6

Please sign in to comment.