From 4343f1f137986e2124efc7de9aacd2db1dd2f944 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 15 Jun 2024 19:45:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20move=20default=20value=20argument?= =?UTF-8?q?=20forward=20=E2=9A=A0=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because the nullable default value argument and last varargs are not distinguished by compiler, missing the default argument can NOT point out at compile-time. --- .../java/io/foldright/cffu/CffuFactory.java | 22 +++--- .../cffu/CompletableFutureUtils.java | 78 +++++++++---------- .../io/foldright/cffu/CffuFactoryTest.java | 10 +-- .../cffu/CompletableFutureUtilsTest.java | 40 ++++------ .../foldright/cffu/kotlin/CffuExtensions.kt | 16 ++-- .../kotlin/CompletableFutureExtensions.kt | 16 ++-- .../foldright/cffu/test/CffuExtensionsTest.kt | 22 +++--- .../test/CompletableFutureExtensionsTest.kt | 8 +- 8 files changed, 102 insertions(+), 110 deletions(-) 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 c37e07e7..b2e96b31 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -303,7 +303,7 @@ public final Cffu[] toCffuArray(CompletionStage... stages) { *

* If you need the successful results of given stages in the given time, prefer below methods: *

    - *
  • {@link #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])} + *
  • {@link #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])} *
  • {@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage)} *
  • {@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)} *
@@ -314,7 +314,7 @@ public final Cffu[] toCffuArray(CompletionStage... stages) { * @see #allResultsOf(CompletionStage[]) * @see #allTupleOf(CompletionStage, CompletionStage) * @see #allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage) * @see #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) * @see CompletableFutureUtils#allOf(CompletionStage[]) @@ -366,7 +366,7 @@ public final Cffu> allResultsOf(CompletionStage... cfs) *

* If you need the successful results of given stages in the given time, prefer below methods: *

    - *
  • {@link #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])} + *
  • {@link #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])} *
  • {@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage)} *
  • {@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)} *
@@ -377,7 +377,7 @@ public final Cffu> allResultsOf(CompletionStage... cfs) * @see #allResultsOfFastFail(CompletionStage[]) * @see #allTupleOfFastFail(CompletionStage, CompletionStage) * @see #allTupleOfFastFail(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage) * @see #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) * @see #allOf(CompletionStage[]) @@ -418,18 +418,18 @@ public final Cffu> allResultsOfFastFail(CompletionStage *

* If the given stage is successful, its result is the completed value; Otherwise the given valueIfNotSuccess. * + * @param valueIfNotSuccess the value to return if not completed successfully * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter - * @param valueIfNotSuccess the value to return if not completed successfully * @param cfs the stages * @see Cffu#getSuccessNow(Object) */ @Contract(pure = true) @SafeVarargs public final Cffu> mostResultsOfSuccess( - long timeout, TimeUnit unit, @Nullable T valueIfNotSuccess, CompletionStage... cfs) { + @Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, CompletionStage... cfs) { return create(CompletableFutureUtils.mostResultsOfSuccess( - defaultExecutor, timeout, unit, valueIfNotSuccess, cfs)); + valueIfNotSuccess, defaultExecutor, timeout, unit, cfs)); } //////////////////////////////////////////////////////////////////////////////// @@ -636,7 +636,7 @@ public Cffu> allTupleOfFastFail( * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new Cffu that is completed when the given two stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see Cffu#getSuccessNow(Object) */ @Contract(pure = true) @@ -654,7 +654,7 @@ public Cffu> mostTupleOfSuccess( * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new Cffu that is completed when the given three stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see Cffu#getSuccessNow(Object) */ @Contract(pure = true) @@ -673,7 +673,7 @@ public Cffu> mostTupleOfSuccess( * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new Cffu that is completed when the given four stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see Cffu#getSuccessNow(Object) */ @Contract(pure = true) @@ -693,7 +693,7 @@ public Cffu> mostTupleOfSuccess( * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new Cffu that is completed when the given five stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see Cffu#getSuccessNow(Object) */ @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 22e47c45..d2e62284 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -209,19 +209,19 @@ public static CompletableFuture> mSupplyFastFailAsync( * If the given supplier is successful in the given time, the return result is the completed value; * Otherwise the given valueIfNotSuccess. * + * @param valueIfNotSuccess the value to return if not completed successfully * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter - * @param valueIfNotSuccess the value to return if not completed successfully * @param suppliers the suppliers returning the value to be used to complete the returned CompletableFuture * @param the suppliers' return type * @return the new CompletableFuture - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see CompletableFuture#supplyAsync(Supplier) */ @SafeVarargs public static CompletableFuture> mSupplyMostSuccessAsync( - long timeout, TimeUnit unit, @Nullable T valueIfNotSuccess, Supplier... suppliers) { - return mSupplyMostSuccessAsync(AsyncPoolHolder.ASYNC_POOL, timeout, unit, valueIfNotSuccess, suppliers); + @Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, Supplier... suppliers) { + return mSupplyMostSuccessAsync(valueIfNotSuccess, AsyncPoolHolder.ASYNC_POOL, timeout, unit, suppliers); } /** @@ -233,25 +233,25 @@ public static CompletableFuture> mSupplyMostSuccessAsync( * If the given supplier is successful in the given time, the return result is the completed value; * Otherwise the given valueIfNotSuccess. * + * @param valueIfNotSuccess the value to return if not completed successfully * @param executor the executor to use for asynchronous execution * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter - * @param valueIfNotSuccess the value to return if not completed successfully * @param suppliers the suppliers returning the value to be used to complete the returned CompletableFuture * @param the suppliers' return type * @return the new CompletableFuture - * @see #mostResultsOfSuccess(Executor, long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, Executor, long, TimeUnit, CompletionStage[]) * @see CompletableFuture#supplyAsync(Supplier, Executor) */ @SafeVarargs public static CompletableFuture> mSupplyMostSuccessAsync( - Executor executor, long timeout, TimeUnit unit, - @Nullable T valueIfNotSuccess, Supplier... suppliers) { + @Nullable T valueIfNotSuccess, Executor executor, long timeout, TimeUnit unit, + Supplier... suppliers) { requireNonNull(executor, "executor is null"); requireNonNull(unit, "unit is null"); requireArrayAndEleNonNull("supplier", suppliers); - return mostResultsOfSuccess(executor, timeout, unit, valueIfNotSuccess, wrapSuppliers(executor, suppliers)); + return mostResultsOfSuccess(valueIfNotSuccess, executor, timeout, unit, wrapSuppliers(executor, suppliers)); } private static CompletableFuture[] wrapSuppliers( @@ -289,8 +289,8 @@ private static CompletableFuture[] wrapSuppliers( *

* If you need the successful results of given stages in the given time, prefer below methods: *

    - *
  • {@link #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])} - *
  • {@link #mostResultsOfSuccess(Executor, long, TimeUnit, Object, CompletionStage[])} + *
  • {@link #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])} + *
  • {@link #mostResultsOfSuccess(Object, Executor, long, TimeUnit, CompletionStage[])} *
  • {@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage)} *
  • {@link #mostTupleOfSuccess(Executor, long, TimeUnit, CompletionStage, CompletionStage)} *
  • {@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)} @@ -303,8 +303,8 @@ private static CompletableFuture[] wrapSuppliers( * @see #allResultsOf(CompletionStage[]) * @see #allTupleOf(CompletionStage, CompletionStage) * @see #allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) - * @see #mostResultsOfSuccess(Executor, long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, Executor, long, TimeUnit, CompletionStage[]) * @see #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage) * @see #mostTupleOfSuccess(Executor, long, TimeUnit, CompletionStage, CompletionStage) * @see #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) @@ -375,8 +375,8 @@ public static CompletableFuture> allResultsOf(CompletionStage * If you need the successful results of given stages in the given time, prefer below methods: *
      - *
    • {@link #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[])} - *
    • {@link #mostResultsOfSuccess(Executor, long, TimeUnit, Object, CompletionStage[])} + *
    • {@link #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[])} + *
    • {@link #mostResultsOfSuccess(Object, Executor, long, TimeUnit, CompletionStage[])} *
    • {@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage)} *
    • {@link #mostTupleOfSuccess(Executor, long, TimeUnit, CompletionStage, CompletionStage)} *
    • {@link #mostTupleOfSuccess(long, TimeUnit, CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage)} @@ -389,8 +389,8 @@ public static CompletableFuture> allResultsOf(CompletionStage CompletableFuture> allResultsOfFastFail(CompletionStag *

      * If the given stage is successful, its result is the completed value; Otherwise the given valueIfNotSuccess. * + * @param valueIfNotSuccess the value to return if not completed successfully * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter - * @param valueIfNotSuccess the value to return if not completed successfully * @param cfs the stages * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @SafeVarargs public static CompletableFuture> mostResultsOfSuccess( - long timeout, TimeUnit unit, @Nullable T valueIfNotSuccess, CompletionStage... cfs) { - return mostResultsOfSuccess(AsyncPoolHolder.ASYNC_POOL, timeout, unit, valueIfNotSuccess, cfs); + @Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, CompletionStage... cfs) { + return mostResultsOfSuccess(valueIfNotSuccess, AsyncPoolHolder.ASYNC_POOL, timeout, unit, cfs); } /** @@ -486,18 +486,18 @@ public static CompletableFuture> mostResultsOfSuccess( *

      * If the given stage is successful, its result is the completed value; Otherwise the given valueIfNotSuccess. * + * @param valueIfNotSuccess the value to return if not completed successfully * @param executorWhenTimeout the async executor when triggered by timeout * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter - * @param valueIfNotSuccess the value to return if not completed successfully * @param cfs the stages * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @SafeVarargs public static CompletableFuture> mostResultsOfSuccess( - Executor executorWhenTimeout, long timeout, TimeUnit unit, - @Nullable T valueIfNotSuccess, CompletionStage... cfs) { + @Nullable T valueIfNotSuccess, Executor executorWhenTimeout, long timeout, TimeUnit unit, + CompletionStage... cfs) { requireNonNull(executorWhenTimeout, "executorWhenTimeout is null"); requireNonNull(unit, "unit is null"); requireNonNull(cfs, "cfs is null"); @@ -900,7 +900,7 @@ private static T tupleOf0(Object... elements) { * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new CompletableFuture that is completed when the given two stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @@ -919,7 +919,7 @@ public static CompletableFuture> mostTupleOfSuccess( * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new CompletableFuture that is completed when the given two stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @@ -938,7 +938,7 @@ public static CompletableFuture> mostTupleOfSuccess( * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new CompletableFuture that is completed when the given three stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @@ -958,7 +958,7 @@ public static CompletableFuture> mostTupleOfSucc * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new CompletableFuture that is completed when the given three stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @@ -977,7 +977,7 @@ public static CompletableFuture> mostTupleOfSucc * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new CompletableFuture that is completed when the given four stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @@ -998,7 +998,7 @@ public static CompletableFuture> mostTup * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new CompletableFuture that is completed when the given four stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @@ -1018,7 +1018,7 @@ public static CompletableFuture> mostTup * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new CompletableFuture that is completed when the given five stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @@ -1039,7 +1039,7 @@ public static CompletableFuture> * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter * @return a new CompletableFuture that is completed when the given five stages complete - * @see #mostResultsOfSuccess(long, TimeUnit, Object, CompletionStage[]) + * @see #mostResultsOfSuccess(Object, long, TimeUnit, CompletionStage[]) * @see #getSuccessNow(CompletableFuture, Object) */ @Contract(pure = true) @@ -1300,18 +1300,18 @@ public static CompletableFuture> thenMApplyFastFailAsync( * If the given function is successful in the given time, the return result is the completed value; * Otherwise the given valueIfNotSuccess. * + * @param valueIfNotSuccess the value to return if not completed successfully * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter - * @param valueIfNotSuccess the value to return if not completed successfully * @param fns the functions to use to compute the values of the returned CompletableFuture * @param the functions' return type * @return the new CompletableFuture */ @SafeVarargs public static CompletableFuture> thenMApplyMostSuccessAsync( - CompletionStage cf, long timeout, TimeUnit unit, - @Nullable U valueIfNotSuccess, Function... fns) { - return thenMApplyMostSuccessAsync(cf, AsyncPoolHolder.ASYNC_POOL, timeout, unit, valueIfNotSuccess, fns); + CompletionStage cf, @Nullable U valueIfNotSuccess, + long timeout, TimeUnit unit, Function... fns) { + return thenMApplyMostSuccessAsync(cf, valueIfNotSuccess, AsyncPoolHolder.ASYNC_POOL, timeout, unit, fns); } /** @@ -1324,24 +1324,24 @@ public static CompletableFuture> thenMApplyMostSuccessAsync( * If the given function is successful in the given time, the return result is the completed value; * Otherwise the given valueIfNotSuccess. * + * @param valueIfNotSuccess the value to return if not completed successfully * @param executor the executor to use for asynchronous execution * @param timeout how long to wait in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter - * @param valueIfNotSuccess the value to return if not completed successfully * @param fns the functions to use to compute the values of the returned CompletableFuture * @param the functions' return type * @return the new CompletableFuture */ @SafeVarargs public static CompletableFuture> thenMApplyMostSuccessAsync( - CompletionStage cf, Executor executor, long timeout, TimeUnit unit, - @Nullable U valueIfNotSuccess, Function... fns) { + CompletionStage cf, @Nullable U valueIfNotSuccess, + Executor executor, long timeout, TimeUnit unit, Function... fns) { requireNonNull(executor, "executor is null"); requireNonNull(unit, "unit is null"); requireArrayAndEleNonNull("fn", fns); return toNonMinCf(cf).thenCompose(v -> mostResultsOfSuccess( - executor, timeout, unit, valueIfNotSuccess, wrapFunctions(executor, v, fns) + valueIfNotSuccess, executor, timeout, unit, wrapFunctions(executor, v, fns) )); } 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 cbb753f9..46bfbd4e 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -280,22 +280,22 @@ void test_mostOf() throws Exception { final Cffu cancelled = cffuFactory.toCffu(createCancelledFuture()); final Cffu incomplete = cffuFactory.toCffu(createIncompleteFuture()); - assertEquals(0, cffuFactory.mostResultsOfSuccess(10, TimeUnit.MILLISECONDS, null).get().size()); + assertEquals(0, cffuFactory.mostResultsOfSuccess(null, 10, TimeUnit.MILLISECONDS).get().size()); assertEquals(Arrays.asList(n, null, null, null), cffuFactory.mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, null, completed, failed, cancelled, incomplete + null, 10, TimeUnit.MILLISECONDS, completed, failed, cancelled, incomplete ).get()); assertEquals(Arrays.asList(n, anotherN, anotherN, anotherN), cffuFactory.mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherN, completedStage, failed, cancelled, incomplete + anotherN, 10, TimeUnit.MILLISECONDS, completedStage, failed, cancelled, incomplete ).get()); assertEquals(Arrays.asList(anotherN, anotherN, anotherN), cffuFactory.mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherN, failed, cancelled, incomplete + anotherN, 10, TimeUnit.MILLISECONDS, failed, cancelled, incomplete ).get()); // do not wait for failed and cancelled assertEquals(Arrays.asList(anotherN, anotherN), cffuFactory.mostResultsOfSuccess( - 10, TimeUnit.DAYS, anotherN, failed, cancelled + anotherN, 10, TimeUnit.DAYS, failed, cancelled ).get()); } 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 15f5cefd..2da0b91b 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CompletableFutureUtilsTest.java @@ -64,18 +64,14 @@ void test_mSupply() throws Exception { final long tick = System.currentTimeMillis(); - @SuppressWarnings("unused") - final CompletableFuture> listCompletableFuture = mSupplyMostSuccessAsync(200, TimeUnit.MILLISECONDS, supplier, supplier); - // FIXME unexpected type inference, move default value argument ahead before timeout??? - @SuppressWarnings("unchecked") CompletableFuture>[] cfs = new CompletableFuture[]{ mSupplyAsync(supplier, supplier), mSupplyAsync(executorService, supplier, supplier), mSupplyFastFailAsync(supplier, supplier), mSupplyFastFailAsync(executorService, supplier, supplier), - mSupplyMostSuccessAsync(500, TimeUnit.MILLISECONDS, anotherN, supplier, supplier), - mSupplyMostSuccessAsync(executorService, 500, TimeUnit.MILLISECONDS, anotherN, supplier, supplier), + mSupplyMostSuccessAsync(anotherN, 500, TimeUnit.MILLISECONDS, supplier, supplier), + mSupplyMostSuccessAsync(anotherN, executorService, 500, TimeUnit.MILLISECONDS, supplier, supplier), }; assertTrue(System.currentTimeMillis() - tick < 50); @@ -392,36 +388,36 @@ void test_mostOf() throws Exception { final CompletableFuture incomplete = createIncompleteFuture(); // 0 input cf - assertEquals(0, mostResultsOfSuccess(10, TimeUnit.MILLISECONDS, null).get().size()); + assertEquals(0, mostResultsOfSuccess(null, 10, TimeUnit.MILLISECONDS).get().size()); // 1 input cf assertEquals(Collections.singletonList(n), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, null, completed).get()); + null, 10, TimeUnit.MILLISECONDS, completed).get()); assertEquals(Collections.singletonList(n), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherN, completedStage).get()); + anotherN, 10, TimeUnit.MILLISECONDS, completedStage).get()); assertEquals(Collections.singletonList(anotherN), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherN, failed).get()); + anotherN, 10, TimeUnit.MILLISECONDS, failed).get()); assertEquals(Collections.singletonList(anotherN), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherN, cancelled).get()); + anotherN, 10, TimeUnit.MILLISECONDS, cancelled).get()); assertEquals(Collections.singletonList(anotherN), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherN, incomplete).get()); + anotherN, 10, TimeUnit.MILLISECONDS, incomplete).get()); // more input cf assertEquals(Arrays.asList(n, null, null, null), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, null, completed, failed, cancelled, incomplete + null, 10, TimeUnit.MILLISECONDS, completed, failed, cancelled, incomplete ).get()); assertEquals(Arrays.asList(n, anotherN, anotherN, anotherN), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherN, completedStage, failed, cancelled, incomplete + anotherN, 10, TimeUnit.MILLISECONDS, completedStage, failed, cancelled, incomplete ).get()); assertEquals(Arrays.asList(anotherN, anotherN, anotherN), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, anotherN, failed, cancelled, incomplete + anotherN, 10, TimeUnit.MILLISECONDS, failed, cancelled, incomplete ).get()); // do not wait for failed and cancelled assertEquals(Arrays.asList(anotherN, anotherN), mostResultsOfSuccess( - 10, TimeUnit.DAYS, anotherN, failed, cancelled + anotherN, 10, TimeUnit.DAYS, failed, cancelled ).get()); } @@ -431,10 +427,10 @@ void test_mostOf_wontModifyInputCf() throws Exception { final CompletableFuture incomplete2 = createIncompleteFuture(); assertEquals(Collections.singletonList(null), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, null, incomplete + null, 10, TimeUnit.MILLISECONDS, incomplete ).get()); assertEquals(Arrays.asList(null, null), mostResultsOfSuccess( - 10, TimeUnit.MILLISECONDS, null, incomplete, incomplete2 + null, 10, TimeUnit.MILLISECONDS, incomplete, incomplete2 ).get()); assertEquals(CffuState.RUNNING, state(incomplete)); @@ -869,10 +865,6 @@ void test_thenMApply() throws Exception { }; final CompletableFuture completed = completedFuture(n); - @SuppressWarnings("unused") - final CompletableFuture> listCompletableFuture = thenMApplyMostSuccessAsync(completed, 200, TimeUnit.MILLISECONDS, supplier, supplier); - // FIXME unexpected type inference, move default value argument ahead before timeout??? - final long tick = System.currentTimeMillis(); @SuppressWarnings("unchecked") CompletableFuture>[] cfs = new CompletableFuture[]{ @@ -880,8 +872,8 @@ void test_thenMApply() throws Exception { thenMApplyAsync(completed, executorService, supplier, supplier), thenMApplyFastFailAsync(completed, supplier, supplier), thenMApplyFastFailAsync(completed, executorService, supplier, supplier), - thenMApplyMostSuccessAsync(completed, 500, TimeUnit.MILLISECONDS, anotherN, supplier, supplier), - thenMApplyMostSuccessAsync(completed, executorService, 500, TimeUnit.MILLISECONDS, anotherN, supplier, supplier), + thenMApplyMostSuccessAsync(completed, anotherN, 500, TimeUnit.MILLISECONDS, supplier, supplier), + thenMApplyMostSuccessAsync(completed, anotherN, executorService, 500, TimeUnit.MILLISECONDS, supplier, supplier), }; assertTrue(System.currentTimeMillis() - tick < 50); 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 48561d0e..cf9a8b29 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 @@ -404,11 +404,11 @@ fun Array>.allOfFastFailCffu(cffuFactory: CffuFactory): C * @see Cffu.getSuccessNow */ fun Collection>.mostResultsOfSuccessCffu( - timeout: Long, unit: TimeUnit, valueIfNotSuccess: T, cffuFactory: CffuFactory = ABSENT + valueIfNotSuccess: T, timeout: Long, unit: TimeUnit, cffuFactory: CffuFactory = ABSENT ): Cffu> { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_COLL) - return factory.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, *toTypedArray()) + return factory.mostResultsOfSuccess(valueIfNotSuccess, timeout, unit, *toTypedArray()) } /** @@ -425,11 +425,11 @@ fun Collection>.mostResultsOfSuccessCffu( * @see Cffu.getSuccessNow */ fun Array>.mostResultsOfSuccessCffu( - timeout: Long, unit: TimeUnit, valueIfNotSuccess: T, cffuFactory: CffuFactory = ABSENT + valueIfNotSuccess: T, timeout: Long, unit: TimeUnit, cffuFactory: CffuFactory = ABSENT ): Cffu> { val factory: CffuFactory = if (cffuFactory !== ABSENT) cffuFactory else firstOrNull()?.cffuFactory() ?: throw IllegalArgumentException(ERROR_MSG_FOR_ARRAY) - return factory.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, *this) + return factory.mostResultsOfSuccess(valueIfNotSuccess, timeout, unit, *this) } /** @@ -447,9 +447,9 @@ fun Array>.mostResultsOfSuccessCffu( */ @JvmName("mostResultsOfSuccessCffuCs") fun Collection>.mostResultsOfSuccessCffu( - timeout: Long, unit: TimeUnit, valueIfNotSuccess: T, cffuFactory: CffuFactory + valueIfNotSuccess: T, timeout: Long, unit: TimeUnit, cffuFactory: CffuFactory ): Cffu> = - cffuFactory.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, *toTypedArray()) + cffuFactory.mostResultsOfSuccess(valueIfNotSuccess, timeout, unit, *toTypedArray()) /** * Returns a new Cffu with the most results in the **same order** of @@ -466,9 +466,9 @@ fun Collection>.mostResultsOfSuccessCffu( * @see Cffu.getSuccessNow */ fun Array>.mostResultsOfSuccessCffu( - timeout: Long, unit: TimeUnit, valueIfNotSuccess: T, cffuFactory: CffuFactory + valueIfNotSuccess: T, timeout: Long, unit: TimeUnit, cffuFactory: CffuFactory ): Cffu> = - cffuFactory.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, *this) + cffuFactory.mostResultsOfSuccess(valueIfNotSuccess, timeout, unit, *this) //////////////////////////////////////// //# anyOf* methods for Array/Collection 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 071fd3a7..a5e3b8ff 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 @@ -190,9 +190,9 @@ fun Array>.allResultsOfFastFailCompletableFuture( * @see getSuccessNow */ fun Collection>.mostResultsOfSuccessCompletableFuture( - timeout: Long, unit: TimeUnit, valueIfNotSuccess: T + valueIfNotSuccess: T, timeout: Long, unit: TimeUnit ): CompletableFuture> = - CompletableFutureUtils.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, *toTypedArray()) + CompletableFutureUtils.mostResultsOfSuccess(valueIfNotSuccess, timeout, unit, *toTypedArray()) /** * Returns a new CompletableFuture with the most results in the **same order** of @@ -206,9 +206,9 @@ fun Collection>.mostResultsOfSuccessCompletableFuture * @see getSuccessNow */ fun Array>.mostResultsOfSuccessCompletableFuture( - timeout: Long, unit: TimeUnit, valueIfNotSuccess: T + valueIfNotSuccess: T, timeout: Long, unit: TimeUnit ): CompletableFuture> = - CompletableFutureUtils.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, *this) + CompletableFutureUtils.mostResultsOfSuccess(valueIfNotSuccess, timeout, unit, *this) /** * Returns a new CompletableFuture with the most results in the **same order** of @@ -223,10 +223,10 @@ fun Array>.mostResultsOfSuccessCompletableFuture( * @see getSuccessNow */ fun Collection>.mostResultsOfSuccessCompletableFuture( - executorWhenTimeout: Executor, timeout: Long, unit: TimeUnit, valueIfNotSuccess: T + valueIfNotSuccess: T, executorWhenTimeout: Executor, timeout: Long, unit: TimeUnit ): CompletableFuture> = CompletableFutureUtils.mostResultsOfSuccess( - executorWhenTimeout, timeout, unit, valueIfNotSuccess, *toTypedArray() + valueIfNotSuccess, executorWhenTimeout, timeout, unit, *toTypedArray() ) /** @@ -242,9 +242,9 @@ fun Collection>.mostResultsOfSuccessCompletableFuture * @see getSuccessNow */ fun Array>.mostResultsOfSuccessCompletableFuture( - executorWhenTimeout: Executor, timeout: Long, unit: TimeUnit, valueIfNotSuccess: T + valueIfNotSuccess: T, executorWhenTimeout: Executor, timeout: Long, unit: TimeUnit ): CompletableFuture> = - CompletableFutureUtils.mostResultsOfSuccess(executorWhenTimeout, timeout, unit, valueIfNotSuccess, *this) + CompletableFutureUtils.mostResultsOfSuccess(valueIfNotSuccess, executorWhenTimeout, timeout, unit, *this) //////////////////////////////////////// //# anyOf* methods for Array/Collection 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 98cb623c..d1f1c89c 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 @@ -303,39 +303,39 @@ class CffuExtensionsTest : FunSpec({ listOf( testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), - ).mostResultsOfSuccessCffu(10, TimeUnit.MILLISECONDS, -1, testCffuFactory).await() shouldBe listOf(-1, 42) + ).mostResultsOfSuccessCffu(-1, 10, TimeUnit.MILLISECONDS, testCffuFactory).await() shouldBe listOf(-1, 42) setOf( testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), - ).mostResultsOfSuccessCffu(10, TimeUnit.MILLISECONDS, -1).await() shouldBe listOf(-1, 42) + ).mostResultsOfSuccessCffu(-1, 10, TimeUnit.MILLISECONDS).await() shouldBe listOf(-1, 42) listOf( CompletableFuture(), CompletableFuture.completedFuture(42), - ).mostResultsOfSuccessCffu(10, TimeUnit.MILLISECONDS, -1, testCffuFactory).await() shouldBe listOf(-1, 42) + ).mostResultsOfSuccessCffu(-1, 10, TimeUnit.MILLISECONDS, testCffuFactory).await() shouldBe listOf(-1, 42) // Array arrayOf( testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), - ).mostResultsOfSuccessCffu(10, TimeUnit.MILLISECONDS, -1, testCffuFactory).await() shouldBe listOf(-1, 42) + ).mostResultsOfSuccessCffu(-1, 10, TimeUnit.MILLISECONDS, testCffuFactory).await() shouldBe listOf(-1, 42) arrayOf( testCffuFactory.newIncompleteCffu(), testCffuFactory.completedFuture(42), - ).mostResultsOfSuccessCffu(10, TimeUnit.MILLISECONDS, -1).await() shouldBe listOf(-1, 42) + ).mostResultsOfSuccessCffu(-1, 10, TimeUnit.MILLISECONDS).await() shouldBe listOf(-1, 42) arrayOf( CompletableFuture(), CompletableFuture.completedFuture(42), - ).mostResultsOfSuccessCffu(10, TimeUnit.MILLISECONDS, -1, testCffuFactory).await() shouldBe listOf(-1, 42) + ).mostResultsOfSuccessCffu(-1, 10, TimeUnit.MILLISECONDS, testCffuFactory).await() shouldBe listOf(-1, 42) // FIXME: java.lang.ClassCastException if not providing the type parameter explicitly: // class [Ljava.lang.Object; cannot be cast to class [Ljava.util.concurrent.CompletionStage; arrayOf>(CompletableFuture(), testCffuFactory.completedFuture(42)) - .mostResultsOfSuccessCffu(10, TimeUnit.MILLISECONDS, -1, testCffuFactory).await() shouldBe listOf(-1, 42) + .mostResultsOfSuccessCffu(-1, 10, TimeUnit.MILLISECONDS, testCffuFactory).await() shouldBe listOf(-1, 42) } test("anyOf*") { @@ -603,10 +603,10 @@ class CffuExtensionsTest : FunSpec({ assertEmptyArray { emptyArray.allOfFastFailCffu() } assertCffuFactoryForOptional(array.allOfFastFailCffu()) - assertEmptyCollection { emptyList.mostResultsOfSuccessCffu(1, TimeUnit.MILLISECONDS, 4) } - assertCffuFactoryForOptional(list.mostResultsOfSuccessCffu(1, TimeUnit.MILLISECONDS, 4)) - assertEmptyArray { emptyArray.mostResultsOfSuccessCffu(1, TimeUnit.MILLISECONDS, 4) } - assertCffuFactoryForOptional(array.mostResultsOfSuccessCffu(1, TimeUnit.MILLISECONDS, 4)) + assertEmptyCollection { emptyList.mostResultsOfSuccessCffu(4, 1, TimeUnit.MILLISECONDS) } + assertCffuFactoryForOptional(list.mostResultsOfSuccessCffu(4, 1, TimeUnit.MILLISECONDS)) + assertEmptyArray { emptyArray.mostResultsOfSuccessCffu(4, 1, TimeUnit.MILLISECONDS) } + assertCffuFactoryForOptional(array.mostResultsOfSuccessCffu(4, 1, TimeUnit.MILLISECONDS)) assertEmptyCollection { emptyList.anyOfCffu() } assertCffuFactoryForOptional(list.anyOfCffu()) 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 13ccaf36..60c9776d 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 @@ -114,15 +114,15 @@ class CompletableFutureExtensionsTest : FunSpec({ test("mostResultsOfSuccessCompletableFuture") { listOf(CompletableFuture(), CompletableFuture.completedFuture(42)) - .mostResultsOfSuccessCompletableFuture(10, TimeUnit.MILLISECONDS, null).await() shouldBe listOf(null, 42) + .mostResultsOfSuccessCompletableFuture(null, 10, TimeUnit.MILLISECONDS).await() shouldBe listOf(null, 42) listOf(CompletableFuture(), CompletableFuture.completedFuture(42)) - .mostResultsOfSuccessCompletableFuture(testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, null) + .mostResultsOfSuccessCompletableFuture(null, testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS) .await() shouldBe listOf(null, 42) arrayOf(CompletableFuture(), CompletableFuture.completedFuture(42)) - .mostResultsOfSuccessCompletableFuture(10, TimeUnit.MILLISECONDS, null).await() shouldBe listOf(null, 42) + .mostResultsOfSuccessCompletableFuture(null, 10, TimeUnit.MILLISECONDS).await() shouldBe listOf(null, 42) arrayOf(CompletableFuture(), CompletableFuture.completedFuture(42)) - .mostResultsOfSuccessCompletableFuture(testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS, null) + .mostResultsOfSuccessCompletableFuture(null, testThreadPoolExecutor, 10, TimeUnit.MILLISECONDS) .await() shouldBe listOf(null, 42) }