From de23bc8e3be5c06967e7359492bb66368e63cbf1 Mon Sep 17 00:00:00 2001 From: huhao80 Date: Thu, 20 Jun 2024 17:00:54 +0800 Subject: [PATCH] =?UTF-8?q?=20=E8=A1=A5=E5=85=A8=E7=9B=B8=E5=BA=94?= =?UTF-8?q?=E7=9A=84tupleOf*=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cffu/CompletableFutureUtils.java | 201 +++++++++++++++++- 1 file changed, 199 insertions(+), 2 deletions(-) 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 681020e9..f835d5b2 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -82,6 +82,48 @@ public static CompletableFuture> mSupplyFastFailAsync( return allResultsOfFastFail(wrapSuppliers(executor, suppliers)); } + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the values obtained by calling the given Suppliers + * in the same order of the given Suppliers arguments. + *

+ * This method is the same as {@link #mSupplyAsync(Supplier[])} except for the fast-fail behavior. + * + * @param supplier1 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the suppliers returning the value to be used to complete the returned CompletableFuture + * @return the new CompletableFuture + * @see #allResultsOfFastFail(CompletionStage[]) + * @see CompletableFuture#supplyAsync(Supplier) + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfMSupplyAsyncFastFailAsync(Supplier supplier1,Supplier supplier2) { + return allTupleOfMSupplyAsyncFastFailAsync(AsyncPoolHolder.ASYNC_POOL,supplier1,supplier2); + } + + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the given Executor with the values obtained by calling the given Suppliers + * in the same order of the given Suppliers arguments. + *

+ * This method is the same as {@link #mSupplyAsync(Executor, Supplier[])} except for the fast-fail behavior. + * + * @param executor the executor to use for asynchronous execution + * @param supplier1 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the suppliers returning the value to be used to complete the returned CompletableFuture + * @return the new CompletableFuture + * @see #allResultsOfFastFail(CompletionStage[]) + * @see CompletableFuture#supplyAsync(Supplier, Executor) + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfMSupplyAsyncFastFailAsync( + Executor executor, Supplier supplier1,Supplier supplier2) { + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("supplier", supplier1,supplier2); + return allTupleOf0(wrapSuppliers(executor,supplier1,supplier2), true); + } + + /** * Returns a new CompletableFuture that is asynchronously completed * by tasks running in the CompletableFuture's default asynchronous execution facility @@ -174,6 +216,83 @@ public static CompletableFuture> mSupplyAsync(Executor executor, Sup return allResultsOf(wrapSuppliers(executor, suppliers)); } + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the values obtained by calling the given Suppliers + * in the same order of the given Suppliers arguments. + * + * @param the suppliers' return type + * @return the new CompletableFuture + * @see #allResultsOf(CompletionStage[]) + * @see CompletableFuture#supplyAsync(Supplier) + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfMSupplyAsync(Supplier supplier1,Supplier supplier2) { + return allTupleOfMSupplyAsync(AsyncPoolHolder.ASYNC_POOL,supplier1,supplier2); + } + + + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the values obtained by calling the given Suppliers + * in the same order of the given Suppliers arguments. + * + * @param supplier1 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the suppliers returning the value to be used to complete the returned CompletableFuture + * @return the new CompletableFuture + * @see #allResultsOf(CompletionStage[]) + * @see CompletableFuture#supplyAsync(Supplier) + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfMSupplyAsync(Executor executor, + Supplier supplier1,Supplier supplier2) { + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("supplier", supplier1,supplier2); + return allTupleOf0(wrapSuppliers(executor,supplier1,supplier2), false); + } + + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the values obtained by calling the given Suppliers + * in the same order of the given Suppliers arguments. + * + * @param supplier1 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier3 the suppliers returning the value to be used to complete the returned CompletableFuture + * @return the new CompletableFuture + * @see #allResultsOf(CompletionStage[]) + * @see CompletableFuture#supplyAsync(Supplier) + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfMSupplyAsync(Supplier supplier1,Supplier supplier2,Supplier supplier3) { + return allTupleOfMSupplyAsync(AsyncPoolHolder.ASYNC_POOL,supplier1,supplier2,supplier3); + } + + /** + * Returns a new CompletableFuture that is asynchronously completed + * by tasks running in the CompletableFuture's default asynchronous execution facility + * with the values obtained by calling the given Suppliers + * in the same order of the given Suppliers arguments. + * + * @param supplier1 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier2 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier3 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param the suppliers' return type + * @return the new CompletableFuture + * @see #allResultsOf(CompletionStage[]) + * @see CompletableFuture#supplyAsync(Supplier) + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfMSupplyAsync(Executor executor,Supplier supplier1,Supplier supplier2,Supplier supplier3) { + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("supplier", supplier1,supplier2,supplier3); + return allTupleOf0(wrapSuppliers(executor,supplier1,supplier2,supplier3), false); + } + + @SafeVarargs private static T[] requireArrayAndEleNonNull(String varName, T... array) { requireNonNull(array, varName + "s is null"); @@ -184,7 +303,7 @@ private static T[] requireArrayAndEleNonNull(String varName, T... array) { } private static CompletableFuture[] wrapSuppliers( - Executor executor, Supplier[] suppliers) { + Executor executor, Supplier... suppliers) { @SuppressWarnings("unchecked") CompletableFuture[] cfs = new CompletableFuture[suppliers.length]; for (int i = 0; i < suppliers.length; i++) { @@ -1337,8 +1456,86 @@ public static CompletableFuture> thenMApplyAsync( return toNonMinCf(cf).thenCompose(v -> allResultsOf(wrapFunctions(executor, v, fns))); } + /** + * Returns a new CompletableFuture that, when the given stage completes normally, + * is executed using the CompletableFuture's default asynchronous execution facility, + * with the values obtained by calling the given Functions + * (with the given stage's result as the argument to the given functions) + * in the same order of the given Functions arguments. + * + * @param function1 the functions to use to compute the values of the returned CompletableFuture + * @param function2 the functions to use to compute the values of the returned CompletableFuture + * @param the functions' return type + * @return the new CompletableFuture + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfThenMApplyAsync( + CompletionStage cf,Function function1,Function function2) { + return allTupleOfThenMApplyAsync(cf, AsyncPoolHolder.ASYNC_POOL, function1,function2); + } + + /** + * Returns a new CompletableFuture that, when the given stage completes normally, + * is executed using the given Executor, with the values obtained by calling the given Functions + * (with the given stage's result as the argument to the given functions) + * in the same order of the given Functions arguments. + * + * @param function1 the functions to use to compute the values of the returned CompletableFuture + * @param function2 the functions to use to compute the values of the returned CompletableFuture + * @param the functions' return type + * @return the new CompletableFuture + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfThenMApplyAsync( + CompletionStage cf, Executor executor, Function function1,Function function2) { + requireNonNull(cf, "cf is null"); + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("fn", function1,function2); + + return toNonMinCf(cf).thenCompose(v -> allTupleOf0(wrapFunctions(executor, v, function1,function2),false)); + } + + /** + * Returns a new CompletableFuture that, when the given stage completes normally, + * is executed using the CompletableFuture's default asynchronous execution facility, + * with the values obtained by calling the given Functions + * (with the given stage's result as the argument to the given functions) + * in the same order of the given Functions arguments. + * + * @param function1 the functions to use to compute the values of the returned CompletableFuture + * @param function2 the functions to use to compute the values of the returned CompletableFuture + * @param the functions' return type + * @return the new CompletableFuture + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfThenMApplyFastFailAsync( + CompletionStage cf,Function function1,Function function2) { + return allTupleOfThenMApplyFastFailAsync(cf, AsyncPoolHolder.ASYNC_POOL, function1,function2); + } + + /** + * Returns a new CompletableFuture that, when the given stage completes normally, + * is executed using the given Executor, with the values obtained by calling the given Functions + * (with the given stage's result as the argument to the given functions) + * in the same order of the given Functions arguments. + * + * @param function1 the functions to use to compute the values of the returned CompletableFuture + * @param function2 the functions to use to compute the values of the returned CompletableFuture + * @param the functions' return type + * @return the new CompletableFuture + */ + @Contract(pure = true) + public static CompletableFuture allTupleOfThenMApplyFastFailAsync( + CompletionStage cf, Executor executor, Function function1,Function function2) { + requireNonNull(cf, "cf is null"); + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("fn", function1,function2); + + return toNonMinCf(cf).thenCompose(v -> allTupleOf0(wrapFunctions(executor, v, function1,function2),true)); + } + private static CompletableFuture[] wrapFunctions( - Executor executor, T v, Function[] fns) { + Executor executor, T v, Function... fns) { @SuppressWarnings("unchecked") CompletableFuture[] cfs = new CompletableFuture[fns.length]; for (int i = 0; i < fns.length; i++) {