diff --git a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java index 95a33c77..fd9d3387 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java +++ b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java @@ -3,6 +3,10 @@ import edu.umd.cs.findbugs.annotations.CheckReturnValue; import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import io.foldright.cffu.tuple.Tuple2; +import io.foldright.cffu.tuple.Tuple3; +import io.foldright.cffu.tuple.Tuple4; +import io.foldright.cffu.tuple.Tuple5; import org.jetbrains.annotations.Blocking; import org.jetbrains.annotations.Contract; @@ -201,6 +205,157 @@ public Cffu thenRunAsync(Runnable action, Executor executor) { return reset0(cf.thenRunAsync(action, executor)); } + //////////////////////////////////////////////////////////// + // region## Then-Multi-Actions(thenM*) Methods + //////////////////////////////////////////////////////////// + + // TODO: TO BE implemented!! + + // endregion + //////////////////////////////////////////////////////////// + // region## Then-Tuple-Multi-Actions(thenTupleM*) Methods + //////////////////////////////////////////////////////////// + + /** + * Returns a new Cffu that, when this Cffu completes normally, + * is executed using the {@link #defaultExecutor()}, + * with the values obtained by calling the given Functions + * (with this Cffu's result as the argument to the given functions) + * in the same order of the given Functions arguments. + *

+ * This method is the same as {@link #thenTupleMApplyAsync(Function, Function)} + * except for the fast-fail behavior. + * + * @return the new Cffu + */ + @Contract(pure = true) + public Cffu> thenTupleMApplyFastFailAsync( + Function fn1, Function fn2) { + return reset0(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, fn1, fn2)); + } + + /** + * Returns a new Cffu that, when this Cffu completes normally, + * is executed using the {@link #defaultExecutor()}, + * with the values obtained by calling the given Functions + * (with this Cffu's result as the argument to the given functions) + * in the same order of the given Functions arguments. + *

+ * This method is the same as {@link #thenTupleMApplyAsync(Function, Function, Function)} + * except for the fast-fail behavior. + * + * @return the new Cffu + */ + @Contract(pure = true) + public Cffu> thenTupleMApplyFastFailAsync( + Function fn1, Function fn2, + Function fn3) { + return reset0(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, fn1, fn2, fn3)); + } + + /** + * Returns a new Cffu that, when this Cffu completes normally, + * is executed using the {@link #defaultExecutor()}, + * with the values obtained by calling the given Functions + * (with this Cffu's result as the argument to the given functions) + * in the same order of the given Functions arguments. + *

+ * This method is the same as {@link #thenTupleMApplyAsync(Function, Function, Function, Function)} + * except for the fast-fail behavior. + * + * @return the new Cffu + */ + @Contract(pure = true) + public Cffu> thenTupleMApplyFastFailAsync( + Function fn1, Function fn2, + Function fn3, Function fn4) { + return reset0(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, fn1, fn2, fn3, fn4)); + } + + /** + * Returns a new Cffu that, when this Cffu completes normally, + * is executed using the {@link #defaultExecutor()}, + * with the values obtained by calling the given Functions + * (with this Cffu's result as the argument to the given functions) + * in the same order of the given Functions arguments. + *

+ * This method is the same as {@link #thenTupleMApplyAsync(Function, Function, Function, Function, Function)} + * except for the fast-fail behavior. + * + * @return the new Cffu + */ + @Contract(pure = true) + public Cffu> thenTupleMApplyFastFailAsync( + Function fn1, Function fn2, + Function fn3, Function fn4, + Function fn5) { + return reset0(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, fn1, fn2, fn3, fn4, fn5)); + } + + /** + * Returns a new Cffu that, when this Cffu completes normally, + * is executed using the {@link #defaultExecutor()}, + * with the values obtained by calling the given Functions + * (with this Cffu's result as the argument to the given functions) + * in the same order of the given Functions arguments. + * + * @return the new Cffu + */ + @Contract(pure = true) + public Cffu> thenTupleMApplyAsync( + Function fn1, Function fn2) { + return reset0(CompletableFutureUtils.thenTupleMApplyAsync(cf, fn1, fn2)); + } + + /** + * Returns a new Cffu that, when this Cffu completes normally, + * is executed using the {@link #defaultExecutor()}, + * with the values obtained by calling the given Functions + * (with this Cffu's result as the argument to the given functions) + * in the same order of the given Functions arguments. + * + * @return the new Cffu + */ + @Contract(pure = true) + public Cffu> thenTupleMApplyAsync( + Function fn1, Function fn2, + Function fn3) { + return reset0(CompletableFutureUtils.thenTupleMApplyAsync(cf, fn1, fn2, fn3)); + } + + /** + * Returns a new Cffu that, when this Cffu completes normally, + * is executed using the {@link #defaultExecutor()}, + * with the values obtained by calling the given Functions + * (with this Cffu's result as the argument to the given functions) + * in the same order of the given Functions arguments. + * + * @return the new Cffu + */ + @Contract(pure = true) + public Cffu> thenTupleMApplyAsync( + Function fn1, Function fn2, + Function fn3, Function fn4) { + return reset0(CompletableFutureUtils.thenTupleMApplyAsync(cf, fn1, fn2, fn3, fn4)); + } + + /** + * Returns a new Cffu that, when this Cffu completes normally, + * is executed using the {@link #defaultExecutor()}, + * with the values obtained by calling the given Functions + * (with this Cffu's result as the argument to the given functions) + * in the same order of the given Functions arguments. + * + * @return the new Cffu + */ + @Contract(pure = true) + public Cffu> thenTupleMApplyAsync( + Function fn1, Function fn2, + Function fn3, Function fn4, + Function fn5) { + return reset0(CompletableFutureUtils.thenTupleMApplyAsync(cf, fn1, fn2, fn3, fn4, fn5)); + } + // endregion //////////////////////////////////////////////////////////////////////////////// // region# thenBoth* Methods(binary input) of CompletionStage 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 e181c5a3..ce30a744 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -676,149 +676,6 @@ public Cffu> mostTupleOfSuccess( return create(CompletableFutureUtils.mostTupleOfSuccess(defaultExecutor, timeout, unit, cf1, cf2, cf3, cf4, cf5)); } - /** - * Returns a new Cffu that, when the given stage completes normally, - * is executed using the {@link #defaultExecutor()}, - * 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. - *

- * This method is the same as {@link #thenTupleMApplyAsync(CompletionStage, Function, Function)} - * except for the fast-fail behavior. - * - * @return the new Cffu - */ - @Contract(pure = true) - public Cffu> thenTupleMApplyFastFailAsync( - CompletionStage cf, Function fn1, Function fn2) { - return create(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, fn1, fn2)); - } - - /** - * Returns a new Cffu that, when the given stage completes normally, - * is executed using the {@link #defaultExecutor()}, - * 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. - *

- * This method is the same as {@link #thenTupleMApplyAsync(CompletionStage, Function, Function, Function)} - * except for the fast-fail behavior. - * - * @return the new Cffu - */ - @Contract(pure = true) - public Cffu> thenTupleMApplyFastFailAsync( - CompletionStage cf, Function fn1, - Function fn2, Function fn3) { - return create(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, fn1, fn2, fn3)); - } - - /** - * Returns a new Cffu that, when the given stage completes normally, - * is executed using the {@link #defaultExecutor()}, - * 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. - *

- * This method is the same as {@link #thenTupleMApplyAsync(CompletionStage, Function, Function, Function, Function)} - * except for the fast-fail behavior. - * - * @return the new Cffu - */ - @Contract(pure = true) - public Cffu> thenTupleMApplyFastFailAsync( - CompletionStage cf, - Function fn1, Function fn2, - Function fn3, Function fn4) { - return create(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, fn1, fn2, fn3, fn4)); - } - - /** - * Returns a new Cffu that, when the given stage completes normally, - * is executed using the {@link #defaultExecutor()}, - * 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. - *

- * This method is the same as {@link #thenTupleMApplyAsync(CompletionStage, Function, Function, Function, Function, Function)} - * except for the fast-fail behavior. - * - * @return the new Cffu - */ - @Contract(pure = true) - public Cffu> thenTupleMApplyFastFailAsync( - CompletionStage cf, Function fn1, - Function fn2, Function fn3, - Function fn4, Function fn5) { - return create(CompletableFutureUtils.thenTupleMApplyFastFailAsync(cf, fn1, fn2, fn3, fn4, fn5)); - } - - /** - * Returns a new Cffu that, when the given stage completes normally, - * is executed using the {@link #defaultExecutor()}, - * 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. - * - * @return the new Cffu - */ - @Contract(pure = true) - public Cffu> thenTupleMApplyAsync( - CompletionStage cf, - Function fn1, Function fn2) { - return create(CompletableFutureUtils.thenTupleMApplyAsync(cf, fn1, fn2)); - } - - /** - * Returns a new Cffu that, when the given stage completes normally, - * is executed using the {@link #defaultExecutor()}, - * 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. - * - * @return the new Cffu - */ - @Contract(pure = true) - public Cffu> thenTupleMApplyAsync( - CompletionStage cf, Function fn1, - Function fn2, Function fn3) { - return create(CompletableFutureUtils.thenTupleMApplyAsync(cf, fn1, fn2, fn3)); - } - - /** - * Returns a new Cffu that, when the given stage completes normally, - * is executed using the {@link #defaultExecutor()}, - * 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. - * - * @return the new Cffu - */ - @Contract(pure = true) - public Cffu> thenTupleMApplyAsync( - CompletionStage cf, - Function fn1, Function fn2, - Function fn3, Function fn4) { - return create(CompletableFutureUtils.thenTupleMApplyAsync(cf, fn1, fn2, fn3, fn4)); - } - - /** - * Returns a new Cffu that, when the given stage completes normally, - * is executed using the {@link #defaultExecutor()}, - * 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. - * - * @return the new Cffu - */ - @Contract(pure = true) - public Cffu> thenTupleMApplyAsync( - CompletionStage cf, Function fn1, - Function fn2, Function fn3, - Function fn4, Function fn5) { - return create(CompletableFutureUtils.thenTupleMApplyAsync(cf, fn1, fn2, fn3, fn4, fn5)); - } - // 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 ff68cd8b..df6891a2 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -662,44 +662,6 @@ void test_tupleMSupplyAsync() throws Exception { assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.tupleMSupplyFastFailAsync(supplier_n, supplier_s, supplier_d, supplier_an, supplier_nn).get()); } - @Test - void test_thenTupleMApplyAsync() 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.thenTupleMApplyAsync(completed,function_n, function_s).get()); - assertEquals(Tuple2.of(n, s), cffuFactory.thenTupleMApplyFastFailAsync(completed,function_n, function_s).get()); - - assertEquals(Tuple3.of(n, s, d), cffuFactory.thenTupleMApplyAsync(completed,function_n, function_s, function_d).get()); - assertEquals(Tuple3.of(n, s, d), cffuFactory.thenTupleMApplyFastFailAsync(completed,function_n, function_s, function_d).get()); - - assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.thenTupleMApplyAsync(completed,function_n, function_s, function_d, function_an).get()); - assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.thenTupleMApplyFastFailAsync(completed,function_n, function_s, function_d, function_an).get()); - - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.thenTupleMApplyAsync(completed,function_n, function_s, function_d, function_an, function_nn).get()); - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.thenTupleMApplyFastFailAsync(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/CffuTest.java b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java index 86d2617b..6d4c641d 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java @@ -1,5 +1,9 @@ package io.foldright.cffu; +import io.foldright.cffu.tuple.Tuple2; +import io.foldright.cffu.tuple.Tuple3; +import io.foldright.cffu.tuple.Tuple4; +import io.foldright.cffu.tuple.Tuple5; import io.foldright.test_utils.TestThreadPoolManager; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -10,6 +14,7 @@ import java.util.concurrent.*; import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.function.Function; import static io.foldright.test_utils.TestUtils.*; import static java.util.function.Function.identity; @@ -28,6 +33,44 @@ class CffuTest { private static CffuFactory forbidObtrudeMethodsCffuFactory; + @Test + void test_thenTupleMApplyAsync() throws Exception { + final Cffu completed = cffuFactory.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), completed.thenTupleMApplyAsync(function_n, function_s).get()); + assertEquals(Tuple2.of(n, s), completed.thenTupleMApplyFastFailAsync(function_n, function_s).get()); + + assertEquals(Tuple3.of(n, s, d), completed.thenTupleMApplyAsync(function_n, function_s, function_d).get()); + assertEquals(Tuple3.of(n, s, d), completed.thenTupleMApplyFastFailAsync(function_n, function_s, function_d).get()); + + assertEquals(Tuple4.of(n, s, d, anotherN), completed.thenTupleMApplyAsync(function_n, function_s, function_d, function_an).get()); + assertEquals(Tuple4.of(n, s, d, anotherN), completed.thenTupleMApplyFastFailAsync(function_n, function_s, function_d, function_an).get()); + + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), completed.thenTupleMApplyAsync(function_n, function_s, function_d, function_an, function_nn).get()); + assertEquals(Tuple5.of(n, s, d, anotherN, n + n), completed.thenTupleMApplyFastFailAsync(function_n, function_s, function_d, function_an, function_nn).get()); + } + //////////////////////////////////////////////////////////////////////////////// //# both methods ////////////////////////////////////////////////////////////////////////////////