From 56d1490377e79600af955023d949f744b1dae7b2 Mon Sep 17 00:00:00 2001 From: huhao80 Date: Fri, 21 Jun 2024 11:54:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0`Cffu`=E4=B8=AD2?= =?UTF-8?q?=E5=88=B05=E4=B8=AA`action`=E7=9A=84`tupleOf*`=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/foldright/cffu/CffuFactory.java | 266 ++++++++++++++++++ .../io/foldright/cffu/CffuFactoryTest.java | 76 +++++ 2 files changed, 342 insertions(+) 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/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 //