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 5b9791b2..6371311e 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java +++ b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java @@ -1882,12 +1882,12 @@ public Cffu resetCffuFactory(CffuFactory cffuFactory) { * {@code underlyingCf.toCompletableFuture()}; if you need the underlying CompletableFuture instance, * call method {@link #cffuUnwrap()}. *

- * {@link CffuFactory#toCompletableFutureArray(CompletionStage[])} is the batch operation to this method. + * {@link CompletableFutureUtils#toCompletableFutureArray(CompletionStage[])} is the batch operation to this method. * * @return the CompletableFuture * @see CompletionStage#toCompletableFuture() * @see #cffuUnwrap() - * @see CffuFactory#toCompletableFutureArray(CompletionStage[]) + * @see CompletableFutureUtils#toCompletableFutureArray(CompletionStage[]) */ @Contract(pure = true) @Override 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 d6ea3d4a..c8f603eb 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -19,6 +19,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray; import static java.util.Objects.requireNonNull; @@ -35,7 +36,7 @@ *

  • factory methods return {@link Cffu} instead of {@link CompletableFuture}. *
  • only provide varargs methods for multiply Cffu/CF input arguments; * if you have {@code List} input, use static util methods {@link #cffuListToArray(List)} - * or {@link #completableFutureListToArray(List)} to convert it to array first. + * or {@link CompletableFutureUtils#completableFutureListToArray(List)} to convert it to array first. * * * @author Jerry Lee (oldratlee at gmail dot com) @@ -621,34 +622,10 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) { //////////////////////////////////////////////////////////////////////////////// //# Conversion (Static) Methods // - // - toCompletableFutureArray: Cffu -> CF - // - cffuArrayUnwrap: Cffu -> CF - // // - cffuListToArray: List -> Cffu[] // - completableFutureListToArray: List -> CF[] //////////////////////////////////////////////////////////////////////////////// - /** - * A convenient util method for converting input {@link Cffu} / {@link CompletableFuture} / {@link CompletionStage} - * array element by {@link Cffu#toCompletableFuture()} / {@link CompletableFuture#toCompletableFuture()} / - * {@link CompletionStage#toCompletableFuture()}. - * - * @see Cffu#toCompletableFuture() - * @see CompletableFuture#toCompletableFuture() - * @see CompletionStage#toCompletableFuture() - * @see #toCffuArray(CompletionStage[]) - */ - @Contract(pure = true) - @SafeVarargs - public static CompletableFuture[] toCompletableFutureArray(CompletionStage... stages) { - @SuppressWarnings("unchecked") - CompletableFuture[] ret = new CompletableFuture[stages.length]; - for (int i = 0; i < stages.length; i++) { - ret[i] = requireNonNull(stages[i], "stage" + (i + 1) + " is null").toCompletableFuture(); - } - return ret; - } - /** * A convenient util method for unwrap input {@link Cffu} array elements by {@link Cffu#cffuUnwrap()}. * @@ -675,15 +652,6 @@ public static Cffu[] cffuListToArray(List> cffuList) { return cffuList.toArray(new Cffu[0]); } - /** - * Convert CompletableFuture list to CompletableFuture array. - */ - @Contract(pure = true) - @SuppressWarnings("unchecked") - public static CompletableFuture[] completableFutureListToArray(List> cfList) { - return cfList.toArray(new CompletableFuture[0]); - } - //////////////////////////////////////////////////////////////////////////////// //# Getter methods of CffuFactory properties //////////////////////////////////////////////////////////////////////////////// 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 c9bb4ccf..207819df 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -16,7 +16,6 @@ import java.util.concurrent.*; import java.util.function.*; -import static io.foldright.cffu.CffuFactory.toCompletableFutureArray; import static java.util.Objects.requireNonNull; import static java.util.function.Function.identity; @@ -60,8 +59,7 @@ public static CompletableFuture> allResultsOf(CompletionStage[] resultSetterCfs = createResultSetterCfs(cfs, result); - return CompletableFuture.allOf(resultSetterCfs) - .thenApply(unused -> (List) arrayList(result)); + return CompletableFuture.allOf(resultSetterCfs).thenApply(unused -> (List) arrayList(result)); } /** @@ -218,7 +216,7 @@ private static void fill(CompletionStage[] cfs, @SafeVarargs @SuppressWarnings({"unchecked", "rawtypes"}) public static CompletableFuture anyOf(CompletionStage... cfs) { - return (CompletableFuture) CompletableFuture.anyOf(toCompletableFutureArray((CompletionStage[]) cfs)); + return (CompletableFuture) CompletableFuture.anyOf(toCompletableFutureArray((CompletionStage[]) cfs)); } /** @@ -282,9 +280,7 @@ public static CompletableFuture> allTupleOf( final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return CompletableFuture.allOf(resultSetterCfs).thenApply(unused -> - Tuple2.of((T1) result[0], (T2) result[1]) - ); + return CompletableFuture.allOf(resultSetterCfs).thenApply(unused -> Tuple2.of((T1) result[0], (T2) result[1])); } /** @@ -310,9 +306,7 @@ public static CompletableFuture> allTupleOfFastFail( final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return allOfFastFail(resultSetterCfs).thenApply(unused -> - Tuple2.of((T1) result[0], (T2) result[1]) - ); + return allOfFastFail(resultSetterCfs).thenApply(unused -> Tuple2.of((T1) result[0], (T2) result[1])); } /** @@ -334,9 +328,8 @@ public static CompletableFuture> allTupleOf( final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return CompletableFuture.allOf(resultSetterCfs).thenApply(unused -> - Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2]) - ); + return CompletableFuture.allOf(resultSetterCfs) + .thenApply(unused -> Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2])); } /** @@ -362,9 +355,8 @@ public static CompletableFuture> allTupleOfFastF final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return allOfFastFail(resultSetterCfs).thenApply(unused -> - Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2]) - ); + return allOfFastFail(resultSetterCfs) + .thenApply(unused -> Tuple3.of((T1) result[0], (T2) result[1], (T3) result[2])); } /** @@ -387,9 +379,8 @@ public static CompletableFuture> allTupl final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return CompletableFuture.allOf(resultSetterCfs).thenApply(unused -> - Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3]) - ); + return CompletableFuture.allOf(resultSetterCfs) + .thenApply(unused -> Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3])); } /** @@ -416,9 +407,8 @@ public static CompletableFuture> allTupl final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return allOfFastFail(resultSetterCfs).thenApply(unused -> - Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3]) - ); + return allOfFastFail(resultSetterCfs) + .thenApply(unused -> Tuple4.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3])); } /** @@ -442,8 +432,7 @@ public static CompletableFuture> final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); return CompletableFuture.allOf(resultSetterCfs).thenApply(unused -> - Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4]) - ); + Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4])); } /** @@ -471,8 +460,7 @@ public static CompletableFuture> final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); return allOfFastFail(resultSetterCfs).thenApply(unused -> - Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4]) - ); + Tuple5.of((T1) result[0], (T2) result[1], (T3) result[2], (T4) result[3], (T5) result[4])); } //////////////////////////////////////////////////////////////////////////////// @@ -573,8 +561,7 @@ public static CompletableFuture thenAcceptBothFastFail( final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return allOfFastFail(resultSetterCfs) - .thenAccept(unused -> action.accept((T) result[0], (U) result[1])); + return allOfFastFail(resultSetterCfs).thenAccept(unused -> action.accept((T) result[0], (U) result[1])); } /** @@ -602,8 +589,7 @@ public static CompletableFuture thenAcceptBothFastFailAsync( final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return allOfFastFail(resultSetterCfs) - .thenAcceptAsync(unused -> action.accept((T) result[0], (U) result[1])); + return allOfFastFail(resultSetterCfs).thenAcceptAsync(unused -> action.accept((T) result[0], (U) result[1])); } /** @@ -660,8 +646,7 @@ public static CompletableFuture thenCombineFastFail( final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return allOfFastFail(resultSetterCfs) - .thenApply(unused -> fn.apply((T) result[0], (U) result[1])); + return allOfFastFail(resultSetterCfs).thenApply(unused -> fn.apply((T) result[0], (U) result[1])); } /** @@ -690,8 +675,7 @@ public static CompletableFuture thenCombineFastFailAsync( final Object[] result = new Object[css.length]; final CompletableFuture[] resultSetterCfs = createResultSetterCfs(css, result); - return allOfFastFail(resultSetterCfs) - .thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1])); + return allOfFastFail(resultSetterCfs).thenApplyAsync(unused -> fn.apply((T) result[0], (U) result[1])); } /** @@ -1428,7 +1412,6 @@ public static CompletableFuture completeAsync( if (IS_JAVA9_PLUS) { return cf.completeAsync(supplier, executor); } - requireNonNull(supplier, "supplier is null"); requireNonNull(executor, "executor is null"); // below code is copied from CompletableFuture#completeAsync with small adoption @@ -1504,6 +1487,43 @@ public static Executor defaultExecutor() { return AsyncPoolHolder.ASYNC_POOL; } + //////////////////////////////////////////////////////////////////////////////// + //# Conversion (Static) Methods + // + // - toCompletableFutureArray: Cffu -> CF + // - cffuArrayUnwrap: Cffu -> CF + //////////////////////////////////////////////////////////////////////////////// + + /** + * A convenient util method for converting input {@link Cffu} / {@link CompletableFuture} / {@link CompletionStage} + * array element by {@link Cffu#toCompletableFuture()} / {@link CompletableFuture#toCompletableFuture()} / + * {@link CompletionStage#toCompletableFuture()}. + * + * @see Cffu#toCompletableFuture() + * @see CompletableFuture#toCompletableFuture() + * @see CompletionStage#toCompletableFuture() + * @see CffuFactory#toCffuArray(CompletionStage[]) + */ + @Contract(pure = true) + @SafeVarargs + public static CompletableFuture[] toCompletableFutureArray(CompletionStage... stages) { + @SuppressWarnings("unchecked") + CompletableFuture[] ret = new CompletableFuture[stages.length]; + for (int i = 0; i < stages.length; i++) { + ret[i] = requireNonNull(stages[i], "stage" + (i + 1) + " is null").toCompletableFuture(); + } + return ret; + } + + /** + * Convert CompletableFuture list to CompletableFuture array. + */ + @Contract(pure = true) + @SuppressWarnings("unchecked") + public static CompletableFuture[] completableFutureListToArray(List> cfList) { + return cfList.toArray(new CompletableFuture[0]); + } + //////////////////////////////////////////////////////////////////////////////// //# Helper fields and classes //////////////////////////////////////////////////////////////////////////////// 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 10da42e3..5e765860 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -17,6 +17,7 @@ import static io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder; import static io.foldright.cffu.CompletableFutureUtils.failedFuture; +import static io.foldright.cffu.CompletableFutureUtils.toCompletableFutureArray; import static io.foldright.test_utils.TestUtils.*; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.ForkJoinPool.commonPool; @@ -737,9 +738,9 @@ void test_toCompletableFutureArray() { cffuFactory.toCffu(cfArray[1]), }; - assertArrayEquals(cfArray, CffuFactory.toCompletableFutureArray(cfArray)); - assertArrayEquals(cfArray, CffuFactory.toCompletableFutureArray(csArray)); - assertArrayEquals(cfArray, CffuFactory.toCompletableFutureArray(cffuArray)); + assertArrayEquals(cfArray, toCompletableFutureArray(cfArray)); + assertArrayEquals(cfArray, toCompletableFutureArray(csArray)); + assertArrayEquals(cfArray, toCompletableFutureArray(cffuArray)); } @Test @@ -774,7 +775,7 @@ void test_completableFutureListToArray() { @SuppressWarnings("unchecked") CompletableFuture[] input = new CompletableFuture[]{completedFuture(n), completedFuture(anotherN)}; - assertArrayEquals(input, CffuFactory.completableFutureListToArray(Arrays.asList(input))); + assertArrayEquals(input, CompletableFutureUtils.completableFutureListToArray(Arrays.asList(input))); } //////////////////////////////////////////////////////////////////////////////// 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 6af0b40b..acc41499 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 @@ -3,6 +3,7 @@ package io.foldright.cffu.kotlin import io.foldright.cffu.Cffu import io.foldright.cffu.CffuFactory import io.foldright.cffu.CffuFactoryBuilder.newCffuFactoryBuilder +import io.foldright.cffu.CompletableFutureUtils import java.util.concurrent.CompletableFuture import java.util.concurrent.CompletionStage @@ -536,7 +537,7 @@ fun Array>.anyOfSuccessCffu(cffuFactory: CffuFa * * This method is the same as [CffuFactory.anyOf], providing this method is convenient for method chaining. * - * @see CffuFactory.toCompletableFutureArray + * @see CompletableFutureUtils.toCompletableFutureArray */ fun Collection>.toCompletableFuture(): List> = map { it.toCompletableFuture() } @@ -546,10 +547,10 @@ fun Collection>.toCompletableFuture(): List Array>.toCompletableFuture(): Array> = - CffuFactory.toCompletableFutureArray(*this) + CompletableFutureUtils.toCompletableFutureArray(*this) //////////////////////////////////////// // cffuUnwrap methods