From 8ab37d19b676f7e8ec43595767dd2e0ee623a714 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 20 Apr 2024 22:23:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20more=20covariance=20to=20methods;?= =?UTF-8?q?=20rename=20more=20methods=20=F0=9F=A7=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 44 ++-- .../src/main/java/io/foldright/cffu/Cffu.java | 10 +- .../java/io/foldright/cffu/CffuFactory.java | 241 +++++------------ .../cffu/CompletableFutureUtils.java | 59 ++--- .../cffu/NoCfsProvidedException.java | 4 +- .../io/foldright/cffu/CffuFactoryTest.java | 79 +++--- .../test/java/io/foldright/cffu/CffuTest.java | 8 +- .../cffu/CompletableFutureUtilsTest.java | 102 ++++---- .../foldright/demo/AllOfWithResultDemo.java | 12 +- .../demo/ConcurrencyStrategyDemo.java | 22 +- .../java/io/foldright/test_utils/TestUtils.kt | 4 +- .../foldright/cffu/kotlin/CffuExtensions.kt | 244 ++++-------------- .../kotlin/CompletableFutureExtensions.kt | 122 ++------- .../foldright/cffu/test/CffuExtensionsTest.kt | 40 +-- .../test/CompletableFutureExtensionsTest.kt | 22 +- demos/cffu-demo/scripts/run.sh | 2 + .../java/io/foldright/demo/cffu/CffuDemo.java | 2 +- .../demo/cffu/CompletableFutureUtilsDemo.java | 2 +- .../io/foldright/demo/cffu/CovariantDemo.java | 62 +++++ 19 files changed, 393 insertions(+), 688 deletions(-) create mode 100644 demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CovariantDemo.java diff --git a/README.md b/README.md index 093c7b9c..50e5e1c6 100644 --- a/README.md +++ b/README.md @@ -70,18 +70,18 @@ - ☘️ **补全业务使用中缺失的功能** - 更方便的功能,如 - - `cffuAllOf`/`allOfWithResult`方法:返回多个`CF`的结果,而不是无返回结果`Void`(`allOf`) - - `cffuCombine`/`combine`方法:返回多个`CF`不同类型的结果,而不是同一类型(`cffuAllOf`/`allOfWithResult`) + - `allResultsOf`方法:返回多个`CF`的结果,而不是无返回结果`Void`(`CompletableFuture#allOf()`) + - `cffuCombine`/`combine`方法:返回多个`CF`不同类型的结果,而不是同一类型(`allResultsOf`) - 更高效灵活的并发执行策略,如 - - `cffuAllOfFastFail`/`allOfFastFail`方法:有`CF`失败时快速返回,而不再等待所有`CF`运行完成(`allOf`) - - `cffuAnyOfSuccess`/`anyOfSuccess`方法:返回首个成功的`CF`结果,而不是首个完成(但可能失败)的`CF`(`anyOf`) + - `allOfFastFail`方法:有`CF`失败时快速返回,而不再等待所有`CF`运行完成(`allOf`) + - `anyOfSuccess`方法:返回首个成功的`CF`结果,而不是首个完成(但可能失败)的`CF`(`anyOf`) - 更安全的使用方式,如 - 支持设置缺省的业务线程池(`CffuFactoryBuilder#newCffuFactoryBuilder(executor)`方法) - - `cffuJoin(timeout, unit)`方法:支持超时的`join`的方法 + - `join(timeout, unit)`方法:支持超时的`join`的方法 - 支持禁止强制篡改(`CffuFactoryBuilder#forbidObtrudeMethods`方法) - 在类方法附加完善的代码质量注解(如`@NonNull`、`@Nullable`、`@CheckReturnValue`、`@Contract`等),在编码时`IDE`能尽早提示出问题 - 💪 **已有功能的增强**,如 - - `cffuAnyOf`/`anyOfWithType`方法:返回类型是`T`(类型安全),而不是返回`Object`(`anyOf`) + - `anyOf`方法:返回类型是`T`(类型安全),而不是返回`Object`(`CompletableFuture#anyOf()`) - ⏳ **`Backport`支持`Java 8`**,`Java 9+`高版本的所有`CF`新功能在`Java 8`等低`Java`版本直接可用,如 - 超时控制:`orTimeout`/`completeOnTimeout`方法 - 延迟执行:`delayedExecutor`方法 @@ -182,7 +182,7 @@ public class CffuDemo { final Cffu combined = longTaskA.thenCombine(longTaskB, Integer::sum) .orTimeout(1500, TimeUnit.MILLISECONDS); System.out.println("combined result: " + combined.get()); - final Cffu anyOfSuccess = cffuFactory.cffuAnyOfSuccess(longTaskC, longFailedTask); + final Cffu anyOfSuccess = cffuFactory.anyOfSuccess(longTaskC, longFailedTask); System.out.println("anyOfSuccess result: " + anyOfSuccess.get()); } } @@ -277,7 +277,7 @@ fun main() { `CompletableFuture`的`allOf`方法没有返回结果,只是返回`Void`,不方便获得所运行的多个`CF`结果。 \# 要再通过入参`CF`的`get`方法来获取结果。 -`cffu`的`cffuAllOf`/`allOfWithResult`方法提供了返回多个`CF`结果的功能。 +`cffu`的`allResultsOf`方法提供了返回多个`CF`结果的功能。 示例代码如下: @@ -288,7 +288,7 @@ public class AllOfWithResultDemo { public static void main(String[] args) { ////////////////////////////////////////////////// - // CffuFactory#cffuAllOf + // CffuFactory#allOf ////////////////////////////////////////////////// Cffu cffu1 = cffuFactory.completedFuture(21); Cffu cffu2 = cffuFactory.completedFuture(42); @@ -298,11 +298,11 @@ public class AllOfWithResultDemo { // the result can be got by input argument `cf1.get()`, but it's cumbersome. // so we can see a lot the util methods to enhance allOf with result in our project. - Cffu> allOfWithResult = cffuFactory.cffuAllOf(cffu1, cffu2); - System.out.println(allOfWithResult.get()); + Cffu> allResults = cffuFactory.allResultsOf(cffu1, cffu2); + System.out.println(allResults.get()); ////////////////////////////////////////////////// - // or CompletableFutureUtils#allOfWithResult + // or CompletableFutureUtils#allResultsOf ////////////////////////////////////////////////// CompletableFuture cf1 = CompletableFuture.completedFuture(21); CompletableFuture cf2 = CompletableFuture.completedFuture(42); @@ -310,8 +310,8 @@ public class AllOfWithResultDemo { CompletableFuture allOf = CompletableFuture.allOf(cf1, cf2); // Result type is Void!! - CompletableFuture> allOfWithResult2 = CompletableFutureUtils.allOfWithResult(cf1, cf2); - System.out.println(allOfWithResult2.get()); + CompletableFuture> allResults2 = CompletableFutureUtils.allOfWithResult(cf1, cf2); + System.out.println(allResults2.get()); } } ``` @@ -414,11 +414,11 @@ public class DefaultExecutorSettingForCffu { - `CompletableFuture`的`allOf`方法会等待所有输入`CF`运行完成;即使有`CF`失败了也要等待后续`CF`运行完成,再返回一个失败的`CF`。 - 对于业务逻辑来说,这样失败且继续等待策略,减慢了业务响应性;会希望如果有输入`CF`失败了,则快速失败不再做于事无补的等待 - - `cffu`提供了相应的`cffuAllOfFastFail`/`allOfFastFail`方法 + - `cffu`提供了相应的`allResultsOfFastFail`/`allOfFastFail`方法 - `allOf`/`allOfFastFail`两者都是,只有当所有的输入`CF`都成功时,才返回成功结果 - `CompletableFuture`的`anyOf`方法返回首个完成的`CF`(不会等待后续没有完成的`CF`,赛马模式);即使首个完成的`CF`是失败的,也会返回这个失败的`CF`结果。 - 对于业务逻辑来说,会希望赛马模式返回首个成功的`CF`结果,而不是首个完成但失败的`CF` - - `cffu`提供了相应的`cffuAnyOfSuccess`/`anyOfSuccess`方法 + - `cffu`提供了相应的`anyOfSuccess`方法 - `anyOfSuccess`只有当所有的输入`CF`都失败时,才返回失败结果 > 📔 关于多个`CF`的并发执行策略,可以看看`JavaScript`规范[`Promise Concurrency`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#promise_concurrency);在`JavaScript`中,`Promise`即对应`CompletableFuture`。 @@ -443,8 +443,8 @@ public class ConcurrencyStrategyDemo { public static void main(String[] args) throws Exception { //////////////////////////////////////////////////////////////////////// - // CffuFactory#cffuAllOfFastFail / allOfFastFail - // CffuFactory#cffuAnyOfSuccess / anyOfSuccess + // CffuFactory#allResultsOfFastFail / allOfFastFail + // CffuFactory#anyOfSuccess //////////////////////////////////////////////////////////////////////// final Cffu successAfterLongTime = cffuFactory.supplyAsync(() -> { sleep(3000); // sleep LONG time @@ -454,14 +454,14 @@ public class ConcurrencyStrategyDemo { // Result type is Void! Cffu cffuAll = cffuFactory.allOfFastFail(successAfterLongTime, failed); - Cffu> fastFailed = cffuFactory.cffuAllOfFastFail(successAfterLongTime, failed); + Cffu> fastFailed = cffuFactory.allResultsOfFastFail(successAfterLongTime, failed); // fast failed without waiting successAfterLongTime System.out.println(fastFailed.exceptionNow()); // Result type is Object! Cffu cffuAny = cffuFactory.anyOfSuccess(successAfterLongTime, failed); System.out.println(cffuAny.get()); - Cffu anyOfSuccess = cffuFactory.cffuAnyOfSuccess(successAfterLongTime, failed); + Cffu anyOfSuccess = cffuFactory.anyOfSuccess(successAfterLongTime, failed); System.out.println(anyOfSuccess.get()); //////////////////////////////////////////////////////////////////////// @@ -498,7 +498,7 @@ public class ConcurrencyStrategyDemo { - 主业务逻辑阻塞,没有机会做相应的处理,以及时响应用户 - 会费掉一个线程,线程是很有限的资源(一般几百个),耗尽线程意味着服务瘫痪故障 -`cffuJoin(timeout, unit)`方法即支持超时的`join`的方法;就像`cf.get(timeout, unit)` 之于 `cf.get()`。 +`join(timeout, unit)`方法即支持超时的`join`的方法;就像`cf.get(timeout, unit)` 之于 `cf.get()`。 这个新方法使用简单类似,不附代码示例。 @@ -519,7 +519,7 @@ public class ConcurrencyStrategyDemo { `CompletableFuture.anyOf`方法返回类型是`Object`,丢失具体类型,不够类型安全,使用时需要转型也不方便。 -`cffu`提供了`cffuAnyOf`/`anyOfWithType`方法,返回类型是`T`(类型安全),而不是返回`Object`(`anyOf`)。 +`cffu`提供了`anyOf`/`anyOf`方法,返回类型是`T`(类型安全),而不是返回`Object`(`CompletableFuture#anyOf()`)。 这个新方法使用简单类似,不附代码示例。 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 bc00c932..795adfd1 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java +++ b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java @@ -1261,7 +1261,7 @@ public Cffu handleAsync( * @throws ExecutionException if the computation threw an exception * @throws InterruptedException if the current thread was interrupted while waiting * @see #join() - * @see #cffuJoin(long, TimeUnit) + * @see #join(long, TimeUnit) * @see #getNow(Object) * @see #resultNow() * @see #get(long, TimeUnit) @@ -1286,7 +1286,7 @@ public T get() throws InterruptedException, ExecutionException { * @throws ExecutionException if the computation threw an exception * @throws InterruptedException if the current thread was interrupted while waiting * @throws TimeoutException if the wait timed out - * @see #cffuJoin(long, TimeUnit) + * @see #join(long, TimeUnit) * @see #getNow(Object) * @see #resultNow() * @see #join() @@ -1312,7 +1312,7 @@ public T get(long timeout, TimeUnit unit) throws InterruptedException, Execution * @throws CancellationException if the computation was cancelled * @throws CompletionException if this future completed exceptionally * or a completion computation threw an exception - * @see #cffuJoin(long, TimeUnit) + * @see #join(long, TimeUnit) * @see #getNow(Object) * @see #resultNow() * @see #get(long, TimeUnit) @@ -1363,7 +1363,7 @@ public T join() { */ @Blocking @Nullable - public T cffuJoin(long timeout, TimeUnit unit) { + public T join(long timeout, TimeUnit unit) { checkMinimalStage(); return CompletableFutureUtils.join(cf, timeout, unit); @@ -1379,7 +1379,7 @@ public T cffuJoin(long timeout, TimeUnit unit) { * @throws CompletionException if this future completed exceptionally * or a completion computation threw an exception * @see #resultNow() - * @see #cffuJoin(long, TimeUnit) + * @see #join(long, TimeUnit) * @see #join() * @see #get(long, TimeUnit) * @see #get() 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 aeaf3445..3d8ce21d 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -312,7 +312,7 @@ public final Cffu[] asCffuArray(CompletionStage... stages) { *

* if you need the results of given Cffus, prefer below methods: *

    - *
  1. {@link #cffuAllOf(Cffu[])} + *
  2. {@link #allResultsOf(Cffu[])} *
  3. {@link #cffuCombine(Cffu, Cffu)} / {@link #cffuCombine(Cffu, Cffu, Cffu, Cffu, Cffu)} * (provided overloaded methods with 2~5 input) *
@@ -324,7 +324,7 @@ public final Cffu[] asCffuArray(CompletionStage... stages) { * @param cfs the Cffus * @return a new Cffu that is completed when all the given Cffus complete * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAllOf(Cffu[]) + * @see #allResultsOf(Cffu[]) * @see #cffuCombine(Cffu, Cffu) * @see #cffuCombine(Cffu, Cffu, Cffu) * @see #cffuCombine(Cffu, Cffu, Cffu, Cffu) @@ -349,7 +349,7 @@ public Cffu allOf(Cffu... cfs) { *

* if you need the results of given CompletableFutures, prefer below methods: *

    - *
  1. {@link #cffuAllOf(CompletableFuture[])} + *
  2. {@link #allResultsOf(CompletableFuture[])} *
  3. {@link #cffuCombine(CompletableFuture, CompletableFuture)} / * {@link #cffuCombine(CompletableFuture, CompletableFuture, CompletableFuture, CompletableFuture, CompletableFuture)} * (provided overloaded methods with 2~5 input) @@ -359,7 +359,7 @@ public Cffu allOf(Cffu... cfs) { * @return a new Cffu that is completed when all the given CompletableFutures complete * @throws NullPointerException if the array or any of its elements are {@code null} * @see #allOf(Cffu[]) - * @see #cffuAllOf(CompletableFuture[]) + * @see #allResultsOf(CompletableFuture[]) * @see #cffuCombine(CompletableFuture, CompletableFuture) * @see #cffuCombine(CompletableFuture, CompletableFuture, CompletableFuture) * @see #cffuCombine(CompletableFuture, CompletableFuture, CompletableFuture, CompletableFuture) @@ -418,7 +418,7 @@ public Cffu allOfFastFail(Cffu... cfs) { * @return a new Cffu that is successful when all the given CompletableFutures success * @throws NullPointerException if the array or any of its elements are {@code null} * @see #allOfFastFail(Cffu[]) - * @see #cffuAllOfFastFail(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see CompletableFutureUtils#allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) @@ -443,41 +443,35 @@ public Cffu allOfFastFail() { * Otherwise, if it completed exceptionally, the returned Cffu also does so, * with a CompletionException holding this exception as its cause.
    * If no Cffus are provided, returns an incomplete Cffu. - *

    - * prefer {@link #cffuAnyOf(Cffu[])} method if the given Cffus have same result type, - * because {@link #cffuAnyOf(Cffu[])} return type {@code T} instead of type {@code Object}, more type safe. * * @param cfs the Cffus * @return a new Cffu that is completed with the result * or exception from any of the given Cffus when one completes * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAnyOf(Cffu[]) * @see CompletableFuture#anyOf(CompletableFuture[]) */ @Contract(pure = true) @SuppressWarnings("unchecked") - public Cffu anyOf(Cffu... cfs) { - return anyOf(toCompletableFutureArray((Cffu[]) cfs)); + @SafeVarargs + public final Cffu anyOf(Cffu... cfs) { + return anyOf(toCompletableFutureArray((Cffu[]) cfs)); } /** * Same as {@link #anyOf(Cffu[])} with overloaded argument type {@link CompletableFuture}. - *

    - * prefer {@link #cffuAnyOf(CompletableFuture[])} method if the given Cffus have same result type, - * because {@link #cffuAnyOf(CompletableFuture[])} return type {@code T} - * instead of type {@code Object}, more type safe. * * @param cfs the CompletableFutures * @return a new Cffu that is completed with the result * or exception from any of the given CompletableFutures when one completes * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAnyOf(CompletableFuture[]) * @see #anyOf(Cffu[]) * @see CompletableFuture#anyOf(CompletableFuture[]) */ @Contract(pure = true) - public Cffu anyOf(CompletableFuture... cfs) { - return new0(CompletableFuture.anyOf(cfs)); + @SuppressWarnings("unchecked") + @SafeVarargs + public final Cffu anyOf(CompletableFuture... cfs) { + return (Cffu) new0(CompletableFuture.anyOf(cfs)); } /** @@ -488,7 +482,7 @@ public Cffu anyOf(CompletableFuture... cfs) { * @see #anyOf(CompletableFuture[]) */ @Contract(pure = true) - public Cffu anyOf() { + public Cffu anyOf() { return newIncompleteCffu(); } @@ -503,11 +497,11 @@ public Cffu anyOf() { * @param cfs the Cffus * @return a new Cffu * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAnyOf(Cffu[]) */ @SuppressWarnings({"unchecked", "rawtypes"}) - public Cffu anyOfSuccess(Cffu... cfs) { - return anyOfSuccess(toCompletableFutureArray((Cffu[]) cfs)); + @SafeVarargs + public final Cffu anyOfSuccess(Cffu... cfs) { + return (Cffu) anyOfSuccess(toCompletableFutureArray((Cffu[]) cfs)); } /** @@ -521,21 +515,17 @@ public Cffu anyOfSuccess(Cffu... cfs) { * @param cfs the CompletableFutures * @return a new Cffu * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAnyOfSuccess(Cffu[]) - * @see #cffuAnyOf(Cffu[]) */ - public Cffu anyOfSuccess(CompletableFuture... cfs) { + @SafeVarargs + public final Cffu anyOfSuccess(CompletableFuture... cfs) { return new0(CompletableFutureUtils.anyOfSuccess(cfs)); } /** - * Provided this overloaded method just for resolving "cffuAnyOfSuccess is ambiguous" problem + * Provided this overloaded method just for resolving "anyOfSuccess is ambiguous" problem * when call {@code anyOfSuccess} with empty arguments: {@code cffuFactory.anyOfSuccess()}. - * - * @see #cffuAnyOfSuccess(Cffu[]) - * @see #cffuAnyOfSuccess(CompletableFuture[]) */ - public Cffu anyOfSuccess() { + public Cffu anyOfSuccess() { return new0(CompletableFutureUtils.anyOfSuccess()); } @@ -580,8 +570,7 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) { //# New type-safe allOf/anyOf Factory Methods // method name prefix with `cffu` // - // - cffuAllOf - // - cffuAnyOf + // - allResultsOf //////////////////////////////////////////////////////////////////////////////// /** @@ -603,8 +592,8 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) { @Contract(pure = true) @SafeVarargs @SuppressWarnings({"rawtypes", "unchecked"}) - public final Cffu> cffuAllOf(Cffu... cfs) { - return cffuAllOf(toCompletableFutureArray((Cffu[]) cfs)); + public final Cffu> allResultsOf(Cffu... cfs) { + return allResultsOf(toCompletableFutureArray((Cffu[]) cfs)); } /** @@ -614,29 +603,29 @@ public final Cffu> cffuAllOf(Cffu... cfs) { * also does so, with a CompletionException holding this exception as its cause. * If no CompletableFutures are provided, returns a Cffu completed with the value empty list. *

    - * Same as {@link #cffuAllOf(Cffu[])} with overloaded argument type {@link CompletableFuture}. + * Same as {@link #allResultsOf(Cffu[])} with overloaded argument type {@link CompletableFuture}. * * @param cfs the CompletableFutures * @return a new Cffu that is completed when all the given CompletableFutures complete * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAllOf(Cffu[]) + * @see #allResultsOf(Cffu[]) */ @Contract(pure = true) @SafeVarargs - public final Cffu> cffuAllOf(CompletableFuture... cfs) { - return new0(CompletableFutureUtils.allOfWithResult(cfs)); + public final Cffu> allResultsOf(CompletableFuture... cfs) { + return new0(CompletableFutureUtils.allResultsOf(cfs)); } /** - * Provided this overloaded method just for resolving "cffuAllOf is ambiguous" problem - * when call {@code cffuAllOf} with empty arguments: {@code cffuFactory.cffuAllOf()}. + * Provided this overloaded method just for resolving "allResultsOf is ambiguous" problem + * when call {@code allResultsOf} with empty arguments: {@code cffuFactory.allResultsOf()}. * - * @see #cffuAllOf(Cffu[]) - * @see #cffuAllOf(CompletableFuture[]) + * @see #allResultsOf(Cffu[]) + * @see #allResultsOf(CompletableFuture[]) */ @Contract(pure = true) - public Cffu> cffuAllOf() { - return new0(CompletableFutureUtils.allOfWithResult()); + public Cffu> allResultsOf() { + return new0(CompletableFutureUtils.allResultsOf()); } /** @@ -651,13 +640,13 @@ public Cffu> cffuAllOf() { * @return a new CompletableFuture that is successful when all the given CompletableFutures success * @throws NullPointerException if the array or any of its elements are {@code null} * @see CompletableFutureUtils#allOfFastFail(CompletableFuture[]) - * @see #cffuAllOfFastFail(Cffu[]) + * @see #allResultsOfFastFail(Cffu[]) */ @Contract(pure = true) @SafeVarargs @SuppressWarnings({"rawtypes", "unchecked"}) - public final Cffu> cffuAllOfFastFail(Cffu... cfs) { - return cffuAllOfFastFail(toCompletableFutureArray((Cffu[]) cfs)); + public final Cffu> allResultsOfFastFail(Cffu... cfs) { + return allResultsOfFastFail(toCompletableFutureArray((Cffu[]) cfs)); } /** @@ -668,128 +657,30 @@ public final Cffu> cffuAllOfFastFail(Cffu... cfs) { * with a CompletionException holding this exception as its cause. * If no CompletableFutures are provided, returns a Cffu completed with the value empty list. *

    - * Same as {@link #cffuAllOfFastFail(Cffu[])} with overloaded argument type {@link CompletableFuture}. + * Same as {@link #allResultsOfFastFail(Cffu[])} with overloaded argument type {@link CompletableFuture}. * * @param cfs the CompletableFutures * @return a new CompletableFuture that is successful when all the given CompletableFutures success * @throws NullPointerException if the array or any of its elements are {@code null} * @see CompletableFutureUtils#allOfFastFail(CompletableFuture[]) - * @see #cffuAllOfFastFail(Cffu[]) - */ - @Contract(pure = true) - @SafeVarargs - public final Cffu> cffuAllOfFastFail(CompletableFuture... cfs) { - return new0(CompletableFutureUtils.allOfFastFailWithResult(cfs)); - } - - /** - * Provided this overloaded method just for resolving "cffuAllOfFastFail is ambiguous" problem - * when call {@code cffuAllOfFastFail} with empty arguments: {@code cffuFactory.cffuAllOfFastFail()}. - * - * @see #cffuAllOfFastFail(Cffu[]) - * @see #cffuAllOfFastFail(CompletableFuture[]) - */ - @Contract(pure = true) - public Cffu> cffuAllOfFastFail() { - return new0(CompletableFutureUtils.allOfFastFailWithResult()); - } - - /** - * Returns a new Cffu that is completed when any of the given Cffus complete, with the same result. - *

    - * Same as {@link #anyOf(Cffu[])}, but return result type is specified type instead of type {@code Object}. - * - * @param cfs the Cffus - * @return a new Cffu that is completed with the result - * or exception from any of the given Cffus when one completes - * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #anyOf(Cffu[]) - */ - @Contract(pure = true) - @SafeVarargs - @SuppressWarnings({"unchecked", "rawtypes"}) - public final Cffu cffuAnyOf(Cffu... cfs) { - return cffuAnyOf(toCompletableFutureArray((Cffu[]) cfs)); - } - - /** - * Returns a new Cffu that is completed when any of the given CompletableFutures complete, with the same result. - *

    - * Same as {@link #cffuAllOf(Cffu[])} with overloaded argument type {@link CompletableFuture}. - * - * @param cfs the CompletableFutures - * @return a new Cffu that is completed with the result - * or exception from any of the given CompletableFutures when one completes - * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAnyOf(Cffu[]) + * @see #allResultsOfFastFail(Cffu[]) */ @Contract(pure = true) @SafeVarargs - public final Cffu cffuAnyOf(CompletableFuture... cfs) { - return new0(CompletableFutureUtils.anyOfWithType(cfs)); + public final Cffu> allResultsOfFastFail(CompletableFuture... cfs) { + return new0(CompletableFutureUtils.allResultsOfFastFail(cfs)); } /** - * Provided this overloaded method just for resolving "cffuAnyOf is ambiguous" problem - * when call {@code cffuAnyOf} with empty arguments: {@code cffuFactory.cffuAnyOf()}. + * Provided this overloaded method just for resolving "allResultsOfFastFail is ambiguous" problem + * when call {@code allResultsOfFastFail} with empty arguments: {@code cffuFactory.allResultsOfFastFail()}. * - * @see #cffuAnyOf(Cffu[]) - * @see #cffuAnyOf(CompletableFuture[]) + * @see #allResultsOfFastFail(Cffu[]) + * @see #allResultsOfFastFail(CompletableFuture[]) */ @Contract(pure = true) - public Cffu cffuAnyOf() { - return newIncompleteCffu(); - } - - /** - * Returns a new Cffu that is successful when any of the given Cffus success, - * with the same result. Otherwise, all the given Cffus complete exceptionally, - * the returned Cffu also does so, with a CompletionException holding - * an exception from any of the given Cffu as its cause. If no Cffu are provided, - * returns a new Cffu that is already completed exceptionally - * with a CompletionException holding a {@link NoCfsProvidedException} as its cause. - * - * @param cfs the Cffus - * @return a new Cffu - * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAnyOf(Cffu[]) - */ - @SafeVarargs - @SuppressWarnings({"rawtypes", "unchecked"}) - public final Cffu cffuAnyOfSuccess(Cffu... cfs) { - return cffuAnyOfSuccess(toCompletableFutureArray((Cffu[]) cfs)); - } - - /** - * Returns a new Cffu that is successful when any of the given CompletableFutures success, - * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, - * the returned Cffu also does so, with a CompletionException holding - * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new Cffu that is already completed exceptionally - * with a CompletionException holding a {@link NoCfsProvidedException} as its cause. - *

    - * Same as {@link #cffuAnyOfSuccess(Cffu[])} with overloaded argument type {@link CompletableFuture}. - * - * @param cfs the CompletableFutures - * @return a new Cffu - * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #cffuAnyOfSuccess(Cffu[]) - * @see #cffuAnyOf(Cffu[]) - */ - @SafeVarargs - public final Cffu cffuAnyOfSuccess(CompletableFuture... cfs) { - return new0(CompletableFutureUtils.anyOfSuccessWithType(cfs)); - } - - /** - * Provided this overloaded method just for resolving "cffuAnyOfSuccess is ambiguous" problem - * when call {@code cffuAnyOfSuccess} with empty arguments: {@code cffuFactory.cffuAnyOfSuccess()}. - * - * @see #cffuAnyOfSuccess(Cffu[]) - * @see #cffuAnyOfSuccess(CompletableFuture[]) - */ - public Cffu cffuAnyOfSuccess() { - return new0(CompletableFutureUtils.anyOfSuccessWithType()); + public Cffu> allResultsOfFastFail() { + return new0(CompletableFutureUtils.allResultsOfFastFail()); } //////////////////////////////////////////////////////////////////////////////// @@ -802,12 +693,12 @@ public Cffu cffuAnyOfSuccess() { * If any of the given Cffu complete exceptionally, then the returned * Cffu also does so, with a CompletionException holding this exception as its cause. *

    - * Same as {@link #cffuAllOf(Cffu[])} but with two inputs and return results as {@code Tuple2}. + * Same as {@link #allResultsOf(Cffu[])} but with two inputs and return results as {@code Tuple2}. * * @return a new Cffu that is completed when the given two Cffus complete * @throws NullPointerException if any input Cffus are {@code null} * @see Cffu#cffuCombine(Cffu) - * @see #cffuAllOf(Cffu[]) + * @see #allResultsOf(Cffu[]) * @see #allOf(Cffu[]) */ @Contract(pure = true) @@ -826,7 +717,7 @@ public Cffu> cffuCombine(Cffu cf1, Cffu cf2) { * @throws NullPointerException if any of the given CompletableFutures are {@code null} * @see Cffu#cffuCombine(CompletableFuture) * @see #cffuCombine(Cffu, Cffu) - * @see #cffuAllOf(CompletableFuture[]) + * @see #allResultsOf(CompletableFuture[]) * @see #allOf(CompletableFuture[]) */ @Contract(pure = true) @@ -843,7 +734,7 @@ public Cffu> cffuCombine(CompletableFuture cf1, Comp * @return a new Cffu that is successful when the given two Cffus success * @throws NullPointerException if any of the given Cffus are {@code null} * @see Cffu#cffuCombineFastFail(Cffu) - * @see #cffuAllOfFastFail(Cffu[]) + * @see #allResultsOfFastFail(Cffu[]) * @see #allOfFastFail(Cffu[]) */ @Contract(pure = true) @@ -861,7 +752,7 @@ public Cffu> cffuCombineFastFail(Cffu cf1, Cffu * @throws NullPointerException if any of the given CompletableFutures are {@code null} * @see Cffu#cffuCombineFastFail(CompletableFuture) * @see #cffuCombineFastFail(Cffu, Cffu) - * @see #cffuAllOfFastFail(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see #allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) @@ -874,12 +765,12 @@ public Cffu> cffuCombineFastFail(CompletableFuture c * If any of the given Cffu complete exceptionally, then the returned * Cffu also does so, with a CompletionException holding this exception as its cause. *

    - * Same as {@link #cffuAllOf(Cffu[])} but with three inputs and return results as {@code Tuple3}. + * Same as {@link #allResultsOf(Cffu[])} but with three inputs and return results as {@code Tuple3}. * * @return a new Cffu that is completed when the given three Cffus complete * @throws NullPointerException if any input Cffus are {@code null} * @see Cffu#cffuCombine(Cffu, Cffu) - * @see #cffuAllOf(Cffu[]) + * @see #allResultsOf(Cffu[]) * @see #allOf(Cffu[]) */ @Contract(pure = true) @@ -898,7 +789,7 @@ public Cffu> cffuCombine(Cffu cf1, Cffu * @throws NullPointerException if any of the given CompletableFutures are {@code null} * @see Cffu#cffuCombine(CompletableFuture, CompletableFuture) * @see #cffuCombine(Cffu, Cffu, Cffu) - * @see #cffuAllOf(CompletableFuture[]) + * @see #allResultsOf(CompletableFuture[]) * @see #allOf(CompletableFuture[]) */ @Contract(pure = true) @@ -916,7 +807,7 @@ public Cffu> cffuCombine( * @return a new Cffu that is successful when the given three Cffus success * @throws NullPointerException if any of the given Cffus are {@code null} * @see Cffu#cffuCombineFastFail(Cffu, Cffu) - * @see #cffuAllOfFastFail(Cffu[]) + * @see #allResultsOfFastFail(Cffu[]) * @see #allOfFastFail(Cffu[]) */ @Contract(pure = true) @@ -934,7 +825,7 @@ public Cffu> cffuCombineFastFail(Cffu cf1, C * @throws NullPointerException if any of the given CompletableFutures are {@code null} * @see Cffu#cffuCombineFastFail(CompletableFuture, CompletableFuture) * @see #cffuCombineFastFail(Cffu, Cffu) - * @see #cffuAllOfFastFail(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see #allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) @@ -948,12 +839,12 @@ public Cffu> cffuCombineFastFail( * If any of the given Cffu complete exceptionally, then the returned * Cffu also does so, with a CompletionException holding this exception as its cause. *

    - * Same as {@link #cffuAllOf(Cffu[])} but with 4 inputs and return results as {@code Tuple4}. + * Same as {@link #allResultsOf(Cffu[])} but with 4 inputs and return results as {@code Tuple4}. * * @return a new Cffu that is completed when the given 4 Cffus complete * @throws NullPointerException if any input Cffus are {@code null} * @see Cffu#cffuCombine(Cffu, Cffu, Cffu) - * @see #cffuAllOf(Cffu[]) + * @see #allResultsOf(Cffu[]) * @see #allOf(Cffu[]) */ @Contract(pure = true) @@ -974,7 +865,7 @@ public Cffu> cffuCombine( * @throws NullPointerException if any of the given CompletableFutures are {@code null} * @see Cffu#cffuCombine(CompletableFuture, CompletableFuture, CompletableFuture) * @see #cffuCombine(Cffu, Cffu, Cffu, Cffu) - * @see #cffuAllOf(CompletableFuture[]) + * @see #allResultsOf(CompletableFuture[]) * @see #allOf(CompletableFuture[]) */ @Contract(pure = true) @@ -994,7 +885,7 @@ public Cffu> cffuCombine( * @return a new Cffu that is successful when the given four Cffus success * @throws NullPointerException if any of the given Cffus are {@code null} * @see Cffu#cffuCombineFastFail(Cffu, Cffu, Cffu) - * @see #cffuAllOfFastFail(Cffu[]) + * @see #allResultsOfFastFail(Cffu[]) * @see #allOfFastFail(Cffu[]) */ @Contract(pure = true) @@ -1014,7 +905,7 @@ public Cffu> cffuCombineFastFail( * @throws NullPointerException if any of the given CompletableFutures are {@code null} * @see Cffu#cffuCombineFastFail(CompletableFuture, CompletableFuture, CompletableFuture) * @see #cffuCombineFastFail(Cffu, Cffu) - * @see #cffuAllOfFastFail(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see #allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) @@ -1029,12 +920,12 @@ public Cffu> cffuCombineFastFail( * If any of the given Cffu complete exceptionally, then the returned * Cffu also does so, with a CompletionException holding this exception as its cause. *

    - * Same as {@link #cffuAllOf(Cffu[])} but with 5 inputs and return results as {@code Tuple5}. + * Same as {@link #allResultsOf(Cffu[])} but with 5 inputs and return results as {@code Tuple5}. * * @return a new Cffu that is completed when the given 5 Cffus complete * @throws NullPointerException if any input Cffus are {@code null} * @see Cffu#cffuCombine(Cffu, Cffu, Cffu, Cffu) - * @see #cffuAllOf(Cffu[]) + * @see #allResultsOf(Cffu[]) * @see #allOf(Cffu[]) */ @Contract(pure = true) @@ -1056,7 +947,7 @@ public Cffu> cffuCombine( * @throws NullPointerException if any of the given CompletableFutures are {@code null} * @see Cffu#cffuCombine(CompletableFuture, CompletableFuture, CompletableFuture, CompletableFuture) * @see #cffuCombine(Cffu, Cffu, Cffu, Cffu, Cffu) - * @see #cffuAllOf(CompletableFuture[]) + * @see #allResultsOf(CompletableFuture[]) * @see #allOf(CompletableFuture[]) */ @Contract(pure = true) @@ -1075,7 +966,7 @@ public Cffu> cffuCombine( * @return a new Cffu that is successful when the given five Cffus success * @throws NullPointerException if any of the given Cffus are {@code null} * @see Cffu#cffuCombineFastFail(Cffu, Cffu, Cffu, Cffu) - * @see #cffuAllOfFastFail(Cffu[]) + * @see #allResultsOfFastFail(Cffu[]) * @see #allOfFastFail(Cffu[]) */ @Contract(pure = true) @@ -1095,7 +986,7 @@ public Cffu> cffuCombineFastFail * @throws NullPointerException if any of the given CompletableFutures are {@code null} * @see Cffu#cffuCombineFastFail(CompletableFuture, CompletableFuture, CompletableFuture, CompletableFuture) * @see #cffuCombineFastFail(Cffu, Cffu) - * @see #cffuAllOfFastFail(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see #allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) 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 84abfad2..e93701bf 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -50,7 +50,7 @@ public final class CompletableFutureUtils { @Contract(pure = true) @SafeVarargs @SuppressWarnings("unchecked") - public static CompletableFuture> allOfWithResult(CompletableFuture... cfs) { + public static CompletableFuture> allResultsOf(CompletableFuture... cfs) { requireCfsAndEleNonNull(cfs); final int size = cfs.length; if (size == 0) return CompletableFuture.completedFuture(arrayList()); @@ -121,7 +121,7 @@ public static CompletableFuture allOfFastFail(CompletableFuture... cfs) @Contract(pure = true) @SafeVarargs @SuppressWarnings({"unchecked", "rawtypes"}) - public static CompletableFuture> allOfFastFailWithResult(CompletableFuture... cfs) { + public static CompletableFuture> allResultsOfFastFail(CompletableFuture... cfs) { requireCfsAndEleNonNull(cfs); final int size = cfs.length; if (size == 0) return CompletableFuture.completedFuture(arrayList()); @@ -134,7 +134,7 @@ public static CompletableFuture> allOfFastFailWithResult(Completable // NOTE: fill the ONE MORE element of failedOrBeIncomplete HERE: // a cf that is successful when all given cfs success, otherwise be incomplete - failedOrBeIncomplete[size] = allOfWithResult(successOrBeIncomplete); + failedOrBeIncomplete[size] = allResultsOf(successOrBeIncomplete); return (CompletableFuture) CompletableFuture.anyOf(failedOrBeIncomplete); } @@ -192,13 +192,13 @@ private static void fill(CompletableFuture[] cfs, * @return a new CompletableFuture that is completed with the result * or exception from any of the given CompletableFutures when one completes * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #anyOfSuccessWithType(CompletableFuture[]) + * @see #anyOfSuccess(CompletableFuture[]) * @see CompletableFuture#anyOf(CompletableFuture[]) */ @Contract(pure = true) @SafeVarargs @SuppressWarnings("unchecked") - public static CompletableFuture anyOfWithType(CompletableFuture... cfs) { + public static CompletableFuture anyOf(CompletableFuture... cfs) { return (CompletableFuture) CompletableFuture.anyOf(cfs); } @@ -214,15 +214,16 @@ public static CompletableFuture anyOfWithType(CompletableFuture anyOfSuccess(CompletableFuture... cfs) { + public static CompletableFuture anyOfSuccess(CompletableFuture... cfs) { requireCfsAndEleNonNull(cfs); final int size = cfs.length; if (size == 0) return failedFuture(new NoCfsProvidedException()); - if (size == 1) return (CompletableFuture) copy(cfs[0]); + if (size == 1) return (CompletableFuture) copy(cfs[0]); // NOTE: fill ONE MORE element of successOrBeIncompleteCfs LATER final CompletableFuture[] successOrBeIncomplete = new CompletableFuture[size + 1]; @@ -233,31 +234,7 @@ public static CompletableFuture anyOfSuccess(CompletableFuture... cfs // a cf that is failed when all given cfs fail, otherwise be incomplete successOrBeIncomplete[size] = CompletableFuture.allOf(failedOrBeIncomplete); - return CompletableFuture.anyOf(successOrBeIncomplete); - } - - /** - * Returns a new CompletableFuture that is successful when any of the given CompletableFutures success, - * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, - * the returned CompletableFuture also does so, with a CompletionException holding - * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new CompletableFuture that is already completed exceptionally - * with a CompletionException holding a {@link NoCfsProvidedException} as its cause. - *

    - * Same as {@link #anyOfSuccess(CompletableFuture[])}, - * but return result type is specified type instead of {@code Object}. - * - * @param cfs the CompletableFutures - * @return a new CompletableFuture that is successful - * when any of the given CompletableFutures success, with the same result - * @throws NullPointerException if the array or any of its elements are {@code null} - * @see #anyOfWithType(CompletableFuture[]) - */ - @Contract(pure = true) - @SafeVarargs - @SuppressWarnings("unchecked") - public static CompletableFuture anyOfSuccessWithType(CompletableFuture... cfs) { - return (CompletableFuture) anyOfSuccess(cfs); + return (CompletableFuture) CompletableFuture.anyOf(successOrBeIncomplete); } //////////////////////////////////////////////////////////////////////////////// @@ -271,7 +248,7 @@ public static CompletableFuture anyOfSuccessWithType(CompletableFuture CompletableFuture> combine( * * @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 #allOfFastFailWithResult(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see #allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) @@ -322,7 +299,7 @@ public static CompletableFuture> combineFastFail( * * @return a new CompletableFuture that is completed when the given 3 CompletableFutures complete * @throws NullPointerException if any of the given CompletableFutures are {@code null} - * @see #allOfWithResult(CompletableFuture[]) + * @see #allResultsOf(CompletableFuture[]) * @see CompletableFuture#allOf(CompletableFuture[]) */ @Contract(pure = true) @@ -349,7 +326,7 @@ public static CompletableFuture> combine( * * @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 #allOfFastFailWithResult(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see #allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) @@ -375,7 +352,7 @@ public static CompletableFuture> combineFastFail * * @return a new CompletableFuture that is completed when the given 4 CompletableFutures complete * @throws NullPointerException if any of the given CompletableFutures are {@code null} - * @see #allOfWithResult(CompletableFuture[]) + * @see #allResultsOf(CompletableFuture[]) * @see CompletableFuture#allOf(CompletableFuture[]) */ @Contract(pure = true) @@ -404,7 +381,7 @@ public static CompletableFuture> combine * * @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 #allOfFastFailWithResult(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see #allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) @@ -432,7 +409,7 @@ public static CompletableFuture> combine * * @return a new CompletableFuture that is completed when the given 5 CompletableFutures complete * @throws NullPointerException if any of the given CompletableFutures are {@code null} - * @see #allOfWithResult(CompletableFuture[]) + * @see #allResultsOf(CompletableFuture[]) * @see CompletableFuture#allOf(CompletableFuture[]) */ @Contract(pure = true) @@ -462,7 +439,7 @@ public static CompletableFuture> * * @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 #allOfFastFailWithResult(CompletableFuture[]) + * @see #allResultsOfFastFail(CompletableFuture[]) * @see #allOfFastFail(CompletableFuture[]) */ @Contract(pure = true) diff --git a/cffu-core/src/main/java/io/foldright/cffu/NoCfsProvidedException.java b/cffu-core/src/main/java/io/foldright/cffu/NoCfsProvidedException.java index 8160f752..4f0ec6de 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/NoCfsProvidedException.java +++ b/cffu-core/src/main/java/io/foldright/cffu/NoCfsProvidedException.java @@ -7,11 +7,9 @@ * Exception indicates that NO cfs({@link Cffu} / {@link CompletableFuture}) are provided * for methods require cf arguments. * - * @see CffuFactory#cffuAnyOfSuccess(Cffu[]) - * @see CffuFactory#cffuAnyOfSuccess(CompletableFuture[]) * @see CffuFactory#anyOfSuccess(Cffu[]) * @see CffuFactory#anyOfSuccess(CompletableFuture[]) - * @see CompletableFutureUtils#anyOfSuccessWithType(CompletableFuture[]) + * @see CompletableFutureUtils#anyOfSuccess(CompletableFuture[]) * @see CompletableFutureUtils#anyOfSuccess(CompletableFuture[]) */ @SuppressWarnings("serial") 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 75d35b63..2664361a 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -223,55 +223,54 @@ void test_anyOf_CompletableFuture() throws Exception { //# New type-safe allOf/anyOf Factory Methods // method name prefix with `cffu` // - // - cffuAllOf - // - cffuAnyOf + // - allResultsOf //////////////////////////////////////////////////////////////////////////////// @Test - void test_cffuAllOf() throws Exception { + void test_allResultsOf() throws Exception { assertEquals(Arrays.asList(n, n + 1), - cffuFactory.cffuAllOf(completedFuture(n), completedFuture(n + 1)).get() + cffuFactory.allResultsOf(completedFuture(n), completedFuture(n + 1)).get() ); assertEquals(Collections.singletonList(n), - cffuFactory.cffuAllOf(completedFuture(n)).get() + cffuFactory.allResultsOf(completedFuture(n)).get() ); assertEquals(Collections.emptyList(), - cffuFactory.cffuAllOf().get() + cffuFactory.allResultsOf().get() ); assertEquals(Arrays.asList(n, n + 1), - cffuFactory.cffuAllOf(cffuFactory.completedFuture(n), cffuFactory.completedFuture(n + 1)).get() + cffuFactory.allResultsOf(cffuFactory.completedFuture(n), cffuFactory.completedFuture(n + 1)).get() ); assertEquals(Collections.singletonList(n), - cffuFactory.cffuAllOf(cffuFactory.completedFuture(n)).get() + cffuFactory.allResultsOf(cffuFactory.completedFuture(n)).get() ); //////////////////////////////////////// assertEquals(Arrays.asList(n, n + 1), - cffuFactory.cffuAllOfFastFail(completedFuture(n), completedFuture(n + 1)).get() + cffuFactory.allResultsOfFastFail(completedFuture(n), completedFuture(n + 1)).get() ); assertEquals(Collections.singletonList(n), - cffuFactory.cffuAllOfFastFail(completedFuture(n)).get() + cffuFactory.allResultsOfFastFail(completedFuture(n)).get() ); assertEquals(Collections.emptyList(), - cffuFactory.cffuAllOfFastFail().get() + cffuFactory.allResultsOfFastFail().get() ); assertEquals(Arrays.asList(n, n + 1), - cffuFactory.cffuAllOfFastFail(cffuFactory.completedFuture(n), cffuFactory.completedFuture(n + 1)).get() + cffuFactory.allResultsOfFastFail(cffuFactory.completedFuture(n), cffuFactory.completedFuture(n + 1)).get() ); assertEquals(Collections.singletonList(n), - cffuFactory.cffuAllOfFastFail(cffuFactory.completedFuture(n)).get() + cffuFactory.allResultsOfFastFail(cffuFactory.completedFuture(n)).get() ); } @Test - void test_cffuAllOf_exceptionally() throws Exception { + void test_allResultsOf_exceptionally() throws Exception { try { - cffuFactory.cffuAllOf( + cffuFactory.allResultsOf( cffuFactory.completedFuture(n), cffuFactory.failedFuture(rte), cffuFactory.completedFuture(s) @@ -283,7 +282,7 @@ void test_cffuAllOf_exceptionally() throws Exception { } try { - cffuFactory.cffuAllOfFastFail( + cffuFactory.allResultsOfFastFail( cffuFactory.completedFuture(n), cffuFactory.failedFuture(rte), cffuFactory.completedFuture(s) @@ -296,56 +295,56 @@ void test_cffuAllOf_exceptionally() throws Exception { } @Test - void test_cffuAnyOf() throws Exception { - assertEquals(n, cffuFactory.cffuAnyOf( + void test_anyOf() throws Exception { + assertEquals(n, cffuFactory.anyOf( createIncompleteFuture(), completedFuture(n) ).get()); - assertEquals(n, cffuFactory.cffuAnyOf( + assertEquals(n, cffuFactory.anyOf( completedFuture(n) ).get()); - assertFalse(cffuFactory.cffuAnyOf().isDone()); + assertFalse(cffuFactory.anyOf().isDone()); - assertEquals(n, cffuFactory.cffuAnyOf( + assertEquals(n, cffuFactory.anyOf( cffuFactory.completedFuture(n), cffuFactory.newIncompleteCffu() ).get()); - assertEquals(n, cffuFactory.cffuAnyOf( + assertEquals(n, cffuFactory.anyOf( cffuFactory.completedFuture(n) ).get()); //////////////////////////////////////// - assertEquals(n, cffuFactory.cffuAnyOfSuccess(createIncompleteFuture(), completedFuture(n)).get()); - assertEquals(n, cffuFactory.cffuAnyOfSuccess(completedFuture(n)).get()); + assertEquals(n, cffuFactory.anyOfSuccess(createIncompleteFuture(), completedFuture(n)).get()); + assertEquals(n, cffuFactory.anyOfSuccess(completedFuture(n)).get()); - assertSame(NoCfsProvidedException.class, cffuFactory.cffuAnyOfSuccess().exceptionNow().getClass()); + assertSame(NoCfsProvidedException.class, cffuFactory.anyOfSuccess().exceptionNow().getClass()); - assertEquals(n, cffuFactory.cffuAnyOfSuccess( + assertEquals(n, cffuFactory.anyOfSuccess( cffuFactory.completedFuture(n), cffuFactory.newIncompleteCffu() ).get()); - assertEquals(n, cffuFactory.cffuAnyOfSuccess(cffuFactory.completedFuture(n)).get()); + assertEquals(n, cffuFactory.anyOfSuccess(cffuFactory.completedFuture(n)).get()); } @Test - void test_cffuAnyOf_exceptionally() throws Exception { - // first exceptionally completed cffuAnyOf cf win, + void test_anyOf_exceptionally() throws Exception { + // first exceptionally completed anyOf cf win, // even later cfs normally completed! try { - cffuFactory.cffuAnyOf(createIncompleteFuture(), failedFuture(rte), createIncompleteFuture()).get(); + cffuFactory.anyOf(createIncompleteFuture(), failedFuture(rte), createIncompleteFuture()).get(); fail(); } catch (ExecutionException expected) { assertSame(rte, expected.getCause()); } - // first normally completed cffuAnyOf cf win, + // first normally completed anyOf cf win, // even later cfs exceptionally completed! - assertEquals(n, cffuFactory.cffuAnyOf( + assertEquals(n, cffuFactory.anyOf( createIncompleteFuture(), completedFuture(n), createIncompleteFuture() @@ -353,9 +352,9 @@ void test_cffuAnyOf_exceptionally() throws Exception { } @Test - void test_cffuAnyOfSuccess__trivial_case() throws Exception { + void test_anyOfSuccess__trivial_case() throws Exception { // success then success - assertEquals(n, cffuFactory.cffuAnyOfSuccess( + assertEquals(n, cffuFactory.anyOfSuccess( cffuFactory.newIncompleteCffu(), cffuFactory.newIncompleteCffu(), cffuFactory.supplyAsync(() -> { @@ -365,7 +364,7 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { cffuFactory.completedFuture(n) ).get()); // success then failed - assertEquals(n, cffuFactory.cffuAnyOfSuccess( + assertEquals(n, cffuFactory.anyOfSuccess( cffuFactory.newIncompleteCffu(), cffuFactory.newIncompleteCffu(), cffuFactory.supplyAsync(() -> { @@ -376,7 +375,7 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { ).get()); // all success - assertEquals(n, cffuFactory.cffuAnyOfSuccess( + assertEquals(n, cffuFactory.anyOfSuccess( cffuFactory.supplyAsync(() -> { sleep(300); return anotherN; @@ -391,7 +390,7 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { ////////////////////////////////////////////////////////////////////////////// // success then success - assertEquals(n, cffuFactory.cffuAnyOfSuccess( + assertEquals(n, cffuFactory.anyOfSuccess( createIncompleteFuture(), createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { @@ -401,7 +400,7 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { completedFuture(n) ).get()); // success then failed - assertEquals(n, cffuFactory.cffuAnyOfSuccess( + assertEquals(n, cffuFactory.anyOfSuccess( createIncompleteFuture(), createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { @@ -412,7 +411,7 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { ).get()); // all success - assertEquals(n, cffuFactory.cffuAnyOfSuccess( + assertEquals(n, cffuFactory.anyOfSuccess( CompletableFuture.supplyAsync(() -> { sleep(300); return anotherN; @@ -426,7 +425,7 @@ void test_cffuAnyOfSuccess__trivial_case() throws Exception { ////////////////////////////////////////////////////////////////////////////// - assertSame(NoCfsProvidedException.class, cffuFactory.cffuAnyOfSuccess().exceptionNow().getClass()); + assertSame(NoCfsProvidedException.class, cffuFactory.anyOfSuccess().exceptionNow().getClass()); } //////////////////////////////////////////////////////////////////////////////// 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 26514942..3ebdf56a 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuTest.java @@ -51,12 +51,12 @@ class CffuTest { @Test void test_cffuJoin() { // Completed Future - assertEquals(n, cffuFactory.completedFuture(n).cffuJoin(1, TimeUnit.MILLISECONDS)); + assertEquals(n, cffuFactory.completedFuture(n).join(1, TimeUnit.MILLISECONDS)); // Incomplete Future -> CompletionException with TimeoutException Cffu incomplete = cffuFactory.newIncompleteCffu(); try { - incomplete.cffuJoin(1, TimeUnit.MILLISECONDS); + incomplete.join(1, TimeUnit.MILLISECONDS); fail(); } catch (CompletionException expected) { assertEquals(TimeoutException.class, expected.getCause().getClass()); @@ -65,7 +65,7 @@ void test_cffuJoin() { // Failed Future -> CompletionException Cffu failed = cffuFactory.failedFuture(rte); try { - failed.cffuJoin(1, TimeUnit.MILLISECONDS); + failed.join(1, TimeUnit.MILLISECONDS); fail(); } catch (CompletionException expected) { assertSame(rte, expected.getCause()); @@ -77,7 +77,7 @@ void test_cffuJoin() { sleep(300); return 42; }); - assertEquals(42, cffu.cffuJoin(3, TimeUnit.SECONDS)); + assertEquals(42, cffu.join(3, TimeUnit.SECONDS)); } @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 60b532f9..f369c270 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -25,35 +25,35 @@ class CompletableFutureUtilsTest { @Test void test_allOf__success__trivial_case() throws Exception { - assertEquals(Arrays.asList(n, n + 1, n + 2), allOfWithResult( + assertEquals(Arrays.asList(n, n + 1, n + 2), allResultsOf( completedFuture(n), completedFuture(n + 1), completedFuture(n + 2) ).get()); - assertEquals(Arrays.asList(n, n + 1), allOfWithResult(completedFuture(n), completedFuture(n + 1) + assertEquals(Arrays.asList(n, n + 1), allResultsOf(completedFuture(n), completedFuture(n + 1) ).get()); - assertEquals(Collections.singletonList(n), allOfWithResult(completedFuture(n)).get()); + assertEquals(Collections.singletonList(n), allResultsOf(completedFuture(n)).get()); - assertEquals(Collections.emptyList(), allOfWithResult().get()); + assertEquals(Collections.emptyList(), allResultsOf().get()); //////////////////////////////////////////////////////////////////////////////// - assertEquals(Arrays.asList(n, n + 1, n + 2), allOfFastFailWithResult( + assertEquals(Arrays.asList(n, n + 1, n + 2), allResultsOfFastFail( completedFuture(n), completedFuture(n + 1), completedFuture(n + 2) ).get()); - assertEquals(Arrays.asList(n, n + 1), allOfFastFailWithResult( + assertEquals(Arrays.asList(n, n + 1), allResultsOfFastFail( completedFuture(n), completedFuture(n + 1) ).get()); - assertEquals(Collections.singletonList(n), allOfFastFailWithResult(completedFuture(n)).get()); + assertEquals(Collections.singletonList(n), allResultsOfFastFail(completedFuture(n)).get()); - assertEquals(Collections.emptyList(), allOfFastFailWithResult().get()); + assertEquals(Collections.emptyList(), allResultsOfFastFail().get()); //////////////////////////////////////////////////////////////////////////////// @@ -72,7 +72,7 @@ void test_allOf__exceptionally() throws Exception { try { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); - allOfWithResult( + allResultsOf( failedFuture(rte), failedFuture(anotherRte), failedFuture(ex1), @@ -81,7 +81,7 @@ void test_allOf__exceptionally() throws Exception { fail(); } catch (ExecutionException expected) { - // anyOfSuccessWithType: the ex of first given cf argument win + // anyOfSuccess: the ex of first given cf argument win // ❗dependent on the implementation behavior of `CF.allOf`️ assertSame(rte, expected.getCause()); } @@ -90,7 +90,7 @@ void test_allOf__exceptionally() throws Exception { try { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); - allOfWithResult( + allResultsOf( CompletableFuture.supplyAsync(() -> { sleep(100); throw rte; @@ -102,14 +102,14 @@ void test_allOf__exceptionally() throws Exception { fail(); } catch (ExecutionException expected) { - // anyOfSuccessWithType: the ex of first given cf argument win + // anyOfSuccess: the ex of first given cf argument win // ❗dependent on the implementation behavior of `CF.allOf`️ assertSame(rte, expected.getCause()); } // success and failed try { - allOfWithResult( + allResultsOf( completedFuture(n), failedFuture(rte), completedFuture(s), @@ -123,7 +123,7 @@ void test_allOf__exceptionally() throws Exception { // failed/incomplete/failed try { - allOfWithResult( + allResultsOf( completedFuture(n), failedFuture(rte), createIncompleteFuture() @@ -136,7 +136,7 @@ void test_allOf__exceptionally() throws Exception { // incomplete fail incomplete try { - allOfWithResult( + allResultsOf( createIncompleteFuture(), failedFuture(rte), createIncompleteFuture() @@ -153,7 +153,7 @@ void test_allOf__exceptionally() throws Exception { try { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); - allOfFastFailWithResult( + allResultsOfFastFail( failedFuture(rte), failedFuture(anotherRte), failedFuture(ex1), @@ -162,7 +162,7 @@ void test_allOf__exceptionally() throws Exception { fail(); } catch (ExecutionException expected) { - // anyOfSuccessWithType: the ex of first complete(in time) cf argument win + // anyOfSuccess: the ex of first complete(in time) cf argument win // ❗dependent on the implementation behavior of `CF.anyOf`️ assertSame(rte, expected.getCause()); } @@ -171,7 +171,7 @@ void test_allOf__exceptionally() throws Exception { try { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); - allOfFastFailWithResult( + allResultsOfFastFail( CompletableFuture.supplyAsync(() -> { sleep(100); throw rte; @@ -183,14 +183,14 @@ void test_allOf__exceptionally() throws Exception { fail(); } catch (ExecutionException expected) { - // anyOfSuccessWithType: the ex of first complete(in time) cf argument win + // anyOfSuccess: the ex of first complete(in time) cf argument win // ❗dependent on the implementation behavior of `CF.anyOf`️ assertSame(anotherRte, expected.getCause()); } // success and failed try { - allOfFastFailWithResult( + allResultsOfFastFail( completedFuture(n), failedFuture(rte), completedFuture(s), @@ -204,7 +204,7 @@ void test_allOf__exceptionally() throws Exception { // failed/incomplete/failed try { - allOfFastFailWithResult( + allResultsOfFastFail( completedFuture(n), failedFuture(rte), createIncompleteFuture() @@ -217,7 +217,7 @@ void test_allOf__exceptionally() throws Exception { // incomplete fail incomplete try { - allOfFastFailWithResult( + allResultsOfFastFail( createIncompleteFuture(), failedFuture(rte), createIncompleteFuture() @@ -234,7 +234,7 @@ void test_allOf__exceptionally() throws Exception { try { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); - allOfFastFailWithResult( + allResultsOfFastFail( failedFuture(rte), failedFuture(anotherRte), failedFuture(ex1), @@ -243,7 +243,7 @@ void test_allOf__exceptionally() throws Exception { fail(); } catch (ExecutionException expected) { - // anyOfSuccessWithType: the ex of first complete(in time) cf argument win + // anyOfSuccess: the ex of first complete(in time) cf argument win // ❗dependent on the implementation behavior of `CF.anyOf`️ assertSame(rte, expected.getCause()); } @@ -264,7 +264,7 @@ void test_allOf__exceptionally() throws Exception { fail(); } catch (ExecutionException expected) { - // anyOfSuccessWithType: the ex of first complete(in time) cf argument win + // anyOfSuccess: the ex of first complete(in time) cf argument win // ❗dependent on the implementation behavior of `CF.anyOf`️ assertSame(anotherRte, expected.getCause()); } @@ -316,23 +316,23 @@ void test_allOf__exceptionally() throws Exception { @Test void test_anyOf__success__trivial_case() throws Exception { - assertEquals(n, anyOfWithType(completedFuture(n), completedFuture(n + 1), completedFuture(n + 2)).get()); - assertEquals(n, anyOfWithType(completedFuture(n), completedFuture(n + 1)).get()); + assertEquals(n, anyOf(completedFuture(n), completedFuture(n + 1), completedFuture(n + 2)).get()); + assertEquals(n, anyOf(completedFuture(n), completedFuture(n + 1)).get()); - assertEquals(n, anyOfWithType(completedFuture(n)).get()); - assertFalse(anyOfWithType().isDone()); + assertEquals(n, anyOf(completedFuture(n)).get()); + assertFalse(anyOf().isDone()); // success with incomplete CF - assertEquals(n, anyOfWithType(createIncompleteFuture(), createIncompleteFuture(), completedFuture(n)).get()); + assertEquals(n, anyOf(createIncompleteFuture(), createIncompleteFuture(), completedFuture(n)).get()); //////////////////////////////////////// - assertEquals(n, anyOfSuccessWithType(completedFuture(n), completedFuture(n + 1), completedFuture(n + 2)).get()); - assertEquals(n, anyOfSuccessWithType(completedFuture(n), completedFuture(n + 1)).get()); + assertEquals(n, anyOfSuccess(completedFuture(n), completedFuture(n + 1), completedFuture(n + 2)).get()); + assertEquals(n, anyOfSuccess(completedFuture(n), completedFuture(n + 1)).get()); - assertEquals(n, anyOfSuccessWithType(completedFuture(n)).get()); + assertEquals(n, anyOfSuccess(completedFuture(n)).get()); try { - anyOfSuccessWithType().get(); + anyOfSuccess().get(); fail(); } catch (ExecutionException expected) { @@ -340,7 +340,7 @@ void test_anyOf__success__trivial_case() throws Exception { } // success with incomplete CF - assertEquals(n, anyOfSuccessWithType( + assertEquals(n, anyOfSuccess( createIncompleteFuture(), createIncompleteFuture(), completedFuture(n) @@ -367,7 +367,7 @@ void test_anyOf__success__trivial_case() throws Exception { @Test void test_anyOf__exceptionally() throws Exception { // NOTE: skip anyOfSuccess test intended - // same implementation with anyOfSuccessWithType + // same implementation with anyOfSuccess //////////////////////////////////////// @@ -375,7 +375,7 @@ void test_anyOf__exceptionally() throws Exception { try { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); - anyOfWithType( + anyOf( CompletableFuture.supplyAsync(() -> { sleep(100); throw rte; @@ -387,13 +387,13 @@ void test_anyOf__exceptionally() throws Exception { fail(); } catch (ExecutionException expected) { - // anyOfSuccessWithType: the ex of first complete(in time) cf argument win + // anyOfSuccess: the ex of first complete(in time) cf argument win // ❗dependent on the implementation behavior of `CF.anyOf`️ assertSame(anotherRte, expected.getCause()); } // incomplete fail incomplete try { - anyOfWithType(createIncompleteFuture(), failedFuture(rte), createIncompleteFuture()).get(); + anyOf(createIncompleteFuture(), failedFuture(rte), createIncompleteFuture()).get(); fail(); } catch (ExecutionException expected) { @@ -406,7 +406,7 @@ void test_anyOf__exceptionally() throws Exception { try { RuntimeException ex1 = new RuntimeException("ex1"); RuntimeException ex2 = new RuntimeException("ex2"); - anyOfSuccessWithType( + anyOfSuccess( CompletableFuture.supplyAsync(() -> { sleep(100); throw rte; @@ -418,13 +418,13 @@ void test_anyOf__exceptionally() throws Exception { fail(); } catch (ExecutionException expected) { - // anyOfSuccessWithType: the ex of first given cf argument win + // anyOfSuccess: the ex of first given cf argument win // ❗dependent on the implementation behavior of `CF.allOf`️ assertSame(rte, expected.getCause()); } // incomplete fail incomplete try { - anyOfSuccessWithType( + anyOfSuccess( createIncompleteFuture(), failedFuture(rte), createIncompleteFuture() @@ -439,12 +439,12 @@ void test_anyOf__exceptionally() throws Exception { @Test void test_anyOf__concurrent() throws Exception { // NOTE: skip anyOfSuccess test intended - // same implementation with anyOfSuccessWithType + // same implementation with anyOfSuccess //////////////////////////////////////// // incomplete/wait-success then success - assertEquals(n, anyOfWithType( + assertEquals(n, anyOf( createIncompleteFuture(), createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { @@ -455,7 +455,7 @@ void test_anyOf__concurrent() throws Exception { ).get()); // wait/success then success - assertEquals(n, anyOfWithType( + assertEquals(n, anyOf( CompletableFuture.supplyAsync(() -> { sleep(300); return anotherN; @@ -468,7 +468,7 @@ void test_anyOf__concurrent() throws Exception { ).get()); // success then failed - assertEquals(n, anyOfWithType( + assertEquals(n, anyOf( createIncompleteFuture(), createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { @@ -480,7 +480,7 @@ void test_anyOf__concurrent() throws Exception { // failed then success try { - anyOfWithType( + anyOf( CompletableFuture.supplyAsync(() -> { sleep(100); return n; @@ -497,7 +497,7 @@ void test_anyOf__concurrent() throws Exception { //////////////////////////////////////// // incomplete/wait-success then success - assertEquals(n, anyOfSuccessWithType( + assertEquals(n, anyOfSuccess( createIncompleteFuture(), createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { @@ -508,7 +508,7 @@ void test_anyOf__concurrent() throws Exception { ).get()); // wait/success then success - assertEquals(n, anyOfSuccessWithType( + assertEquals(n, anyOfSuccess( CompletableFuture.supplyAsync(() -> { sleep(300); return anotherN; @@ -521,7 +521,7 @@ void test_anyOf__concurrent() throws Exception { ).get()); // success then failed - assertEquals(n, anyOfSuccessWithType( + assertEquals(n, anyOfSuccess( createIncompleteFuture(), createIncompleteFuture(), CompletableFuture.supplyAsync(() -> { @@ -532,7 +532,7 @@ void test_anyOf__concurrent() throws Exception { ).get()); // failed then success - assertEquals(n, anyOfSuccessWithType( + assertEquals(n, anyOfSuccess( CompletableFuture.supplyAsync(() -> { sleep(100); return n; diff --git a/cffu-core/src/test/java/io/foldright/demo/AllOfWithResultDemo.java b/cffu-core/src/test/java/io/foldright/demo/AllOfWithResultDemo.java index 9dcca380..09056bdb 100644 --- a/cffu-core/src/test/java/io/foldright/demo/AllOfWithResultDemo.java +++ b/cffu-core/src/test/java/io/foldright/demo/AllOfWithResultDemo.java @@ -18,7 +18,7 @@ public class AllOfWithResultDemo { public static void main(String[] args) throws Exception { ////////////////////////////////////////////////// - // CffuFactory#cffuAllOf + // CffuFactory#allResultsOf ////////////////////////////////////////////////// Cffu cffu1 = cffuFactory.completedFuture(21); Cffu cffu2 = cffuFactory.completedFuture(42); @@ -29,11 +29,11 @@ public static void main(String[] args) throws Exception { // the result can be got by input argument `cf1.get()`, but it's cumbersome. // so we can see a lot the util methods to enhance allOf with result in our project. - Cffu> allOfWithResult = cffuFactory.cffuAllOf(cffu1, cffu2); - System.out.println(allOfWithResult.get()); + Cffu> allResults = cffuFactory.allResultsOf(cffu1, cffu2); + System.out.println(allResults.get()); ////////////////////////////////////////////////// - // or CompletableFutureUtils#allOfWithResult + // or CompletableFutureUtils#allResultsOf ////////////////////////////////////////////////// CompletableFuture cf1 = CompletableFuture.completedFuture(21); CompletableFuture cf2 = CompletableFuture.completedFuture(42); @@ -41,8 +41,8 @@ public static void main(String[] args) throws Exception { CompletableFuture allOf2 = CompletableFuture.allOf(cf1, cf2); // Result type is Void!! - CompletableFuture> allOfWithResult2 = CompletableFutureUtils.allOfWithResult(cf1, cf2); - System.out.println(allOfWithResult2.get()); + CompletableFuture> allResults2 = CompletableFutureUtils.allResultsOf(cf1, cf2); + System.out.println(allResults2.get()); //////////////////////////////////////// // cleanup diff --git a/cffu-core/src/test/java/io/foldright/demo/ConcurrencyStrategyDemo.java b/cffu-core/src/test/java/io/foldright/demo/ConcurrencyStrategyDemo.java index 24ff252a..ec590a91 100644 --- a/cffu-core/src/test/java/io/foldright/demo/ConcurrencyStrategyDemo.java +++ b/cffu-core/src/test/java/io/foldright/demo/ConcurrencyStrategyDemo.java @@ -18,8 +18,8 @@ public class ConcurrencyStrategyDemo { public static void main(String[] args) throws Exception { //////////////////////////////////////////////////////////////////////// - // CffuFactory#cffuAllOfFastFail / allOfFastFail - // CffuFactory#cffuAnyOfSuccess / anyOfSuccess + // CffuFactory#allResultsOfFastFail / allOfFastFail + // CffuFactory#anyOfSuccess //////////////////////////////////////////////////////////////////////// final Cffu successAfterLongTime = cffuFactory.supplyAsync(() -> { sleep(3000); // sleep LONG time @@ -29,19 +29,16 @@ public static void main(String[] args) throws Exception { // Result type is Void! Cffu cffuAll = cffuFactory.allOfFastFail(successAfterLongTime, failed); - Cffu> fastFailed = cffuFactory.cffuAllOfFastFail(successAfterLongTime, failed); + Cffu> fastFailed = cffuFactory.allResultsOfFastFail(successAfterLongTime, failed); // fast failed without waiting successAfterLongTime System.out.println(fastFailed.exceptionNow()); - // Result type is Object! - Cffu cffuAny = cffuFactory.anyOfSuccess(successAfterLongTime, failed); - System.out.println(cffuAny.get()); - Cffu anyOfSuccess = cffuFactory.cffuAnyOfSuccess(successAfterLongTime, failed); + Cffu anyOfSuccess = cffuFactory.anyOfSuccess(successAfterLongTime, failed); System.out.println(anyOfSuccess.get()); //////////////////////////////////////////////////////////////////////// - // or CompletableFutureUtils#allOfFastFailWithResult / allOfFastFail - // CompletableFutureUtils#anyOfSuccessWithType / anyOfSuccess + // or CompletableFutureUtils#allResultsOfFastFail / allOfFastFail + // CompletableFutureUtils#anyOfSuccess / anyOfSuccess //////////////////////////////////////////////////////////////////////// final CompletableFuture successAfterLongTimeCf = CompletableFuture.supplyAsync(() -> { sleep(3000); // sleep LONG time @@ -51,14 +48,11 @@ public static void main(String[] args) throws Exception { // Result type is Void! CompletableFuture cfAll = CompletableFutureUtils.allOfFastFail(successAfterLongTimeCf, failedCf); - CompletableFuture> fastFailedCf = CompletableFutureUtils.allOfFastFailWithResult(successAfterLongTimeCf, failedCf); + CompletableFuture> fastFailedCf = CompletableFutureUtils.allResultsOfFastFail(successAfterLongTimeCf, failedCf); // fast failed without waiting successAfterLongTime System.out.println(CompletableFutureUtils.exceptionNow(fastFailedCf)); - // Result type is Object! - CompletableFuture cfAny = CompletableFutureUtils.anyOfSuccess(successAfterLongTimeCf, failedCf); - System.out.println(cfAny.get()); - CompletableFuture cfSuccess = CompletableFutureUtils.anyOfSuccessWithType(successAfterLongTimeCf, failedCf); + CompletableFuture cfSuccess = CompletableFutureUtils.anyOfSuccess(successAfterLongTimeCf, failedCf); System.out.println(cfSuccess.get()); //////////////////////////////////////// diff --git a/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt b/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt index 83c1ca27..48fc340e 100644 --- a/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt +++ b/cffu-core/src/test/java/io/foldright/test_utils/TestUtils.kt @@ -405,7 +405,7 @@ private fun Cffu.shouldMinCffu(recursive: Boolean = false) { //////////////////////////////////////////////////////////// shouldThrow { - cffuJoin(1, TimeUnit.MILLISECONDS) + join(1, TimeUnit.MILLISECONDS) }.message shouldBe "unsupported because this is a minimal stage" shouldThrow { cffuState() @@ -504,7 +504,7 @@ private fun Cffu.shouldNotMinCffu(recursive: Boolean = false) { // Cffu specified methods //////////////////////////////////////////////////////////// - cffuJoin(1, TimeUnit.MILLISECONDS) + join(1, TimeUnit.MILLISECONDS) cffuState() //# Cffu Re-Config methods diff --git a/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt b/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt index fddebfe0..7e7ddbad 100644 --- a/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt +++ b/cffu-kotlin/src/main/java/io/foldright/cffu/kotlin/CffuExtensions.kt @@ -71,18 +71,18 @@ private const val ERROR_MSG_FOR_ARRAY = "no cffuFactory argument provided when t * If no Cffus are provided, returns a Cffu completed with the value empty list. * * Same as [allOfCffuVoid], but the returned Cffu contains the results of input Cffus. - * Same as [CffuFactory.cffuAllOf], providing this method is convenient for method chaining. + * Same as [CffuFactory.allResultsOf], providing this method is convenient for method chaining. * * If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. * If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. * * @see allOfCffuVoid - * @see CffuFactory.cffuAllOf + * @see CffuFactory.allResultsOf */ -fun Collection>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu> { +fun Collection>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu> { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL) - return factory.cffuAllOf(*this.toTypedArray()) + return factory.allResultsOf(*this.toTypedArray()) } /** @@ -93,18 +93,18 @@ fun Collection>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu Array>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu> { +fun Array>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu> { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY) - return factory.cffuAllOf(*this) + return factory.allResultsOf(*this) } /** @@ -115,14 +115,14 @@ fun Array>.allOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu Collection>.allOfCffu(cffuFactory: CffuFactory): Cffu> = - cffuFactory.cffuAllOf(*this.toTypedArray()) +fun Collection>.allOfCffu(cffuFactory: CffuFactory): Cffu> = + cffuFactory.allResultsOf(*this.toTypedArray()) /** * Returns a new Cffu with the results in the same order of all the given CompletableFutures, @@ -132,13 +132,13 @@ fun Collection>.allOfCffu(cffuFactory: CffuFactory): Cf * If no CompletableFutures are provided, returns a Cffu completed with the value empty list. * * Same as [allOfCffuVoid], but the returned Cffu contains the results of input CompletableFutures. - * Same as [CffuFactory.cffuAllOf], providing this method is convenient for method chaining. + * Same as [CffuFactory.allResultsOf], providing this method is convenient for method chaining. * * @see allOfCffuVoid - * @see CffuFactory.cffuAllOf + * @see CffuFactory.allResultsOf */ -fun Array>.allOfCffu(cffuFactory: CffuFactory): Cffu> = - cffuFactory.cffuAllOf(*this) +fun Array>.allOfCffu(cffuFactory: CffuFactory): Cffu> = + cffuFactory.allResultsOf(*this) /** * Returns a new Cffu that is completed when all the given Cffus complete. @@ -227,18 +227,18 @@ fun Array>.allOfCffuVoid(cffuFactory: CffuFactory): Cff * If no CompletableFutures are provided, returns a Cffu completed with the value empty list. * * Same as [allOfFastFailCffuVoid], but the returned Cffu contains the results of input Cffus. - * Same as [CffuFactory.cffuAllOfFastFail], providing this method is convenient for method chaining. + * Same as [CffuFactory.allResultsOfFastFail], providing this method is convenient for method chaining. * * If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. * If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. * * @see allOfFastFailCffuVoid - * @see CffuFactory.cffuAllOfFastFail + * @see CffuFactory.allResultsOfFastFail */ -fun Collection>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cffu> { +fun Collection>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cffu> { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL) - return factory.cffuAllOfFastFail(*this.toTypedArray()) + return factory.allResultsOfFastFail(*this.toTypedArray()) } /** @@ -250,18 +250,18 @@ fun Collection>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT) * If no CompletableFutures are provided, returns a Cffu completed with the value empty list. * * Same as [allOfFastFailCffuVoid], but the returned Cffu contains the results of input Cffus. - * Same as [CffuFactory.cffuAllOfFastFail], providing this method is convenient for method chaining. + * Same as [CffuFactory.allResultsOfFastFail], providing this method is convenient for method chaining. * * If this array is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. * If this array is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. * * @see allOfFastFailCffuVoid - * @see CffuFactory.cffuAllOfFastFail + * @see CffuFactory.allResultsOfFastFail */ -fun Array>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cffu> { +fun Array>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cffu> { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY) - return factory.cffuAllOfFastFail(*this) + return factory.allResultsOfFastFail(*this) } /** @@ -273,14 +273,14 @@ fun Array>.allOfFastFailCffu(cffuFactory: CffuFactory = ABSENT): Cff * If no CompletableFutures are provided, returns a Cffu completed with the value empty list. * * Same as [allOfFastFailCffuVoid], but the returned Cffu contains the results of input Cffus. - * Same as [CffuFactory.cffuAllOfFastFail], providing this method is convenient for method chaining. + * Same as [CffuFactory.allResultsOfFastFail], providing this method is convenient for method chaining. * * @see allOfFastFailCffuVoid - * @see CffuFactory.cffuAllOfFastFail + * @see CffuFactory.allResultsOfFastFail */ @JvmName("allOfFastFailCffuCf") -fun Collection>.allOfFastFailCffu(cffuFactory: CffuFactory): Cffu> = - cffuFactory.cffuAllOfFastFail(*this.toTypedArray()) +fun Collection>.allOfFastFailCffu(cffuFactory: CffuFactory): Cffu> = + cffuFactory.allResultsOfFastFail(*this.toTypedArray()) /** * Returns a new Cffu with the results in the same order of all the given CompletableFutures, @@ -291,13 +291,13 @@ fun Collection>.allOfFastFailCffu(cffuFactory: CffuFact * If no CompletableFutures are provided, returns a Cffu completed with the value empty list. * * Same as [allOfFastFailCffuVoid], but the returned Cffu contains the results of input Cffus. - * Same as [CffuFactory.cffuAllOfFastFail], providing this method is convenient for method chaining. + * Same as [CffuFactory.allResultsOfFastFail], providing this method is convenient for method chaining. * * @see allOfFastFailCffuVoid - * @see CffuFactory.cffuAllOfFastFail + * @see CffuFactory.allResultsOfFastFail */ -fun Array>.allOfFastFailCffu(cffuFactory: CffuFactory): Cffu> = - cffuFactory.cffuAllOfFastFail(*this) +fun Array>.allOfFastFailCffu(cffuFactory: CffuFactory): Cffu> = + cffuFactory.allResultsOfFastFail(*this) /** * Returns a new Cffu that is successful when all the given Cffus success, @@ -389,67 +389,6 @@ fun Array>.allOfFastFailCffuVoid(cffuFactory: CffuFacto // - anyOfSuccessCffuAny //////////////////////////////////////// -/** - * Returns a new Cffu that is completed when any of the given Cffus complete, with the same result. - * - * Same as [anyOfCffuAny], but return result type is specified type instead of type `Any`. - * Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining. - * - * If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. - * If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. - * - * @see anyOfCffuAny - * @see CffuFactory.cffuAnyOf - */ -fun Collection>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu { - val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory - else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL) - return factory.cffuAnyOf(*this.toTypedArray()) -} - -/** - * Returns a new Cffu that is completed when any of the given Cffus complete, with the same result. - * - * Same as [anyOfCffuAny], but return result type is specified type instead of type `Any`. - * Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining. - * - * If this array is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. - * If this array is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. - * - * @see anyOfCffuAny - * @see CffuFactory.cffuAnyOf - */ -fun Array>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu { - val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory - else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY) - return factory.cffuAnyOf(*this) -} - -/** - * Returns a new Cffu that is completed when any of the given CompletableFutures complete, with the same result. - * - * Same as [anyOfCffuAny], but return result type is specified type instead of type `Any`. - * Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining. - * - * @see anyOfCffuAny - * @see CffuFactory.cffuAnyOf - */ -@JvmName("anyOfCffuCf") -fun Collection>.anyOfCffu(cffuFactory: CffuFactory): Cffu = - cffuFactory.cffuAnyOf(*this.toTypedArray()) - -/** - * Returns a new Cffu that is completed when any of the given CompletableFutures complete, with the same result. - * - * Same as [anyOfCffuAny], but return result type is specified type instead of type `Any`. - * Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining. - * - * @see anyOfCffuAny - * @see CffuFactory.cffuAnyOf - */ -fun Array>.anyOfCffu(cffuFactory: CffuFactory): Cffu = - cffuFactory.cffuAnyOf(*this) - /** * Returns a new Cffu that is completed when any of the given Cffus complete, with the same result. * @@ -458,10 +397,9 @@ fun Array>.anyOfCffu(cffuFactory: CffuFactory): Cffu * If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. * If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. * - * @see anyOfCffu * @see CffuFactory.anyOf */ -fun Collection>.anyOfCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu { +fun Collection>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL) return factory.anyOf(*this.toTypedArray()) @@ -475,10 +413,9 @@ fun Collection>.anyOfCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu>.anyOfCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu { +fun Array>.anyOfCffu(cffuFactory: CffuFactory = ABSENT): Cffu { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY) return factory.anyOf(*this) @@ -489,11 +426,10 @@ fun Array>.anyOfCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu>.anyOfCffuAny(cffuFactory: CffuFactory): Cffu = +@JvmName("anyOfCffuCf") +fun Collection>.anyOfCffu(cffuFactory: CffuFactory): Cffu = cffuFactory.anyOf(*this.toTypedArray()) /** @@ -501,10 +437,9 @@ fun Collection>.anyOfCffuAny(cffuFactory: CffuFactory): Cff * * Same as [CffuFactory.anyOf], providing this method is convenient for method chaining. * - * @see anyOfCffu * @see CffuFactory.anyOf */ -fun Array>.anyOfCffuAny(cffuFactory: CffuFactory): Cffu = +fun Array>.anyOfCffu(cffuFactory: CffuFactory): Cffu = cffuFactory.anyOf(*this) /** @@ -515,18 +450,18 @@ fun Array>.anyOfCffuAny(cffuFactory: CffuFactory): Cffu * returns a new Cffu that is already completed exceptionally with a CompletionException * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. * - * Same as [CffuFactory.cffuAnyOfSuccess], providing this method is convenient for method chaining. + * Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining. * * If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. * If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. * * @see anyOfCffu - * @see CffuFactory.cffuAnyOfSuccess + * @see CffuFactory.anyOfSuccess */ -fun Collection>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu { +fun Collection>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL) - return factory.cffuAnyOfSuccess(*this.toTypedArray()) + return factory.anyOfSuccess(*this.toTypedArray()) } /** @@ -537,92 +472,15 @@ fun Collection>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): * returns a new Cffu that is already completed exceptionally with a CompletionException * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. * - * Same as [CffuFactory.cffuAnyOfSuccess], providing this method is convenient for method chaining. - * - * If this array is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. - * If this array is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. - * - * @see anyOfCffu - * @see CffuFactory.cffuAnyOfSuccess - */ -fun Array>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu { - val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory - else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY) - return factory.cffuAnyOfSuccess(*this) -} - -/** - * Returns a new Cffu that is successful when any of the given CompletableFutures success, - * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, - * the returned Cffu also does so, with a CompletionException holding - * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new Cffu that is already completed exceptionally with a CompletionException - * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. - * - * Same as [CffuFactory.cffuAnyOfSuccess], providing this method is convenient for method chaining. - * - * @see anyOfCffu - * @see CffuFactory.cffuAnyOfSuccess - */ -@JvmName("anyOfSuccessCffuCf") -fun Collection>.anyOfSuccessCffu(cffuFactory: CffuFactory): Cffu = - cffuFactory.cffuAnyOfSuccess(*this.toTypedArray()) - -/** - * Returns a new Cffu that is successful when any of the given CompletableFutures success, - * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, - * the returned Cffu also does so, with a CompletionException holding - * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new Cffu that is already completed exceptionally with a CompletionException - * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. - * - * Same as [CffuFactory.cffuAnyOfSuccess], providing this method is convenient for method chaining. - * - * @see anyOfCffu - * @see CffuFactory.cffuAnyOfSuccess - */ -fun Array>.anyOfSuccessCffu(cffuFactory: CffuFactory): Cffu = - cffuFactory.cffuAnyOfSuccess(*this) - -/** - * Returns a new Cffu that is successful when any of the given CompletableFutures success, - * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, - * the returned Cffu also does so, with a CompletionException holding - * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new Cffu that is already completed exceptionally with a CompletionException - * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. - * - * Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining. - * - * If this collection is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. - * If this collection is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. - * - * @see anyOfSuccessCffu - * @see CffuFactory.anyOfSuccess - */ -fun Collection>.anyOfSuccessCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu { - val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory - else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL) - return factory.anyOfSuccess(*this.toTypedArray()) -} - -/** - * Returns a new Cffu that is successful when any of the given CompletableFutures success, - * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, - * the returned Cffu also does so, with a CompletionException holding - * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new Cffu that is already completed exceptionally with a CompletionException - * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. - * * Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining. * * If this array is not empty, `cffuFactory` argument is optional, use the `cffuFactory` of the first cffu element. * If this array is empty and no`cffuFactory` provided, throw [IllegalArgumentException]. * - * @see anyOfSuccessCffu + * @see anyOfCffu * @see CffuFactory.anyOfSuccess */ -fun Array>.anyOfSuccessCffuAny(cffuFactory: CffuFactory = ABSENT): Cffu { +fun Array>.anyOfSuccessCffu(cffuFactory: CffuFactory = ABSENT): Cffu { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY) return factory.anyOfSuccess(*this) @@ -638,11 +496,11 @@ fun Array>.anyOfSuccessCffuAny(cffuFactory: CffuFactory = ABSENT): C * * Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining. * - * @see anyOfSuccessCffu + * @see anyOfCffu * @see CffuFactory.anyOfSuccess */ -@JvmName("anyOfSuccessCffuAnyCf") -fun Collection>.anyOfSuccessCffuAny(cffuFactory: CffuFactory): Cffu = +@JvmName("anyOfSuccessCffuCf") +fun Collection>.anyOfSuccessCffu(cffuFactory: CffuFactory): Cffu = cffuFactory.anyOfSuccess(*this.toTypedArray()) /** @@ -655,10 +513,10 @@ fun Collection>.anyOfSuccessCffuAny(cffuFactory: CffuFactor * * Same as [CffuFactory.anyOfSuccess], providing this method is convenient for method chaining. * - * @see anyOfSuccessCffu + * @see anyOfCffu * @see CffuFactory.anyOfSuccess */ -fun Array>.anyOfSuccessCffuAny(cffuFactory: CffuFactory): Cffu = +fun Array>.anyOfSuccessCffu(cffuFactory: CffuFactory): Cffu = cffuFactory.anyOfSuccess(*this) //////////////////////////////////////// @@ -668,7 +526,7 @@ fun Array>.anyOfSuccessCffuAny(cffuFactory: CffuFactory /** * Convert [Cffu] collection elements to [CompletableFuture] by [Cffu.toCompletableFuture]. * - * Same as [CffuFactory.cffuAnyOf], providing this method is convenient for method chaining. + * Same as [CffuFactory.anyOf], providing this method is convenient for method chaining. * * @see CffuFactory.toCompletableFutureArray */ @@ -678,7 +536,7 @@ fun Collection>.toCompletableFuture(): List Collection>.cffuUnwrap(): List> = * * @see CffuFactory.cffuArrayUnwrap */ -fun Array>.cffuUnwrap(): Array> = +fun Array>.cffuUnwrap(): Array> = CffuFactory.cffuArrayUnwrap(*this) 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 4cedf695..82b31d94 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 @@ -35,13 +35,13 @@ import java.util.concurrent.TimeUnit * * Same as [allOfCompletableFutureVoid], * but the returned CompletableFuture contains the results of input CompletableFutures. - * Same as [CompletableFutureUtils.allOfWithResult], providing this method is convenient for method chaining. + * Same as [CompletableFutureUtils.allResultsOf], providing this method is convenient for method chaining. * * @see allOfCffu * @see allOfCompletableFutureVoid */ -fun Collection>.allOfCompletableFuture(): CompletableFuture> = - CompletableFutureUtils.allOfWithResult(*this.toTypedArray()) +fun Collection>.allOfCompletableFuture(): CompletableFuture> = + CompletableFutureUtils.allResultsOf(*this.toTypedArray()) /** * Returns a new CompletableFuture with the results in the same order of all the given @@ -52,13 +52,13 @@ fun Collection>.allOfCompletableFuture(): CompletableFu * * Same as [allOfCompletableFutureVoid], * but the returned CompletableFuture contains the results of input CompletableFutures. - * Same as [CompletableFutureUtils.allOfWithResult], providing this method is convenient for method chaining. + * Same as [CompletableFutureUtils.allResultsOf], providing this method is convenient for method chaining. * * @see allOfCffu * @see allOfCompletableFutureVoid */ -fun Array>.allOfCompletableFuture(): CompletableFuture> = - CompletableFutureUtils.allOfWithResult(*this) +fun Array>.allOfCompletableFuture(): CompletableFuture> = + CompletableFutureUtils.allResultsOf(*this) /** * Returns a new CompletableFuture that is completed when all the given CompletableFutures complete. @@ -114,13 +114,13 @@ fun Array>.allOfCompletableFutureVoid(): CompletableFut * * Same as [allOfFastFailCompletableFutureVoid], * but the returned CompletableFuture contains the results of input CompletableFutures. - * Same as [CompletableFutureUtils.allOfFastFailWithResult], providing this method is convenient for method chaining. + * Same as [CompletableFutureUtils.allResultsOfFastFail], providing this method is convenient for method chaining. * * @see allOfFastFailCffu * @see allOfFastFailCompletableFutureVoid */ -fun Collection>.allOfFastFailCompletableFuture(): CompletableFuture> = - CompletableFutureUtils.allOfFastFailWithResult(*this.toTypedArray()) +fun Collection>.allOfFastFailCompletableFuture(): CompletableFuture> = + CompletableFutureUtils.allResultsOfFastFail(*this.toTypedArray()) /** * Returns a new CompletableFuture with the results in the same order of all the given @@ -132,13 +132,13 @@ fun Collection>.allOfFastFailCompletableFuture(): Compl * * Same as [allOfFastFailCompletableFutureVoid], * but the returned CompletableFuture contains the results of input CompletableFutures. - * Same as [CompletableFutureUtils.allOfFastFailWithResult], providing this method is convenient for method chaining. + * Same as [CompletableFutureUtils.allResultsOfFastFail], providing this method is convenient for method chaining. * * @see allOfFastFailCffu * @see allOfFastFailCompletableFutureVoid */ -fun Array>.allOfFastFailCompletableFuture(): CompletableFuture> = - CompletableFutureUtils.allOfFastFailWithResult(*this) +fun Array>.allOfFastFailCompletableFuture(): CompletableFuture> = + CompletableFutureUtils.allResultsOfFastFail(*this) /** * Returns a new CompletableFuture that is successful when all the given CompletableFutures success, @@ -183,9 +183,7 @@ fun Array>.allOfFastFailCompletableFutureVoid(): Comple //# anyOf* methods // // - anyOfCompletableFuture -// - anyOfCompletableFutureAny // - anyOfSuccessCompletableFuture -// - anyOfSuccessCompletableFutureAny //////////////////////////////////////// /** @@ -195,14 +193,12 @@ fun Array>.allOfFastFailCompletableFutureVoid(): Comple * with a CompletionException holding this exception as its cause. * If no CompletableFutures are provided, returns an incomplete CompletableFuture. * - * Same as [anyOfCompletableFutureAny], but return result type is specified type instead of type `Any`. - * Same as [CompletableFutureUtils.anyOfWithType], providing this method is convenient for method chaining. + * Same as [CompletableFutureUtils.anyOf], providing this method is convenient for method chaining. * * @see anyOfCffu - * @see anyOfCompletableFutureAny */ -fun Collection>.anyOfCompletableFuture(): CompletableFuture = - CompletableFutureUtils.anyOfWithType(*this.toTypedArray()) +fun Collection>.anyOfCompletableFuture(): CompletableFuture = + CompletableFutureUtils.anyOf(*this.toTypedArray()) /** * Returns a new CompletableFuture that is completed @@ -211,82 +207,12 @@ fun Collection>.anyOfCompletableFuture(): CompletableFu * with a CompletionException holding this exception as its cause. * If no CompletableFutures are provided, returns an incomplete CompletableFuture. * - * Same as [anyOfCompletableFutureAny], but return result type is specified type instead of type `Any`. - * Same as [CompletableFutureUtils.anyOfWithType], providing this method is convenient for method chaining. + * Same as [CompletableFutureUtils.anyOf], providing this method is convenient for method chaining. * * @see anyOfCffu - * @see anyOfCompletableFutureAny */ -fun Array>.anyOfCompletableFuture(): CompletableFuture = - CompletableFutureUtils.anyOfWithType(*this) - -/** - * Returns a new CompletableFuture that is completed - * when any of the given CompletableFutures complete, with the same result. - * Otherwise, if it completed exceptionally, the returned CompletableFuture also does so, - * with a CompletionException holding this exception as its cause. - * If no CompletableFutures are provided, returns an incomplete CompletableFuture. - * - * Same as [CompletableFuture.anyOf], providing this method is convenient for method chaining. - * - * @see anyOfCffu - * @see anyOfCffuAny - * @see anyOfCompletableFuture - * @see CompletableFuture.anyOf - */ -fun Collection>.anyOfCompletableFutureAny(): CompletableFuture = - CompletableFuture.anyOf(*this.toTypedArray()) - -/** - * Returns a new CompletableFuture that is completed - * when any of the given CompletableFutures complete, with the same result. - * Otherwise, if it completed exceptionally, the returned CompletableFuture also does so, - * with a CompletionException holding this exception as its cause. - * If no CompletableFutures are provided, returns an incomplete CompletableFuture. - * - * Same as [CompletableFuture.anyOf], providing this method is convenient for method chaining. - * - * @see anyOfCffu - * @see anyOfCffuAny - * @see anyOfCompletableFuture - * @see CompletableFuture.anyOf - */ -fun Array>.anyOfCompletableFutureAny(): CompletableFuture = - CompletableFuture.anyOf(*this) - -/** - * Returns a new CompletableFuture that is successful when any of the given CompletableFutures success, - * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, - * the returned CompletableFuture also does so, with a CompletionException holding - * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new CompletableFuture that is already completed exceptionally with a CompletionException - * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. - * - * Same as [anyOfSuccessCompletableFutureAny], but return result type is specified type instead of type `Any`. - * Same as [CompletableFutureUtils.anyOfSuccessWithType], providing this method is convenient for method chaining. - * - * @see anyOfCompletableFuture - * @see CompletableFutureUtils.anyOfSuccessWithType - */ -fun Collection>.anyOfSuccessCompletableFuture(): CompletableFuture = - CompletableFutureUtils.anyOfSuccessWithType(*this.toTypedArray()) - -/** - * Returns a new CompletableFuture that is successful when any of the given CompletableFutures success, - * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, - * the returned CompletableFuture also does so, with a CompletionException holding - * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new CompletableFuture that is already completed exceptionally with a CompletionException - * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. - * - * Same as [anyOfSuccessCompletableFutureAny], but return result type is specified type instead of type `Any`. - * Same as [CompletableFutureUtils.anyOfSuccessWithType], providing this method is convenient for method chaining. - * - * @see anyOfCompletableFuture - * @see CompletableFutureUtils.anyOfSuccessWithType - */ -fun Array>.anyOfSuccessCompletableFuture(): CompletableFuture = - CompletableFutureUtils.anyOfSuccessWithType(*this) +fun Array>.anyOfCompletableFuture(): CompletableFuture = + CompletableFutureUtils.anyOf(*this) /** * Returns a new CompletableFuture that is successful when any of the given CompletableFutures success, @@ -298,28 +224,26 @@ fun Array>.anyOfSuccessCompletableFuture(): Complet * * Same as [CompletableFutureUtils.anyOfSuccess], providing this method is convenient for method chaining. * - * @see anyOfSuccessCffu * @see anyOfCompletableFuture * @see CompletableFutureUtils.anyOfSuccess */ -fun Collection>.anyOfSuccessCompletableFutureAny(): CompletableFuture = +fun Collection>.anyOfSuccessCompletableFuture(): CompletableFuture = CompletableFutureUtils.anyOfSuccess(*this.toTypedArray()) /** * Returns a new CompletableFuture that is successful when any of the given CompletableFutures success, * with the same result. Otherwise, all the given CompletableFutures complete exceptionally, * the returned CompletableFuture also does so, with a CompletionException holding - * an exception CompletableFuture any of the given CompletableFutures as its cause. If no CompletableFutures are provided, - * returns a new Cffu that is already completed exceptionally with a CompletionException + * an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, + * returns a new CompletableFuture that is already completed exceptionally with a CompletionException * holding a [NoCfsProvidedException][io.foldright.cffu.NoCfsProvidedException] as its cause. * * Same as [CompletableFutureUtils.anyOfSuccess], providing this method is convenient for method chaining. * - * @see anyOfSuccessCffu * @see anyOfCompletableFuture * @see CompletableFutureUtils.anyOfSuccess */ -fun Array>.anyOfSuccessCompletableFutureAny(): CompletableFuture = +fun Array>.anyOfSuccessCompletableFuture(): CompletableFuture = CompletableFutureUtils.anyOfSuccess(*this) //////////////////////////////////////// @@ -587,7 +511,7 @@ fun CompletableFuture.cffuExceptionallyComposeAsync( * @see CompletableFuture.join */ @Suppress("UNCHECKED_CAST") -fun CompletableFuture.cffuJoin(timeout: Long, unit: TimeUnit): T = +fun CompletableFuture.join(timeout: Long, unit: TimeUnit): T = CompletableFutureUtils.join(this, timeout, unit) as T /** diff --git a/cffu-kotlin/src/test/java/io/foldright/cffu/test/CffuExtensionsTest.kt b/cffu-kotlin/src/test/java/io/foldright/cffu/test/CffuExtensionsTest.kt index b3cecbdd..d86ff545 100644 --- a/cffu-kotlin/src/test/java/io/foldright/cffu/test/CffuExtensionsTest.kt +++ b/cffu-kotlin/src/test/java/io/foldright/cffu/test/CffuExtensionsTest.kt @@ -267,37 +267,37 @@ class CffuExtensionsTest : FunSpec({ testCffuFactory.newIncompleteCffu(), testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), - ).anyOfCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfCffu(testCffuFactory).await() shouldBe 42 setOf( testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), testCffuFactory.newIncompleteCffu(), - ).anyOfCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfCffu(testCffuFactory).await() shouldBe 42 listOf( CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfCffu(testCffuFactory).await() shouldBe 42 setOf( CompletableFuture(), CompletableFuture.completedFuture(42), CompletableFuture(), - ).anyOfCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfCffu(testCffuFactory).await() shouldBe 42 arrayOf( testCffuFactory.newIncompleteCffu(), testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), - ).anyOfCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfCffu(testCffuFactory).await() shouldBe 42 arrayOf( CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfCffu(testCffuFactory).await() shouldBe 42 //////////////////////////////////////// @@ -347,37 +347,37 @@ class CffuExtensionsTest : FunSpec({ testCffuFactory.newIncompleteCffu(), testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), - ).anyOfSuccessCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfSuccessCffu(testCffuFactory).await() shouldBe 42 setOf( testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), testCffuFactory.newIncompleteCffu(), - ).anyOfSuccessCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfSuccessCffu(testCffuFactory).await() shouldBe 42 listOf( CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfSuccessCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfSuccessCffu(testCffuFactory).await() shouldBe 42 setOf( CompletableFuture(), CompletableFuture.completedFuture(42), CompletableFuture(), - ).anyOfSuccessCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfSuccessCffu(testCffuFactory).await() shouldBe 42 arrayOf( testCffuFactory.newIncompleteCffu(), testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), - ).anyOfSuccessCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfSuccessCffu(testCffuFactory).await() shouldBe 42 arrayOf( CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfSuccessCffuAny(testCffuFactory).await() shouldBe 42 + ).anyOfSuccessCffu(testCffuFactory).await() shouldBe 42 } @@ -430,20 +430,20 @@ class CffuExtensionsTest : FunSpec({ assertEmptyArray { emptyArray.anyOfCffu() } assertCffuFactoryForOptional(array.anyOfCffu()) - assertEmptyCollection { emptyList.anyOfCffuAny() } - assertCffuFactoryForOptional(list.anyOfCffuAny()) - assertEmptyArray { emptyArray.anyOfCffuAny() } - assertCffuFactoryForOptional(array.anyOfCffuAny()) + assertEmptyCollection { emptyList.anyOfCffu() } + assertCffuFactoryForOptional(list.anyOfCffu()) + assertEmptyArray { emptyArray.anyOfCffu() } + assertCffuFactoryForOptional(array.anyOfCffu()) assertEmptyCollection { emptyList.anyOfSuccessCffu() } assertCffuFactoryForOptional(list.anyOfSuccessCffu()) assertEmptyArray { emptyArray.anyOfSuccessCffu() } assertCffuFactoryForOptional(array.anyOfSuccessCffu()) - assertEmptyCollection { emptyList.anyOfSuccessCffuAny() } - assertCffuFactoryForOptional(list.anyOfSuccessCffuAny()) - assertEmptyArray { emptyArray.anyOfSuccessCffuAny() } - assertCffuFactoryForOptional(array.anyOfSuccessCffuAny()) + assertEmptyCollection { emptyList.anyOfSuccessCffu() } + assertCffuFactoryForOptional(list.anyOfSuccessCffu()) + assertEmptyArray { emptyArray.anyOfSuccessCffu() } + assertCffuFactoryForOptional(array.anyOfSuccessCffu()) } //////////////////////////////////////// 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 56e11f7c..2eb7aaac 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 @@ -139,21 +139,21 @@ class CompletableFutureExtensionsTest : FunSpec({ CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfCompletableFutureAny().await() shouldBe 42 - listOf>().anyOfCompletableFutureAny().isDone.shouldBeFalse() + ).anyOfCompletableFuture().await() shouldBe 42 + listOf>().anyOfCompletableFuture().isDone.shouldBeFalse() setOf( CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfCompletableFutureAny().await() shouldBe 42 + ).anyOfCompletableFuture().await() shouldBe 42 arrayOf( CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfCompletableFutureAny().await() shouldBe 42 - arrayOf>().anyOfCompletableFutureAny().isDone.shouldBeFalse() + ).anyOfCompletableFuture().await() shouldBe 42 + arrayOf>().anyOfCompletableFuture().isDone.shouldBeFalse() //////////////////////////////////////// @@ -187,24 +187,24 @@ class CompletableFutureExtensionsTest : FunSpec({ CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfSuccessCompletableFutureAny().await() shouldBe 42 + ).anyOfSuccessCompletableFuture().await() shouldBe 42 shouldThrow { - listOf>().anyOfSuccessCompletableFutureAny().await() + listOf>().anyOfSuccessCompletableFuture().await() } setOf( CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfSuccessCompletableFutureAny().await() shouldBe 42 + ).anyOfSuccessCompletableFuture().await() shouldBe 42 arrayOf( CompletableFuture(), CompletableFuture(), CompletableFuture.completedFuture(42), - ).anyOfSuccessCompletableFutureAny().await() shouldBe 42 + ).anyOfSuccessCompletableFuture().await() shouldBe 42 shouldThrow { - arrayOf>().anyOfSuccessCompletableFutureAny().await() + arrayOf>().anyOfSuccessCompletableFuture().await() } } @@ -296,7 +296,7 @@ class CompletableFutureExtensionsTest : FunSpec({ val cf = CompletableFuture.completedFuture(n) val ff = CompletableFutureUtils.failedFuture(rte) - cf.cffuJoin(1, TimeUnit.MILLISECONDS) shouldBe n + cf.join(1, TimeUnit.MILLISECONDS) shouldBe n cf.cffuResultNow() shouldBe n ff.cffuExceptionNow() shouldBeSameInstanceAs rte diff --git a/demos/cffu-demo/scripts/run.sh b/demos/cffu-demo/scripts/run.sh index eafadecc..96b22028 100755 --- a/demos/cffu-demo/scripts/run.sh +++ b/demos/cffu-demo/scripts/run.sh @@ -9,3 +9,5 @@ source "$BABY_ROOT"/lib/common_utils.sh source "$BABY_ROOT"/lib/maven_utils.sh mvu::mvn_cmd clean compile exec:exec +mvu::mvn_cmd exec:exec -Dexec.main.class=io.foldright.demo.cffu.CompletableFutureUtilsDemo +mvu::mvn_cmd exec:exec -Dexec.main.class=io.foldright.demo.cffu.CovariantDemo diff --git a/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CffuDemo.java b/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CffuDemo.java index 87e72a01..b3c9dc15 100644 --- a/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CffuDemo.java +++ b/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CffuDemo.java @@ -41,7 +41,7 @@ public static void main(String[] args) throws Exception { final Cffu combined = longTaskA.thenCombine(longTaskB, Integer::sum) .orTimeout(1500, TimeUnit.MILLISECONDS); System.out.println("combined result: " + combined.get()); - final Cffu anyOfSuccess = cffuFactory.cffuAnyOfSuccess(longTaskC, longFailedTask); + final Cffu anyOfSuccess = cffuFactory.anyOfSuccess(longTaskC, longFailedTask); System.out.println("anyOfSuccess result: " + anyOfSuccess.get()); //////////////////////////////////////// diff --git a/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CompletableFutureUtilsDemo.java b/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CompletableFutureUtilsDemo.java index 4fcdea7c..1e5fe0d3 100644 --- a/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CompletableFutureUtilsDemo.java +++ b/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CompletableFutureUtilsDemo.java @@ -36,7 +36,7 @@ public static void main(String[] args) throws Exception { final CompletableFuture combined = longTaskA.thenCombine(longTaskB, Integer::sum); final CompletableFuture combinedWithTimeout = CompletableFutureUtils.orTimeout(combined, 1500, TimeUnit.MILLISECONDS); System.out.println("combined result: " + combinedWithTimeout.get()); - final CompletableFuture anyOfSuccess = CompletableFutureUtils.anyOfSuccessWithType(longTaskC, longFailedTask); + final CompletableFuture anyOfSuccess = CompletableFutureUtils.anyOfSuccess(longTaskC, longFailedTask); System.out.println("anyOfSuccess result: " + anyOfSuccess.get()); //////////////////////////////////////// diff --git a/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CovariantDemo.java b/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CovariantDemo.java new file mode 100644 index 00000000..4363a5b6 --- /dev/null +++ b/demos/cffu-demo/src/main/java/io/foldright/demo/cffu/CovariantDemo.java @@ -0,0 +1,62 @@ +package io.foldright.demo.cffu; + +import io.foldright.cffu.CompletableFutureUtils; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + + +public class CovariantDemo { + public static void main(String[] args) { + useAllOf(); + useAnyOf(); + } + + public static void useAllOf() { + CompletableFuture> cf1 = CompletableFutureUtils.allResultsOf( + CompletableFuture.completedFuture(42), + CompletableFuture.completedFuture(42.0) + ); + cf1.thenAccept(System.out::println); + + CompletableFutureUtils.allResultsOf( + CompletableFuture.completedFuture(42), + CompletableFuture.completedFuture(42.0) + ) + .thenApply(numbers -> "to string: " + numbers.toString()) + .thenAccept(System.out::println); + + CompletableFuture> cf5 = CompletableFutureUtils.allResultsOf( + CompletableFuture.completedFuture(42), + CompletableFuture.completedFuture(42) + ); + cf5.thenAccept(System.out::println); + } + + public static void useAnyOf() { + CompletableFuture cf1 = CompletableFutureUtils.anyOfSuccess( + CompletableFuture.completedFuture(42), + CompletableFuture.completedFuture(42.0) + ).thenApply(Number::byteValue); + cf1.thenAccept(System.out::println); + + CompletableFuture cf2 = CompletableFutureUtils.anyOfSuccess( + CompletableFuture.completedFuture(42), + CompletableFuture.completedFuture(42.0) + ); + cf2.thenAccept(System.out::println); + + CompletableFutureUtils.anyOfSuccess( + CompletableFuture.completedFuture(42), + CompletableFuture.completedFuture(42.0) + ) + .thenApply(Number::byteValue) + .thenAccept(System.out::println); + + CompletableFuture cf5 = CompletableFutureUtils.anyOfSuccess( + CompletableFuture.completedFuture(42), + CompletableFuture.completedFuture(42) + ); + cf5.thenAccept(System.out::println); + } +}