From 077b4aa7d4970563f92539530248f7c076b8f971 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 25 May 2024 19:44:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20remove=20`tuple*`=20methods=20in=20?= =?UTF-8?q?`Cffu`/`CompletableFutureExtensions.kt`=20=F0=9F=A7=B9=20?= =?UTF-8?q?=E2=9A=A0=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +- .../src/main/java/io/foldright/cffu/Cffu.java | 180 ----------- .../io/foldright/cffu/CffuFactoryTest.java | 99 +----- .../cffu/CompletableFutureUtilsTest.java | 27 ++ .../io/foldright/demo/AllTupleOfDemo.java | 3 +- .../kotlin/CompletableFutureExtensions.kt | 282 ------------------ .../test/CompletableFutureExtensionsTest.kt | 117 -------- 7 files changed, 31 insertions(+), 680 deletions(-) diff --git a/README.md b/README.md index 076f13f3..6b5eff21 100644 --- a/README.md +++ b/README.md @@ -333,8 +333,7 @@ public class AllTupleOfDemo { Cffu cffu1 = cffuFactory.completedFuture("21"); Cffu cffu2 = cffuFactory.completedFuture(42); - Cffu> allTuple = cffu1.allTupleOf(cffu2); - // or: cffuFactory.allTupleOf(cffu1, cffu2); + Cffu> allTuple = cffuFactory.allTupleOf(cffu1, cffu2); System.out.println(allTuple.get()); ////////////////////////////////////////////////// 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 35ef439e..0a3c4218 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java +++ b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java @@ -1,10 +1,6 @@ package io.foldright.cffu; import edu.umd.cs.findbugs.annotations.*; -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; @@ -562,182 +558,6 @@ public Cffu thenCombineFastFailAsync(CompletionStage othe return reset0(CompletableFutureUtils.thenCombineFastFailAsync(this, other, fn, executor)); } - //////////////////////////////////////////////////////////////////////////////// - //# convenient `allTupleOf` methods: - // providing these method is convenient for method chaining - // - // - allTupleOf(CompletionStage...) - // - allTupleOfFastFail(CompletionStage...) - //////////////////////////////////////////////////////////////////////////////// - - /** - * This method is the same as {@link CffuFactory#allTupleOf(CompletionStage, CompletionStage)}, - * providing this method is convenient for method chaining. - *

- * Calling this method - *

- * {@code allTuple = cffu.allTupleOf(cf2);} - *

- * is the same as: - *

- * {@code allTuple = cffu.cffuFactory().allTupleOf(cffu, cf2);} - * - * @return the new Cffu - * @see CffuFactory#allTupleOf(CompletionStage, CompletionStage) - */ - @Contract(pure = true) - public Cffu> allTupleOf(CompletionStage cf2) { - return fac.allTupleOf(cf, cf2); - } - - /** - * This method is the same as {@link CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage)}, - * providing this method is convenient for method chaining. - *

- * Calling this method - *

- * {@code allTuple = cffu.allTupleOfFastFail(cf2);} - *

- * is the same as: - *

- * {@code allTuple = cffu.cffuFactory().allTupleOfFastFail(cffu, cf2);} - * - * @return the new Cffu - * @see CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage) - */ - @Contract(pure = true) - public Cffu> allTupleOfFastFail(CompletionStage cf2) { - return fac.allTupleOfFastFail(cf, cf2); - } - - /** - * This method is the same as {@link CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage)}, - * providing this method is convenient for method chaining. - *

- * Calling this method - *

- * {@code allTuple = cffu.allTupleOf(cf2, cf3);} - *

- * is the same as: - *

- * {@code allTuple = cffu.cffuFactory().allTupleOf(cffu, cf2, cf3);} - * - * @return the new Cffu - * @see CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage) - */ - @Contract(pure = true) - public Cffu> allTupleOf( - CompletionStage cf2, CompletionStage cf3) { - return fac.allTupleOf(cf, cf2, cf3); - } - - /** - * This method is the same as {@link CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage)}, - * providing this method is convenient for method chaining. - *

- * Calling this method - *

- * {@code allTuple = cffu.allTupleOfFastFail(cf2, cf3);} - *

- * is the same as: - *

- * {@code allTuple = cffu.cffuFactory().allTupleOfFastFail(cffu, cf2, cf3);} - * - * @return the new Cffu - * @see CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage) - */ - @Contract(pure = true) - public Cffu> allTupleOfFastFail( - CompletionStage cf2, CompletionStage cf3) { - return fac.allTupleOfFastFail(cf, cf2, cf3); - } - - /** - * This method is the same as {@link CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage)}, - * providing this method is convenient for method chaining. - *

- * Calling this method - *

- * {@code allTuple = cffu.allTupleOf(cf2, cf3, cf4);} - *

- * is the same as: - *

- * {@code allTuple = cffu.cffuFactory().allTupleOf(cffu, cf2, cf3, cf4);} - * - * @return the new Cffu - * @see CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage) - */ - @Contract(pure = true) - public Cffu> allTupleOf( - CompletionStage cf2, CompletionStage cf3, CompletionStage cf4) { - return fac.allTupleOf(cf, cf2, cf3, cf4); - } - - /** - * This method is the same as {@link CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage)}, - * providing this method is convenient for method chaining. - *

- * Calling this method - *

- * {@code allTuple = cffu.allTupleOfFastFail(cf2, cf3, cf4);} - *

- * is the same as: - *

- * {@code allTuple = cffu.cffuFactory().allTupleOfFastFail(cffu, cf2, cf3, cf4);} - * - * @return the new Cffu - * @see CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage) - */ - @Contract(pure = true) - public Cffu> allTupleOfFastFail( - CompletionStage cf2, CompletionStage cf3, CompletionStage cf4) { - return fac.allTupleOfFastFail(cf, cf2, cf3, cf4); - } - - /** - * This method is the same as {@link CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)}, - * providing this method is convenient for method chaining. - *

- * Calling this method - *

- * {@code allTuple = cffu.allTupleOf(cf2, cf3, cf4, cf5);} - *

- * is the same as: - *

- * {@code allTuple = cffu.cffuFactory().allTupleOf(cffu, cf2, cf3, cf4, cf5);} - * - * @return the new Cffu - * @see CffuFactory#allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) - */ - @Contract(pure = true) - public Cffu> allTupleOf( - CompletionStage cf2, CompletionStage cf3, - CompletionStage cf4, CompletionStage cf5) { - return fac.allTupleOf(cf, cf2, cf3, cf4, cf5); - } - - /** - * This method is the same as {@link CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)}, - * providing this method is convenient for method chaining. - *

- * Calling this method - *

- * {@code allTuple = cffu.allTupleOfFastFail(cf2, cf3, cf4, cf5);} - *

- * is the same as: - *

- * {@code allTuple = cffu.cffuFactory().allTupleOfFastFail(cffu, cf2, cf3, cf4, cf5);} - * - * @return the new Cffu - * @see CffuFactory#allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) - */ - @Contract(pure = true) - public Cffu> allTupleOfFastFail( - CompletionStage cf2, CompletionStage cf3, - CompletionStage cf4, CompletionStage cf5) { - return fac.allTupleOfFastFail(cf, cf2, cf3, cf4, cf5); - } - //////////////////////////////////////////////////////////////////////////////// //# `then either(binary input)` methods of CompletionStage: // 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 60c2f6c5..f5d501c6 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -15,7 +15,8 @@ import java.util.Collections; import java.util.concurrent.*; -import static io.foldright.cffu.CompletableFutureUtils.*; +import static io.foldright.cffu.CompletableFutureUtils.failedFuture; +import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray; import static io.foldright.test_utils.TestUtils.*; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.ForkJoinPool.commonPool; @@ -514,54 +515,6 @@ void test_allTupleOf() throws Exception { cffuFactory.completedFuture(anotherN), cffuFactory.completedFuture(n + n) ).get()); - - //////////////////////////////////////////////////////////////////////////////// - - assertEquals(Tuple2.of(n, s), cffuFactory.completedFuture(n).allTupleOf( - completedFuture(s) - ).get()); - - assertEquals(Tuple3.of(n, s, d), cffuFactory.completedFuture(n).allTupleOf( - completedFuture(s), - completedFuture(d) - ).get()); - - assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).allTupleOf( - completedFuture(s), - completedFuture(d), - completedFuture(anotherN) - ).get()); - - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).allTupleOf( - completedFuture(s), - completedFuture(d), - completedFuture(anotherN), - completedFuture(n + n) - ).get()); - - //////////////////////////////////////////////////////////////////////////////// - - assertEquals(Tuple2.of(n, s), cffuFactory.completedFuture(n).allTupleOf( - cffuFactory.completedFuture(s) - ).get()); - - assertEquals(Tuple3.of(n, s, d), cffuFactory.completedFuture(n).allTupleOf( - cffuFactory.completedFuture(s), - cffuFactory.completedFuture(d) - ).get()); - - assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).allTupleOf( - cffuFactory.completedFuture(s), - cffuFactory.completedFuture(d), - cffuFactory.completedFuture(anotherN) - ).get()); - - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).allTupleOf( - cffuFactory.completedFuture(s), - cffuFactory.completedFuture(d), - cffuFactory.completedFuture(anotherN), - cffuFactory.completedFuture(n + n) - ).get()); } @Test @@ -652,54 +605,6 @@ void test_allTupleOfFastFail() throws Exception { cffuFactory.completedFuture(anotherN), cffuFactory.completedFuture(n + n) ).get()); - - //////////////////////////////////////////////////////////////////////////////// - - assertEquals(Tuple2.of(n, s), cffuFactory.completedFuture(n).allTupleOfFastFail( - completedFuture(s) - ).get()); - - assertEquals(Tuple3.of(n, s, d), cffuFactory.completedFuture(n).allTupleOfFastFail( - completedFuture(s), - completedFuture(d) - ).get()); - - assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).allTupleOfFastFail( - completedFuture(s), - completedFuture(d), - completedFuture(anotherN) - ).get()); - - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).allTupleOfFastFail( - completedFuture(s), - completedFuture(d), - completedFuture(anotherN), - completedFuture(n + n) - ).get()); - - //////////////////////////////////////////////////////////////////////////////// - - assertEquals(Tuple2.of(n, s), cffuFactory.completedFuture(n).allTupleOfFastFail( - cffuFactory.completedFuture(s) - ).get()); - - assertEquals(Tuple3.of(n, s, d), cffuFactory.completedFuture(n).allTupleOfFastFail( - cffuFactory.completedFuture(s), - cffuFactory.completedFuture(d) - ).get()); - - assertEquals(Tuple4.of(n, s, d, anotherN), cffuFactory.completedFuture(n).allTupleOfFastFail( - cffuFactory.completedFuture(s), - cffuFactory.completedFuture(d), - cffuFactory.completedFuture(anotherN) - ).get()); - - assertEquals(Tuple5.of(n, s, d, anotherN, n + n), cffuFactory.completedFuture(n).allTupleOfFastFail( - cffuFactory.completedFuture(s), - cffuFactory.completedFuture(d), - cffuFactory.completedFuture(anotherN), - cffuFactory.completedFuture(n + n) - ).get()); } @Test 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 bed5387c..1e01a77d 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -772,6 +772,33 @@ void test_mostTupleOfSuccess() throws Exception { assertEquals(Tuple5.of(null, n, s, null, null), mostTupleOfSuccess( 10, TimeUnit.MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed ).get()); + + // with `executorWhenTimeout` + + assertEquals(Tuple2.of(n, s), mostTupleOfSuccess( + executorService, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted + ).get()); + assertEquals(Tuple2.of(n, null), mostTupleOfSuccess( + executorService, 10, TimeUnit.MILLISECONDS, completed, failed + ).get()); + + assertEquals(Tuple3.of(n, s, null), mostTupleOfSuccess( + executorService, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled + ).get()); + assertEquals(Tuple3.of(null, null, s), mostTupleOfSuccess( + executorService, 10, TimeUnit.MILLISECONDS, incomplete, failed, anotherCompleted + ).get()); + + assertEquals(Tuple4.of(n, s, null, null), mostTupleOfSuccess( + executorService, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, cancelled, incomplete + ).get()); + assertEquals(Tuple4.of(null, null, null, null), mostTupleOfSuccess( + executorService, 10, TimeUnit.MILLISECONDS, incomplete, failed, cancelled, incomplete + ).get()); + + assertEquals(Tuple5.of(null, n, s, null, null), mostTupleOfSuccess( + executorService, 10, TimeUnit.MILLISECONDS, cancelled, completed, anotherCompleted, incomplete, failed + ).get()); } //////////////////////////////////////////////////////////////////////////////// diff --git a/cffu-core/src/test/java/io/foldright/demo/AllTupleOfDemo.java b/cffu-core/src/test/java/io/foldright/demo/AllTupleOfDemo.java index 1dfa5db8..5d343951 100644 --- a/cffu-core/src/test/java/io/foldright/demo/AllTupleOfDemo.java +++ b/cffu-core/src/test/java/io/foldright/demo/AllTupleOfDemo.java @@ -21,8 +21,7 @@ public static void main(String[] args) throws Exception { Cffu cffu1 = cffuFactory.completedFuture("21"); Cffu cffu2 = cffuFactory.completedFuture(42); - Cffu> allTuple = cffu1.allTupleOf(cffu2); - // or: cffuFactory.allTupleOf(cffu1, cffu2); + Cffu> allTuple = cffuFactory.allTupleOf(cffu1, cffu2); System.out.println(allTuple.get()); ////////////////////////////////////////////////// diff --git a/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CompletableFutureExtensions.kt b/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CompletableFutureExtensions.kt index 59e1a9f3..5bb4c122 100644 --- a/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CompletableFutureExtensions.kt +++ b/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CompletableFutureExtensions.kt @@ -5,10 +5,6 @@ package io.foldright.cffu.kotlin import io.foldright.cffu.Cffu import io.foldright.cffu.CffuState import io.foldright.cffu.CompletableFutureUtils -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 java.util.concurrent.* import java.util.function.* import java.util.function.Function @@ -39,7 +35,6 @@ import java.util.function.Function * If you need the results of given stages, prefer below methods: * * - [allResultsOfCompletableFuture] - * - [allTupleOf] (provided overloaded methods with 2~5 input) * * This method is the same as [CompletableFutureUtils.allOf], providing this method is convenient for method chaining. * @@ -59,7 +54,6 @@ fun Collection>.allOfCompletableFuture(): CompletableFuture>.allOfCompletableFuture(): CompletableFuture>.allOfFastFailCompletableFuture(): Completable * If you need the results of given stages, prefer below methods: * * - [allResultsOfFastFailCompletableFuture] - * - [allTupleOfFastFail] (provided overloaded methods with 2~5 input) * * This method is the same as [CompletableFutureUtils.allOfFastFail], * providing this method is convenient for method chaining. @@ -490,280 +482,6 @@ fun CompletionStage.thenCombineFastFailAsync( ): CompletableFuture = CompletableFutureUtils.thenCombineFastFailAsync(this, other, fn, executor) -//////////////////////////////////////// -//# allTupleOf* methods -// -// - allTupleOf -// - allTupleOfFastFail -// - mostTupleOfSuccess -//////////////////////////////////////// - -/** - * Returns a new CompletableFuture that is completed when the given two CompletableFutures complete. - * If any of the given CompletableFutures complete exceptionally, then the returned - * CompletableFuture also does so, with a CompletionException holding this exception as its cause. - * - * @return a new CompletableFuture that is completed when the given 2 CompletableFutures complete - * @throws NullPointerException if any input CompletableFutures are `null` - * @see allResultsOfCompletableFuture - * @see allOfCompletableFuture - */ -fun CompletionStage.allTupleOf(cf2: CompletionStage): CompletableFuture> = - CompletableFutureUtils.allTupleOf(this, cf2) - -/** - * Returns a new CompletableFuture that is successful when the given two CompletableFutures success. - * If any of the given CompletableFutures complete exceptionally, then the returned - * CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, - * with a CompletionException holding this exception as its cause. - * - * @return a new CompletableFuture that is successful when the given two CompletableFutures success - * @throws NullPointerException if any of the given CompletableFutures are {@code null} - * @see allResultsOfFastFailCompletableFuture - */ -fun CompletionStage.allTupleOfFastFail(cf2: CompletionStage): CompletableFuture> = - CompletableFutureUtils.allTupleOfFastFail(this, cf2) - -/** - * Returns a new CompletableFuture that is completed when the given three CompletableFutures complete. - * If any of the given CompletableFutures complete exceptionally, then the returned - * CompletableFuture also does so, with a CompletionException holding this exception as its cause. - * - * @return a new CompletableFuture that is completed when the given 3 CompletableFutures complete - * @throws NullPointerException if any input CompletableFutures are `null` - * @see allResultsOfCompletableFuture - * @see allOfCompletableFuture - */ -fun CompletionStage.allTupleOf( - cf2: CompletionStage, cf3: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.allTupleOf(this, cf2, cf3) - -/** - * Returns a new CompletableFuture that is successful when the given three CompletableFutures success. - * If any of the given CompletableFutures complete exceptionally, then the returned - * CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, - * with a CompletionException holding this exception as its cause. - * - * @return a new CompletableFuture that is successful when the given three CompletableFutures success - * @throws NullPointerException if any of the given CompletableFutures are {@code null} - * @see allResultsOfFastFailCompletableFuture - */ -fun CompletionStage.allTupleOfFastFail( - cf2: CompletionStage, cf3: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.allTupleOfFastFail(this, cf2, cf3) - -/** - * Returns a new CompletableFuture that is completed when the given 4 CompletableFutures complete. - * If any of the given CompletableFutures complete exceptionally, then the returned - * CompletableFuture also does so, with a CompletionException holding this exception as its cause. - * - * @return a new CompletableFuture that is completed when the given 4 CompletableFutures complete - * @throws NullPointerException if any input CompletableFutures are `null` - * @see allResultsOfCompletableFuture - * @see allOfCompletableFuture - */ -fun CompletionStage.allTupleOf( - cf2: CompletionStage, cf3: CompletionStage, cf4: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.allTupleOf(this, cf2, cf3, cf4) - -/** - * Returns a new CompletableFuture that is successful when the given 4 CompletableFutures success. - * If any of the given CompletableFutures complete exceptionally, then the returned - * CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, - * with a CompletionException holding this exception as its cause. - * - * @return a new CompletableFuture that is successful when the given 4 CompletableFutures success - * @throws NullPointerException if any of the given CompletableFutures are {@code null} - * @see allResultsOfFastFailCompletableFuture - * @see allOfFastFailCompletableFuture - */ -fun CompletionStage.allTupleOfFastFail( - cf2: CompletionStage, cf3: CompletionStage, cf4: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.allTupleOfFastFail(this, cf2, cf3, cf4) - -/** - * Returns a new CompletableFuture that is completed when the given 5 CompletableFutures complete. - * If any of the given CompletableFutures complete exceptionally, then the returned - * CompletableFuture also does so, with a CompletionException holding this exception as its cause. - * - * @return a new CompletableFuture that is completed when the given 5 CompletableFutures complete - * @throws NullPointerException if any input CompletableFutures are `null` - * @see allResultsOfCompletableFuture - * @see allOfCompletableFuture - */ -fun CompletionStage.allTupleOf( - cf2: CompletionStage, cf3: CompletionStage, - cf4: CompletionStage, cf5: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.allTupleOf(this, cf2, cf3, cf4, cf5) - -/** - * Returns a new CompletableFuture that is successful when the given 5 CompletableFutures success. - * If any of the given CompletableFutures complete exceptionally, then the returned - * CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, - * with a CompletionException holding this exception as its cause. - * - * @return a new CompletableFuture that is successful when the given 5 CompletableFutures success - * @throws NullPointerException if any of the given CompletableFutures are {@code null} - * @see allResultsOfFastFailCompletableFuture - */ -fun CompletionStage.allTupleOfFastFail( - cf2: CompletionStage, cf3: CompletionStage, - cf4: CompletionStage, cf5: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.allTupleOfFastFail(this, cf2, cf3, cf4, cf5) - -/** - * Returns a new CompletableFuture with the most results in the **same order** of - * the given two stages in the given time(`timeout`), aka as many results as possible in the given time. - * - * If the given stage is successful, its result is the completed value; Otherwise the value `null`. - * - * @param timeout how long to wait in units of `unit` - * @param unit a `TimeUnit` determining how to interpret the `timeout` parameter - * @return a new CompletableFuture that is completed when the given two stages complete - * @see mostResultsOfSuccessCompletableFuture - * @see getSuccessNow - */ -fun CompletionStage.mostTupleOfSuccess( - timeout: Long, unit: TimeUnit, cf2: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, this, cf2) - -/** - * Returns a new CompletableFuture with the most results in the **same order** of - * the given two stages in the given time(`timeout`), aka as many results as possible in the given time. - * - * If the given stage is successful, its result is the completed value; Otherwise the value `null`. - * - * @param executorWhenTimeout the async executor when triggered by timeout - * @param timeout how long to wait in units of `unit` - * @param unit a `TimeUnit` determining how to interpret the `timeout` parameter - * @return a new CompletableFuture that is completed when the given two stages complete - * @see mostResultsOfSuccessCompletableFuture - * @see getSuccessNow - */ -fun CompletionStage.mostTupleOfSuccess( - executorWhenTimeout: Executor, timeout: Long, unit: TimeUnit, cf2: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.mostTupleOfSuccess(executorWhenTimeout, timeout, unit, this, cf2) - -/** - * Returns a new CompletableFuture with the most results in the **same order** of - * the given three stages in the given time(`timeout`), aka as many results as possible in the given time. - * - * If the given stage is successful, its result is the completed value; Otherwise the value `null`. - * - * @param timeout how long to wait in units of `unit` - * @param unit a `TimeUnit` determining how to interpret the `timeout` parameter - * @return a new CompletableFuture that is completed when the given three stages complete - * @see mostResultsOfSuccessCompletableFuture - * @see getSuccessNow - */ -fun CompletionStage.mostTupleOfSuccess( - timeout: Long, unit: TimeUnit, cf2: CompletionStage, cf3: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, this, cf2, cf3) - -/** - * Returns a new CompletableFuture with the most results in the **same order** of - * the given three stages in the given time(`timeout`), aka as many results as possible in the given time. - * - * If the given stage is successful, its result is the completed value; Otherwise the value `null`. - * - * @param executorWhenTimeout the async executor when triggered by timeout - * @param timeout how long to wait in units of `unit` - * @param unit a `TimeUnit` determining how to interpret the `timeout` parameter - * @return a new CompletableFuture that is completed when the given three stages complete - * @see mostResultsOfSuccessCompletableFuture - * @see getSuccessNow - */ -fun CompletionStage.mostTupleOfSuccess( - executorWhenTimeout: Executor, timeout: Long, unit: TimeUnit, - cf2: CompletionStage, cf3: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.mostTupleOfSuccess(executorWhenTimeout, timeout, unit, this, cf2, cf3) - -/** - * Returns a new CompletableFuture with the most results in the **same order** of - * the given four stages in the given time(`timeout`), aka as many results as possible in the given time. - * - * If the given stage is successful, its result is the completed value; Otherwise the value `null`. - * - * @param timeout how long to wait in units of `unit` - * @param unit a `TimeUnit` determining how to interpret the `timeout` parameter - * @return a new CompletableFuture that is completed when the given four stages complete - * @see mostResultsOfSuccessCompletableFuture - * @see getSuccessNow - */ -fun CompletionStage.mostTupleOfSuccess( - timeout: Long, unit: TimeUnit, - cf2: CompletionStage, cf3: CompletionStage, cf4: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, this, cf2, cf3, cf4) - -/** - * Returns a new CompletableFuture with the most results in the **same order** of - * the given four stages in the given time(`timeout`), aka as many results as possible in the given time. - * - * If the given stage is successful, its result is the completed value; Otherwise the value `null`. - * - * @param executorWhenTimeout the async executor when triggered by timeout - * @param timeout how long to wait in units of `unit` - * @param unit a `TimeUnit` determining how to interpret the `timeout` parameter - * @return a new CompletableFuture that is completed when the given four stages complete - * @see mostResultsOfSuccessCompletableFuture - * @see getSuccessNow - */ -fun CompletionStage.mostTupleOfSuccess( - executorWhenTimeout: Executor, timeout: Long, unit: TimeUnit, - cf2: CompletionStage, cf3: CompletionStage, cf4: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.mostTupleOfSuccess(executorWhenTimeout, timeout, unit, this, cf2, cf3, cf4) - -/** - * Returns a new CompletableFuture with the most results in the **same order** of - * the given five stages in the given time(`timeout`), aka as many results as possible in the given time. - * - * If the given stage is successful, its result is the completed value; Otherwise the value `null`. - * - * @param timeout how long to wait in units of `unit` - * @param unit a `TimeUnit` determining how to interpret the `timeout` parameter - * @return a new CompletableFuture that is completed when the given five stages complete - * @see mostResultsOfSuccessCompletableFuture - * @see getSuccessNow - */ -fun CompletionStage.mostTupleOfSuccess( - timeout: Long, unit: TimeUnit, - cf2: CompletionStage, cf3: CompletionStage, - cf4: CompletionStage, cf5: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, this, cf2, cf3, cf4, cf5) - -/** - * Returns a new CompletableFuture with the most results in the **same order** of - * the given five stages in the given time(`timeout`), aka as many results as possible in the given time. - * - * If the given stage is successful, its result is the completed value; Otherwise the value `null`. - * - * @param executorWhenTimeout the async executor when triggered by timeout - * @param timeout how long to wait in units of `unit` - * @param unit a `TimeUnit` determining how to interpret the `timeout` parameter - * @return a new CompletableFuture that is completed when the given five stages complete - * @see mostResultsOfSuccessCompletableFuture - * @see getSuccessNow - */ -fun CompletionStage.mostTupleOfSuccess( - executorWhenTimeout: Executor, timeout: Long, unit: TimeUnit, - cf2: CompletionStage, cf3: CompletionStage, - cf4: CompletionStage, cf5: CompletionStage -): CompletableFuture> = - CompletableFutureUtils.mostTupleOfSuccess(executorWhenTimeout, timeout, unit, this, cf2, cf3, cf4, cf5) - //////////////////////////////////////////////////////////////////////////////// //# `then either(binary input)` methods with either(any)-success support: // diff --git a/cffu-kotlin/src/test/java/io/foldright/cffu/test/CompletableFutureExtensionsTest.kt b/cffu-kotlin/src/test/java/io/foldright/cffu/test/CompletableFutureExtensionsTest.kt index 8380b1a9..13ccaf36 100644 --- a/cffu-kotlin/src/test/java/io/foldright/cffu/test/CompletableFutureExtensionsTest.kt +++ b/cffu-kotlin/src/test/java/io/foldright/cffu/test/CompletableFutureExtensionsTest.kt @@ -4,11 +4,6 @@ import io.foldright.cffu.CffuState import io.foldright.cffu.CompletableFutureUtils import io.foldright.cffu.NoCfsProvidedException import io.foldright.cffu.kotlin.* -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.createCancelledFuture import io.foldright.test_utils.createIncompleteFuture import io.foldright.test_utils.sleep import io.foldright.test_utils.testThreadPoolExecutor @@ -227,118 +222,6 @@ class CompletableFutureExtensionsTest : FunSpec({ } } - //////////////////////////////////////// - // allTupleOf - //////////////////////////////////////// - - test("allTupleOf - CompletableFuture") { - CompletableFuture.completedFuture(n).allTupleOf( - CompletableFuture.completedFuture(s) - ).get() shouldBe Tuple2.of(n, s) - - CompletableFuture.completedFuture(n).allTupleOf( - CompletableFuture.completedFuture(s), - CompletableFuture.completedFuture(d) - ).get() shouldBe Tuple3.of(n, s, d) - - CompletableFuture.completedFuture(n).allTupleOf( - CompletableFuture.completedFuture(s), - CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(anotherN) - ).get() shouldBe Tuple4.of(n, s, d, anotherN) - - CompletableFuture.completedFuture(n).allTupleOf( - CompletableFuture.completedFuture(s), - CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(anotherN), - CompletableFuture.completedFuture(n + n) - ).get() shouldBe Tuple5.of(n, s, d, anotherN, n + n) - } - - test("allTupleOfFastFail - CompletableFuture") { - CompletableFuture.completedFuture(n).allTupleOfFastFail( - CompletableFuture.completedFuture(s) - ).get() shouldBe Tuple2.of(n, s) - - CompletableFuture.completedFuture(n).allTupleOfFastFail( - CompletableFuture.completedFuture(s), - CompletableFuture.completedFuture(d) - ).get() shouldBe Tuple3.of(n, s, d) - - CompletableFuture.completedFuture(n).allTupleOfFastFail( - CompletableFuture.completedFuture(s), - CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(anotherN) - ).get() shouldBe Tuple4.of(n, s, d, anotherN) - - CompletableFuture.completedFuture(n).allTupleOfFastFail( - CompletableFuture.completedFuture(s), - CompletableFuture.completedFuture(d), - CompletableFuture.completedFuture(anotherN), - CompletableFuture.completedFuture(n + n) - ).get() shouldBe Tuple5.of(n, s, d, anotherN, n + n) - } - - test("mostTupleOfSuccess - CompletableFuture") { - val completed = CompletableFuture.completedFuture(n) - val anotherCompleted = CompletableFutureUtils.completedStage(s) - val failed = CompletableFutureUtils.failedFuture(rte) - val cancelled = createCancelledFuture() - val incomplete = createIncompleteFuture() - - completed.mostTupleOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherCompleted - ).await() shouldBe Tuple2.of(n, s) - completed.mostTupleOfSuccess( - 10, TimeUnit.MILLISECONDS, failed - ).await() shouldBe Tuple2.of(n, null) - - completed.mostTupleOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherCompleted, cancelled - ).await() shouldBe Tuple3.of(n, s, null) - incomplete.mostTupleOfSuccess( - 10, TimeUnit.MILLISECONDS, failed, anotherCompleted - ).await() shouldBe Tuple3.of(null, null, s) - - completed.mostTupleOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherCompleted, cancelled, incomplete - ).await() shouldBe Tuple4.of(n, s, null, null) - incomplete.mostTupleOfSuccess( - 10, TimeUnit.MILLISECONDS, failed, cancelled, incomplete - ).await() shouldBe Tuple4.of(null, null, null, null) - - cancelled.mostTupleOfSuccess( - 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, incomplete, failed - ).await() shouldBe Tuple5.of(null, n, s, null, null) - - // with `executorWhenTimeout` - - completed.mostTupleOfSuccess( - testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, anotherCompleted - ).await() shouldBe Tuple2.of(n, s) - completed.mostTupleOfSuccess( - testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, failed - ).await() shouldBe Tuple2.of(n, null) - - completed.mostTupleOfSuccess( - testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, anotherCompleted, cancelled - ).await() shouldBe Tuple3.of(n, s, null) - incomplete.mostTupleOfSuccess( - testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, failed, anotherCompleted - ).await() shouldBe Tuple3.of(null, null, s) - - completed.mostTupleOfSuccess( - testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, anotherCompleted, cancelled, incomplete - ).await() shouldBe Tuple4.of(n, s, null, null) - incomplete.mostTupleOfSuccess( - testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, failed, cancelled, incomplete - ).await() shouldBe Tuple4.of(null, null, null, null) - - cancelled.mostTupleOfSuccess( - testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, completed, anotherCompleted, incomplete, failed - ).await() shouldBe Tuple5.of(null, n, s, null, null) - } - //////////////////////////////////////////////////////////////////////////////// //# both methods ////////////////////////////////////////////////////////////////////////////////