diff --git a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java index d3240bfd..2e5a87c5 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -14,6 +14,7 @@ import java.util.concurrent.CompletionStage; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; +import java.util.function.Function; import java.util.function.Supplier; import static java.util.Objects.requireNonNull; @@ -546,6 +547,271 @@ public Cffu> mostTupleOfSuccess( return create(CompletableFutureUtils.mostTupleOfSuccess(defaultExecutor, timeout, unit, cf1, cf2, cf3, cf4, cf5)); } + /** + * Returns a new Cffu that is successful when the given three stages success. + * If any of the given stages complete exceptionally, then the returned Cffu also does so + * *without* waiting other incomplete given stages, with a CompletionException holding this exception as its cause. + *

+ * This method is the same as {@link #allTupleOfMSupplyAsync(Supplier, Supplier)} + * except for the fast-fail behavior. + * + * @return a new Cffu that is successful when the given three stages success + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOfFastFail(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfMSupplyFastFailAsync( + Supplier supplier1, Supplier supplier2) { + return create(CompletableFutureUtils.allTupleOfMSupplyFastFailAsync(supplier1, supplier2)); + } + + /** + * Returns a new Cffu that is completed when the given three stages complete. + * If any of the given stages complete exceptionally, then the returned Cffu also does so, + * with a CompletionException holding this exception as its cause. + * + * @return a new Cffu that is completed when the given three stages complete + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOf(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfMSupplyAsync( + Supplier supplier1, Supplier supplier2) { + return create(CompletableFutureUtils.allTupleOfMSupplyAsync(supplier1, supplier2)); + } + + /** + * Returns a new Cffu that is successful when the given three stages success. + * If any of the given stages complete exceptionally, then the returned Cffu also does so + * *without* waiting other incomplete given stages, with a CompletionException holding this exception as its cause. + *

+ * This method is the same as {@link #allTupleOfMSupplyAsync(Supplier, Supplier, Supplier)} + * except for the fast-fail behavior. + * + * @return a new Cffu that is successful when the given three stages success + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOfFastFail(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfMSupplyFastFailAsync( + Supplier supplier1, Supplier supplier2, Supplier supplier3) { + return create(CompletableFutureUtils.allTupleOfMSupplyFastFailAsync(supplier1, supplier2,supplier3)); + } + + /** + * Returns a new Cffu that is completed when the given three stages complete. + * If any of the given stages complete exceptionally, then the returned Cffu also does so, + * with a CompletionException holding this exception as its cause. + * + * @return a new Cffu that is completed when the given three stages complete + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOf(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfMSupplyAsync( + Supplier supplier1, Supplier supplier2, Supplier supplier3) { + return create(CompletableFutureUtils.allTupleOfMSupplyAsync(supplier1, supplier2,supplier3)); + } + + /** + * Returns a new Cffu that is successful when the given three stages success. + * If any of the given stages complete exceptionally, then the returned Cffu also does so + * *without* waiting other incomplete given stages, with a CompletionException holding this exception as its cause. + *

+ * This method is the same as {@link #allTupleOfMSupplyAsync(Supplier, Supplier, Supplier, Supplier)} + * except for the fast-fail behavior. + * + * @return a new Cffu that is successful when the given three stages success + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOfFastFail(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfMSupplyFastFailAsync( + Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { + return create(CompletableFutureUtils.allTupleOfMSupplyFastFailAsync(supplier1, supplier2,supplier3,supplier4)); + } + + /** + * Returns a new Cffu that is completed when the given three stages complete. + * If any of the given stages complete exceptionally, then the returned Cffu also does so, + * with a CompletionException holding this exception as its cause. + * + * @return a new Cffu that is completed when the given three stages complete + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOf(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfMSupplyAsync( + Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { + return create(CompletableFutureUtils.allTupleOfMSupplyAsync(supplier1,supplier2,supplier3,supplier4)); + } + + + /** + * Returns a new Cffu that is successful when the given three stages success. + * If any of the given stages complete exceptionally, then the returned Cffu also does so + * *without* waiting other incomplete given stages, with a CompletionException holding this exception as its cause. + *

+ * This method is the same as {@link #allTupleOfMSupplyAsync(Supplier, Supplier, Supplier, Supplier, Supplier)} + * except for the fast-fail behavior. + * + * @return a new Cffu that is successful when the given three stages success + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOfFastFail(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfMSupplyFastFailAsync( + Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { + return create(CompletableFutureUtils.allTupleOfMSupplyFastFailAsync(supplier1, supplier2,supplier3,supplier4,supplier5)); + } + + /** + * Returns a new Cffu that is completed when the given three stages complete. + * If any of the given stages complete exceptionally, then the returned Cffu also does so, + * with a CompletionException holding this exception as its cause. + * + * @return a new Cffu that is completed when the given three stages complete + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOf(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfMSupplyAsync( + Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { + return create(CompletableFutureUtils.allTupleOfMSupplyAsync(supplier1,supplier2,supplier3,supplier4,supplier5)); + } + + + /** + * Returns a new Cffu that is successful when the given three stages success. + * If any of the given stages complete exceptionally, then the returned Cffu also does so + * *without* waiting other incomplete given stages, with a CompletionException holding this exception as its cause. + *

+ * This method is the same as {@link #allTupleOfThenMApplyAsync(CompletionStage,Function, Function)} + * except for the fast-fail behavior. + * + * @return a new Cffu that is successful when the given three stages success + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOfFastFail(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfThenMApplyFastFailAsync( + CompletionStage cf, Function function1,Function function2) { + return create(CompletableFutureUtils.allTupleOfThenMApplyFastFailAsync(cf,function1, function2)); + } + /** + * Returns a new Cffu that is successful when the given three stages success. + * If any of the given stages complete exceptionally, then the returned Cffu also does so + * *without* waiting other incomplete given stages, with a CompletionException holding this exception as its cause. + *

+ * This method is the same as {@link #allTupleOfThenMApplyAsync(CompletionStage,Function, Function,Function)} + * except for the fast-fail behavior. + * + * @return a new Cffu that is successful when the given three stages success + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOfFastFail(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfThenMApplyFastFailAsync( + CompletionStage cf, Function function1,Function function2,Function function3) { + return create(CompletableFutureUtils.allTupleOfThenMApplyFastFailAsync(cf,function1, function2,function3)); + } + + /** + * Returns a new Cffu that is successful when the given three stages success. + * If any of the given stages complete exceptionally, then the returned Cffu also does so + * *without* waiting other incomplete given stages, with a CompletionException holding this exception as its cause. + *

+ * This method is the same as {@link #allTupleOfThenMApplyAsync(CompletionStage,Function, Function,Function,Function)} + * except for the fast-fail behavior. + * + * @return a new Cffu that is successful when the given three stages success + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOfFastFail(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfThenMApplyFastFailAsync( + CompletionStage cf, Function function1,Function function2,Function function3,Function function4) { + return create(CompletableFutureUtils.allTupleOfThenMApplyFastFailAsync(cf,function1, function2,function3,function4)); + } + + /** + * Returns a new Cffu that is successful when the given three stages success. + * If any of the given stages complete exceptionally, then the returned Cffu also does so + * *without* waiting other incomplete given stages, with a CompletionException holding this exception as its cause. + *

+ * This method is the same as {@link #allTupleOfThenMApplyAsync(CompletionStage,Function, Function,Function, Function,Function)} + * except for the fast-fail behavior. + * + * @return a new Cffu that is successful when the given three stages success + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOfFastFail(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfThenMApplyFastFailAsync( + CompletionStage cf, Function function1,Function function2,Function function3,Function function4,Function function5) { + return create(CompletableFutureUtils.allTupleOfThenMApplyFastFailAsync(cf,function1, function2,function3,function4,function5)); + } + + /** + * Returns a new Cffu that is completed when the given three stages complete. + * If any of the given stages complete exceptionally, then the returned Cffu also does so, + * with a CompletionException holding this exception as its cause. + * + * @return a new Cffu that is completed when the given three stages complete + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOf(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfThenMApplyAsync( + CompletionStage cf,Function function1,Function function2) { + return create(CompletableFutureUtils.allTupleOfThenMApplyAsync(cf,function1, function2)); + } + + /** + * Returns a new Cffu that is completed when the given three stages complete. + * If any of the given stages complete exceptionally, then the returned Cffu also does so, + * with a CompletionException holding this exception as its cause. + * + * @return a new Cffu that is completed when the given three stages complete + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOf(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfThenMApplyAsync( + CompletionStage cf,Function function1,Function function2,Function function3) { + return create(CompletableFutureUtils.allTupleOfThenMApplyAsync(cf,function1, function2,function3)); + } + /** + * Returns a new Cffu that is completed when the given three stages complete. + * If any of the given stages complete exceptionally, then the returned Cffu also does so, + * with a CompletionException holding this exception as its cause. + * + * @return a new Cffu that is completed when the given three stages complete + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOf(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfThenMApplyAsync( + CompletionStage cf,Function function1,Function function2,Function function3,Function function4) { + return create(CompletableFutureUtils.allTupleOfThenMApplyAsync(cf,function1, function2,function3,function4)); + } + + /** + * Returns a new Cffu that is completed when the given three stages complete. + * If any of the given stages complete exceptionally, then the returned Cffu also does so, + * with a CompletionException holding this exception as its cause. + * + * @return a new Cffu that is completed when the given three stages complete + * @throws NullPointerException if any of the given stages are {@code null} + * @see #allResultsOf(CompletionStage[]) + */ + @Contract(pure = true) + public Cffu> allTupleOfThenMApplyAsync( + CompletionStage cf,Function function1,Function function2,Function function3,Function function4,Function function5) { + return create(CompletableFutureUtils.allTupleOfThenMApplyAsync(cf,function1, function2,function3,function4,function5)); + } + + // endregion //////////////////////////////////////////////////////////////////////////////// // region## Immediate Value Argument Factory Methods 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 1fe4cb25..5a3b0d5c 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -77,6 +77,183 @@ 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> allTupleOfMSupplyFastFailAsync(Supplier supplier1,Supplier supplier2) { + return allTupleOfMSupplyFastFailAsync(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> allTupleOfMSupplyFastFailAsync( + 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 + * 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 + * @param supplier3 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> allTupleOfMSupplyFastFailAsync(Supplier supplier1,Supplier supplier2,Supplier supplier3) { + return allTupleOfMSupplyFastFailAsync(AsyncPoolHolder.ASYNC_POOL,supplier1,supplier2,supplier3); + } + + /** + * 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 + * @param supplier3 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> allTupleOfMSupplyFastFailAsync( + 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), true); + } + + /** + * 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 + * @param supplier3 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier4 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> allTupleOfMSupplyFastFailAsync(Supplier supplier1,Supplier supplier2,Supplier supplier3,Supplier supplier4) { + return allTupleOfMSupplyFastFailAsync(AsyncPoolHolder.ASYNC_POOL,supplier1,supplier2,supplier3,supplier4); + } + + /** + * 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 + * @param supplier3 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier4 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> allTupleOfMSupplyFastFailAsync( + Executor executor, Supplier supplier1,Supplier supplier2,Supplier supplier3,Supplier supplier4) { + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("supplier", supplier1,supplier2,supplier3,supplier4); + return allTupleOf0(wrapSuppliers(executor,supplier1,supplier2,supplier3,supplier4), true); + } + + + /** + * 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 + * @param supplier3 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier4 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier5 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> allTupleOfMSupplyFastFailAsync(Supplier supplier1,Supplier supplier2,Supplier supplier3,Supplier supplier4,Supplier supplier5) { + return allTupleOfMSupplyFastFailAsync(AsyncPoolHolder.ASYNC_POOL,supplier1,supplier2,supplier3,supplier4,supplier5); + } + + /** + * 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 + * @param supplier3 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier4 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier5 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> allTupleOfMSupplyFastFailAsync( + Executor executor, Supplier supplier1,Supplier supplier2,Supplier supplier3,Supplier supplier4,Supplier supplier5) { + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("supplier", supplier1,supplier2,supplier3,supplier4,supplier5); + return allTupleOf0(wrapSuppliers(executor,supplier1,supplier2,supplier3,supplier4,supplier5), true); + } + /** * Returns a new CompletableFuture that is asynchronously completed * by tasks running in the CompletableFuture's default asynchronous execution facility @@ -169,6 +346,166 @@ 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); + } + + /** + * 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 supplier4 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,Supplier supplier4) { + return allTupleOfMSupplyAsync(AsyncPoolHolder.ASYNC_POOL,supplier1,supplier2,supplier3,supplier4); + } + + /** + * 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 supplier4 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,Supplier supplier4) { + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("supplier", supplier1,supplier2,supplier3,supplier4); + return allTupleOf0(wrapSuppliers(executor,supplier1,supplier2,supplier3,supplier4), 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 + * @param supplier4 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier5 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,Supplier supplier4,Supplier supplier5) { + return allTupleOfMSupplyAsync(AsyncPoolHolder.ASYNC_POOL,supplier1,supplier2,supplier3,supplier4,supplier5); + } + + /** + * 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 supplier4 the suppliers returning the value to be used to complete the returned CompletableFuture + * @param supplier5 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,Supplier supplier4,Supplier supplier5) { + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("supplier", supplier1,supplier2,supplier3,supplier4,supplier5); + return allTupleOf0(wrapSuppliers(executor,supplier1,supplier2,supplier3,supplier4,supplier5), false); + } + @SafeVarargs private static T[] requireArrayAndEleNonNull(String varName, T... array) { requireNonNull(array, varName + "s is null"); @@ -179,7 +516,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++) { @@ -1281,8 +1618,387 @@ 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); + CompletableFuture cf1= toNonMinCf(cf); + CompletableFuture completableFuture1 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function1.apply(v), executor)); + CompletableFuture completableFuture2 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function2.apply(v), executor)); + CompletionStage[] css = {completableFuture1,completableFuture2}; + return allTupleOf0(css,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 function3 the functions to use to compute the values of the returned CompletableFuture + * @return the new CompletableFuture + */ + @Contract(pure = true) + public static CompletableFuture> allTupleOfThenMApplyAsync( + CompletionStage cf,Function function1,Function function2,Function function3) { + return allTupleOfThenMApplyAsync(cf, AsyncPoolHolder.ASYNC_POOL, function1,function2,function3); + } + + /** + * 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 function3 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,Function function3) { + requireNonNull(cf, "cf is null"); + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("fn", function1,function2,function3); + + CompletableFuture cf1= toNonMinCf(cf); + CompletableFuture completableFuture1 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function1.apply(v), executor)); + CompletableFuture completableFuture2 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function2.apply(v), executor)); + CompletableFuture completableFuture3 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function3.apply(v), executor)); + CompletionStage[] css = {completableFuture1,completableFuture2,completableFuture3}; + return allTupleOf0(css,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 function3 the functions to use to compute the values of the returned CompletableFuture + * @param function4 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,Function function3,Function function4) { + return allTupleOfThenMApplyAsync(cf, AsyncPoolHolder.ASYNC_POOL, function1,function2,function3,function4); + } + + /** + * 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 function3 the functions to use to compute the values of the returned CompletableFuture + * @param function4 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,Function function3,Function function4) { + requireNonNull(cf, "cf is null"); + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("fn", function1,function2,function3,function4); + + CompletableFuture cf1= toNonMinCf(cf); + CompletableFuture completableFuture1 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function1.apply(v), executor)); + CompletableFuture completableFuture2 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function2.apply(v), executor)); + CompletableFuture completableFuture3 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function3.apply(v), executor)); + CompletableFuture completableFuture4 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function4.apply(v), executor)); + CompletionStage[] css = {completableFuture1,completableFuture2,completableFuture3,completableFuture4}; + return allTupleOf0(css,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 function3 the functions to use to compute the values of the returned CompletableFuture + * @param function4 the functions to use to compute the values of the returned CompletableFuture + * @param function5 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,Function function3,Function function4,Function function5) { + return allTupleOfThenMApplyAsync(cf, AsyncPoolHolder.ASYNC_POOL, function1,function2,function3,function4,function5); + } + + /** + * 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 function3 the functions to use to compute the values of the returned CompletableFuture + * @param function4 the functions to use to compute the values of the returned CompletableFuture + * @param function5 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,Function function3,Function function4,Function function5) { + requireNonNull(cf, "cf is null"); + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("fn", function1,function2,function3,function4,function5); + + CompletableFuture cf1= toNonMinCf(cf); + CompletableFuture completableFuture1 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function1.apply(v), executor)); + CompletableFuture completableFuture2 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function2.apply(v), executor)); + CompletableFuture completableFuture3 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function3.apply(v), executor)); + CompletableFuture completableFuture4 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function4.apply(v), executor)); + CompletableFuture completableFuture5 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function5.apply(v), executor)); + CompletionStage[] css = {completableFuture1,completableFuture2,completableFuture3,completableFuture4,completableFuture5}; + return allTupleOf0(css,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 + * @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); + CompletableFuture cf1= toNonMinCf(cf); + CompletableFuture completableFuture1 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function1.apply(v), executor)); + CompletableFuture completableFuture2 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function2.apply(v), executor)); + CompletionStage[] css = {completableFuture1,completableFuture2}; + return allTupleOf0(css,true); + } + + /** + * 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 function3 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,Function function3) { + return allTupleOfThenMApplyFastFailAsync(cf, AsyncPoolHolder.ASYNC_POOL, function1,function2,function3); + } + + /** + * 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 function3 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,Function function3) { + requireNonNull(cf, "cf is null"); + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("fn", function1,function2,function3); + + CompletableFuture cf1= toNonMinCf(cf); + CompletableFuture completableFuture1 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function1.apply(v), executor)); + CompletableFuture completableFuture2 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function2.apply(v), executor)); + CompletableFuture completableFuture3 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function3.apply(v), executor)); + CompletionStage[] css = {completableFuture1,completableFuture2,completableFuture3}; + return allTupleOf0(css,true); + } + + + /** + * 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 function3 the functions to use to compute the values of the returned CompletableFuture + * @param function4 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,Function function3,Function function4) { + return allTupleOfThenMApplyFastFailAsync(cf, AsyncPoolHolder.ASYNC_POOL, function1,function2,function3,function4); + } + + /** + * 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 function3 the functions to use to compute the values of the returned CompletableFuture + * @param function4 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,Function function3,Function function4) { + requireNonNull(cf, "cf is null"); + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("fn", function1,function2,function3,function4); + + CompletableFuture cf1= toNonMinCf(cf); + CompletableFuture completableFuture1 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function1.apply(v), executor)); + CompletableFuture completableFuture2 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function2.apply(v), executor)); + CompletableFuture completableFuture3 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function3.apply(v), executor)); + CompletableFuture completableFuture4 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function4.apply(v), executor)); + CompletionStage[] css = {completableFuture1,completableFuture2,completableFuture3,completableFuture4}; + return allTupleOf0(css,true); + } + + /** + * 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 function3 the functions to use to compute the values of the returned CompletableFuture + * @param function4 the functions to use to compute the values of the returned CompletableFuture + * @param function5 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,Function function3,Function function4,Function function5) { + return allTupleOfThenMApplyFastFailAsync(cf, AsyncPoolHolder.ASYNC_POOL, function1,function2,function3,function4,function5); + } + + /** + * 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 function3 the functions to use to compute the values of the returned CompletableFuture + * @param function4 the functions to use to compute the values of the returned CompletableFuture + * @param function5 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,Function function3,Function function4,Function function5) { + requireNonNull(cf, "cf is null"); + requireNonNull(executor, "executor is null"); + requireArrayAndEleNonNull("fn", function1,function2,function3,function4,function5); + + CompletableFuture cf1= toNonMinCf(cf); + CompletableFuture completableFuture1 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function1.apply(v), executor)); + CompletableFuture completableFuture2 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function2.apply(v), executor)); + CompletableFuture completableFuture3 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function3.apply(v), executor)); + CompletableFuture completableFuture4 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function4.apply(v), executor)); + CompletableFuture completableFuture5 = cf1.thenCompose(v->CompletableFuture.supplyAsync(() -> function5.apply(v), executor)); + CompletionStage[] css = {completableFuture1,completableFuture2,completableFuture3,completableFuture4,completableFuture5}; + return allTupleOf0(css,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++) { @@ -1292,6 +2008,7 @@ private static CompletableFuture[] wrapFunctions( return cfs; } + /** * Returns a new CompletableFuture that, when the given stage completes normally, * is executed using the CompletableFuture's default asynchronous execution facility, diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java index 46bfbd4e..245714fc 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -14,6 +14,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.concurrent.*; +import java.util.function.Function; +import java.util.function.Supplier; import static io.foldright.cffu.CompletableFutureUtils.failedFuture; import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray; @@ -624,6 +626,80 @@ void test_mostTupleOfSuccess() throws Exception { ).get()); } + @Test + void test_allTupleOfMSupplyAsync() throws Exception { + final Supplier supplier_n = () -> { + sleep(100); + return n; + }; + final Supplier supplier_s = () -> { + sleep(100); + return s; + }; + + final Supplier supplier_d = () -> { + sleep(100); + return d; + }; + final Supplier supplier_an = () -> { + sleep(100); + return anotherN; + }; + final Supplier supplier_nn = () -> { + sleep(100); + return n+n; + }; + assertEquals(Tuple2.of(n, s), cffuFactory.allTupleOfMSupplyAsync(supplier_n, supplier_s).get()); + assertEquals(Tuple2.of(n, s), cffuFactory.allTupleOfMSupplyFastFailAsync(supplier_n, supplier_s).get()); + + assertEquals(Tuple3.of(n, s, d), cffuFactory.allTupleOfMSupplyAsync(supplier_n, supplier_s, supplier_d).get()); + assertEquals(Tuple3.of(n, s, d), cffuFactory.allTupleOfMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d).get()); + + assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.allTupleOfMSupplyAsync(supplier_n, supplier_s, supplier_d, supplier_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.allTupleOfMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an).get()); + + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allTupleOfMSupplyAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allTupleOfMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); + } + + @Test + void test_allTupleOfThenMApplyAsync() throws Exception { + final CompletableFuture completed = completedFuture(n); + final Function function_n = (x) -> { + sleep(100); + return n; + }; + + final Function function_s = (x) -> { + sleep(100); + return s; + }; + + final Function function_d = (x) -> { + sleep(100); + return d; + }; + final Function function_an = (x) -> { + sleep(100); + return anotherN; + }; + final Function function_nn = (x) -> { + sleep(100); + return n+n; + }; + assertEquals(Tuple2.of(n, s), cffuFactory.allTupleOfThenMApplyAsync(completed,function_n, function_s).get()); + assertEquals(Tuple2.of(n, s), cffuFactory.allTupleOfThenMApplyFastFailAsync(completed,function_n, function_s).get()); + + assertEquals(Tuple3.of(n, s, d), cffuFactory.allTupleOfThenMApplyAsync(completed,function_n, function_s, function_d).get()); + assertEquals(Tuple3.of(n, s, d), cffuFactory.allTupleOfThenMApplyFastFailAsync(completed,function_n, function_s, function_d).get()); + + assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.allTupleOfThenMApplyAsync(completed,function_n, function_s, function_d, function_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.allTupleOfThenMApplyFastFailAsync(completed,function_n, function_s, function_d, function_an).get()); + + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allTupleOfThenMApplyAsync(completed,function_n, function_s, function_d, function_an, function_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.allTupleOfThenMApplyFastFailAsync(completed,function_n, function_s, function_d, function_an, function_nn).get()); + } + //////////////////////////////////////////////////////////////////////////////// //# Conversion (Static) Methods // diff --git a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java index 2da0b91b..92848b85 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -749,6 +749,82 @@ void test_allTupleOf_NotFastFail() throws Exception { ); } + + @Test + void test_allTupleOfMSupplyAsync() throws Exception { + final Supplier supplier_n = () -> { + sleep(100); + return n; + }; + final Supplier supplier_s = () -> { + sleep(100); + return s; + }; + + final Supplier supplier_d = () -> { + sleep(100); + return d; + }; + final Supplier supplier_an = () -> { + sleep(100); + return anotherN; + }; + final Supplier supplier_nn = () -> { + sleep(100); + return n+n; + }; + assertEquals(Tuple2.of(n, s), allTupleOfMSupplyAsync(supplier_n, supplier_s).get()); + assertEquals(Tuple2.of(n, s), allTupleOfMSupplyFastFailAsync(supplier_n, supplier_s).get()); + + assertEquals(Tuple3.of(n, s, d), allTupleOfMSupplyAsync(supplier_n, supplier_s, supplier_d).get()); + assertEquals(Tuple3.of(n, s, d), allTupleOfMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d).get()); + + assertEquals(Tuple4.of(n, s, d, anotherN), allTupleOfMSupplyAsync(supplier_n, supplier_s, supplier_d, supplier_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), allTupleOfMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an).get()); + + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), allTupleOfMSupplyAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), allTupleOfMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); + } + + + @Test + void test_allTupleOfThenMApplyAsync() throws Exception { + final CompletableFuture completed = completedFuture(n); + final Function function_n = (x) -> { + sleep(100); + return n; + }; + + final Function function_s = (x) -> { + sleep(100); + return s; + }; + + final Function function_d = (x) -> { + sleep(100); + return d; + }; + final Function function_an = (x) -> { + sleep(100); + return anotherN; + }; + final Function function_nn = (x) -> { + sleep(100); + return n+n; + }; + assertEquals(Tuple2.of(n, s), allTupleOfThenMApplyAsync(completed,function_n, function_s).get()); + assertEquals(Tuple2.of(n, s), allTupleOfThenMApplyFastFailAsync(completed,function_n, function_s).get()); + + assertEquals(Tuple3.of(n, s, d), allTupleOfThenMApplyAsync(completed,function_n, function_s, function_d).get()); + assertEquals(Tuple3.of(n, s, d), allTupleOfThenMApplyFastFailAsync(completed,function_n, function_s, function_d).get()); + + assertEquals(Tuple4.of(n, s, d, anotherN), allTupleOfThenMApplyAsync(completed,function_n, function_s, function_d, function_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), allTupleOfThenMApplyFastFailAsync(completed,function_n, function_s, function_d, function_an).get()); + + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), allTupleOfThenMApplyAsync(completed,function_n, function_s, function_d, function_an, function_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), allTupleOfThenMApplyFastFailAsync(completed,function_n, function_s, function_d, function_an, function_nn).get()); + } + @Test void test_mostTupleOfSuccess() throws Exception { final CompletableFuture completed = completedFuture(n);