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 eaf69610..64291c0d 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -56,16 +56,6 @@ public final class CffuFactory { this.forbidObtrudeMethods = forbidObtrudeMethods; } - @Contract(pure = true) - private Cffu new0(CompletableFuture cf) { - return new Cffu<>(this, false, cf); - } - - @Contract(pure = true) - private Cffu newMin(CompletableFuture cf) { - return new Cffu<>(this, true, cf); - } - /** * Returns a {@link CffuFactoryBuilder} with {@code defaultExecutor} setting. * @@ -77,6 +67,16 @@ public static CffuFactoryBuilder builder(Executor defaultExecutor) { return new CffuFactoryBuilder(CompletableFutureUtils.screenExecutor(defaultExecutor)); } + @Contract(pure = true) + private Cffu create(CompletableFuture cf) { + return new Cffu<>(this, false, cf); + } + + @Contract(pure = true) + private Cffu createMin(CompletableFuture cf) { + return new Cffu<>(this, true, cf); + } + //////////////////////////////////////////////////////////////////////////////// //# Factory Methods, equivalent to same name static methods of CompletableFuture // @@ -95,7 +95,7 @@ public static CffuFactoryBuilder builder(Executor defaultExecutor) { */ @Contract(pure = true) public Cffu completedFuture(@Nullable T value) { - return new0(CompletableFuture.completedFuture(value)); + return create(CompletableFuture.completedFuture(value)); } /** @@ -114,7 +114,7 @@ public Cffu completedFuture(@Nullable T value) { */ @Contract(pure = true) public CompletionStage completedStage(@Nullable T value) { - return newMin((CompletableFuture) CompletableFutureUtils.completedStage(value)); + return createMin((CompletableFuture) CompletableFutureUtils.completedStage(value)); } /** @@ -127,7 +127,7 @@ public CompletionStage completedStage(@Nullable T value) { */ @Contract(pure = true) public Cffu failedFuture(Throwable ex) { - return new0(CompletableFutureUtils.failedFuture(ex)); + return create(CompletableFutureUtils.failedFuture(ex)); } /** @@ -146,7 +146,7 @@ public Cffu failedFuture(Throwable ex) { */ @Contract(pure = true) public CompletionStage failedStage(Throwable ex) { - return newMin((CompletableFuture) CompletableFutureUtils.failedStage(ex)); + return createMin((CompletableFuture) CompletableFutureUtils.failedStage(ex)); } //////////////////////////////////////////////////////////////////////////////// @@ -179,7 +179,7 @@ public Cffu runAsync(Runnable action) { * @see CompletableFuture#runAsync(Runnable, Executor) */ public Cffu runAsync(Runnable action, Executor executor) { - return new0(CompletableFuture.runAsync(action, executor)); + return create(CompletableFuture.runAsync(action, executor)); } /** @@ -211,7 +211,7 @@ public Cffu supplyAsync(Supplier supplier) { @CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `runAsync`") @SuppressWarnings("BoundedWildcard") public Cffu supplyAsync(Supplier supplier, Executor executor) { - return new0(CompletableFuture.supplyAsync(supplier, executor)); + return create(CompletableFuture.supplyAsync(supplier, executor)); } //////////////////////////////////////////////////////////////////////////////// @@ -241,7 +241,7 @@ public Cffu supplyAsync(Supplier supplier, Executor executor) { */ @Contract(pure = true) public Cffu newIncompleteCffu() { - return new0(new CompletableFuture<>()); + return create(new CompletableFuture<>()); } /** @@ -268,13 +268,13 @@ public Cffu toCffu(CompletionStage stage) { requireNonNull(stage, "stage is null"); if (CompletableFutureUtils.isMinimalStageCf(stage)) { - return newMin((CompletableFuture) stage); + return createMin((CompletableFuture) stage); } else if (stage instanceof CompletableFuture) { - return new0((CompletableFuture) stage); + return create((CompletableFuture) stage); } else if (stage instanceof Cffu) { return ((Cffu) stage).resetCffuFactory(this); } - return new0(stage.toCompletableFuture()); + return create(stage.toCompletableFuture()); } /** @@ -335,7 +335,7 @@ public final Cffu[] toCffuArray(CompletionStage... stages) { */ @Contract(pure = true) public Cffu allOf(CompletionStage... cfs) { - return new0(CompletableFutureUtils.allOf(cfs)); + return create(CompletableFutureUtils.allOf(cfs)); } /** @@ -357,7 +357,7 @@ public Cffu allOf(CompletionStage... cfs) { @Contract(pure = true) @SafeVarargs public final Cffu> allResultsOf(CompletionStage... cfs) { - return new0(CompletableFutureUtils.allResultsOf(cfs)); + return create(CompletableFutureUtils.allResultsOf(cfs)); } /** @@ -390,7 +390,7 @@ public final Cffu> allResultsOf(CompletionStage... cfs) */ @Contract(pure = true) public Cffu allOfFastFail(CompletionStage... cfs) { - return new0(CompletableFutureUtils.allOfFastFail(cfs)); + return create(CompletableFutureUtils.allOfFastFail(cfs)); } /** @@ -415,7 +415,7 @@ public Cffu allOfFastFail(CompletionStage... cfs) { @Contract(pure = true) @SafeVarargs public final Cffu> allResultsOfFastFail(CompletionStage... cfs) { - return new0(CompletableFutureUtils.allResultsOfFastFail(cfs)); + return create(CompletableFutureUtils.allResultsOfFastFail(cfs)); } /** @@ -434,7 +434,7 @@ public final Cffu> allResultsOfFastFail(CompletionStage @SafeVarargs public final Cffu> mostResultsOfSuccess( long timeout, TimeUnit unit, @Nullable T valueIfNotSuccess, CompletionStage... cfs) { - return new0(CompletableFutureUtils.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, cfs)); + return create(CompletableFutureUtils.mostResultsOfSuccess(timeout, unit, valueIfNotSuccess, cfs)); } //////////////////////////////////////////////////////////////////////////////// @@ -464,7 +464,7 @@ public final Cffu> mostResultsOfSuccess( @Contract(pure = true) @SafeVarargs public final Cffu anyOf(CompletionStage... cfs) { - return new0(CompletableFutureUtils.anyOf(cfs)); + return create(CompletableFutureUtils.anyOf(cfs)); } /** @@ -485,7 +485,7 @@ public final Cffu anyOf(CompletionStage... cfs) { */ @SafeVarargs public final Cffu anyOfSuccess(CompletionStage... cfs) { - return new0(CompletableFutureUtils.anyOfSuccess(cfs)); + return create(CompletableFutureUtils.anyOfSuccess(cfs)); } //////////////////////////////////////////////////////////////////////////////// @@ -508,7 +508,7 @@ public final Cffu anyOfSuccess(CompletionStage... cfs) { @Contract(pure = true) public Cffu> allTupleOf( CompletionStage cf1, CompletionStage cf2) { - return new0(CompletableFutureUtils.allTupleOf(cf1, cf2)); + return create(CompletableFutureUtils.allTupleOf(cf1, cf2)); } /** @@ -526,7 +526,7 @@ public Cffu> allTupleOf( @Contract(pure = true) public Cffu> allTupleOfFastFail( CompletionStage cf1, CompletionStage cf2) { - return new0(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2)); + return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2)); } /** @@ -541,7 +541,7 @@ public Cffu> allTupleOfFastFail( @Contract(pure = true) public Cffu> allTupleOf( CompletionStage cf1, CompletionStage cf2, CompletionStage cf3) { - return new0(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3)); + return create(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3)); } /** @@ -559,7 +559,7 @@ public Cffu> allTupleOf( @Contract(pure = true) public Cffu> allTupleOfFastFail( CompletionStage cf1, CompletionStage cf2, CompletionStage cf3) { - return new0(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3)); + return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3)); } /** @@ -575,7 +575,7 @@ public Cffu> allTupleOfFastFail( public Cffu> allTupleOf( CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4) { - return new0(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3, cf4)); + return create(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3, cf4)); } /** @@ -594,7 +594,7 @@ public Cffu> allTupleOf( public Cffu> allTupleOfFastFail( CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4) { - return new0(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4)); + return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4)); } /** @@ -610,7 +610,7 @@ public Cffu> allTupleOfFastFail( public Cffu> allTupleOf( CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4, CompletionStage cf5) { - return new0(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3, cf4, cf5)); + return create(CompletableFutureUtils.allTupleOf(cf1, cf2, cf3, cf4, cf5)); } /** @@ -629,7 +629,7 @@ public Cffu> allTupleOf( public Cffu> allTupleOfFastFail( CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4, CompletionStage cf5) { - return new0(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4, cf5)); + return create(CompletableFutureUtils.allTupleOfFastFail(cf1, cf2, cf3, cf4, cf5)); } /** @@ -647,7 +647,7 @@ public Cffu> allTupleOfFastFail( @Contract(pure = true) public Cffu> mostTupleOfSuccess( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2) { - return new0(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2)); + return create(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2)); } /** @@ -666,7 +666,7 @@ public Cffu> mostTupleOfSuccess( public Cffu> mostTupleOfSuccess( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3) { - return new0(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3)); + return create(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3)); } /** @@ -686,7 +686,7 @@ public Cffu> mostTupleOfSuccess( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4) { - return new0(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3, cf4)); + return create(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3, cf4)); } /** @@ -706,7 +706,7 @@ public Cffu> mostTupleOfSuccess( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4, CompletionStage cf5) { - return new0(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3, cf4, cf5)); + return create(CompletableFutureUtils.mostTupleOfSuccess(timeout, unit, cf1, cf2, cf3, cf4, cf5)); } //////////////////////////////////////////////////////////////////////////////// @@ -721,8 +721,7 @@ public Cffu> mostTupleOfSuccess( * Each delay commences upon invocation of the returned executor's {@code execute} method. * * @param delay how long to delay, in units of {@code unit} - * @param unit a {@code TimeUnit} determining how to interpret the - * {@code delay} parameter + * @param unit a {@code TimeUnit} determining how to interpret the {@code delay} parameter * @return the new delayed executor */ @Contract(pure = true) @@ -736,8 +735,7 @@ public Executor delayedExecutor(long delay, TimeUnit unit) { * Each delay commences upon invocation of the returned executor's {@code execute} method. * * @param delay how long to delay, in units of {@code unit} - * @param unit a {@code TimeUnit} determining how to interpret the - * {@code delay} parameter + * @param unit a {@code TimeUnit} determining how to interpret the {@code delay} parameter * @param executor the base executor * @return the new delayed executor */ @@ -749,8 +747,8 @@ public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) { //////////////////////////////////////////////////////////////////////////////// //# Conversion (Static) Methods // - // - cffuListToArray: List -> Cffu[] - // - completableFutureListToArray: List -> CF[] + // - cffuArrayUnwrap: Cffu[] -> CompletableFuture[] + // - cffuListToArray: List -> Cffu[] //////////////////////////////////////////////////////////////////////////////// /** 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 6fffa6ec..2d1a0f40 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -737,34 +737,21 @@ void test_allTupleOfFastFail_exceptionally() throws Exception { //////////////////////////////////////////////////////////////////////////////// //# Conversion (Static) Methods // - // - toCompletableFutureArray: CompletionStage[](including Cffu) -> CF[] - // - completableFutureListToArray: List -> CF[] - // - cffuArrayUnwrap: Cffu -> CF // - cffuListToArray: List -> Cffu[] + // - toCompletableFutureArray: CompletionStage[](including Cffu) -> CF[] //////////////////////////////////////////////////////////////////////////////// @Test - void test_toCompletableFutureArray() { - @SuppressWarnings("unchecked") - CompletableFuture[] cfArray = new CompletableFuture[]{ - completedFuture(n), - completedFuture(anotherN) - }; - @SuppressWarnings("unchecked") - CompletionStage[] csArray = new CompletableFuture[]{ - cfArray[0], - cfArray[1], - }; + void test_cffuListToArray() { @SuppressWarnings("unchecked") - Cffu[] cffuArray = new Cffu[]{ - cffuFactory.toCffu(cfArray[0]), - cffuFactory.toCffu(cfArray[1]), + Cffu[] input = new Cffu[]{ + cffuFactory.completedFuture(n), + cffuFactory.completedFuture(anotherN), + cffuFactory.newIncompleteCffu() }; - assertArrayEquals(cfArray, toCompletableFutureArray(cfArray)); - assertArrayEquals(cfArray, toCompletableFutureArray(csArray)); - assertArrayEquals(cfArray, toCompletableFutureArray(cffuArray)); + assertArrayEquals(input, CffuFactory.cffuListToArray(Arrays.asList(input))); } @Test @@ -783,15 +770,26 @@ void test_cffuArrayUnwrap() { } @Test - void test_cffuListToArray() { + void test_toCompletableFutureArray() { @SuppressWarnings("unchecked") - Cffu[] input = new Cffu[]{ - cffuFactory.completedFuture(n), - cffuFactory.completedFuture(anotherN), - cffuFactory.newIncompleteCffu() + CompletableFuture[] cfArray = new CompletableFuture[]{ + completedFuture(n), + completedFuture(anotherN) + }; + @SuppressWarnings("unchecked") + CompletionStage[] csArray = new CompletableFuture[]{ + cfArray[0], + cfArray[1], + }; + @SuppressWarnings("unchecked") + Cffu[] cffuArray = new Cffu[]{ + cffuFactory.toCffu(cfArray[0]), + cffuFactory.toCffu(cfArray[1]), }; - assertArrayEquals(input, CffuFactory.cffuListToArray(Arrays.asList(input))); + assertArrayEquals(cfArray, toCompletableFutureArray(cfArray)); + assertArrayEquals(cfArray, toCompletableFutureArray(csArray)); + assertArrayEquals(cfArray, toCompletableFutureArray(cffuArray)); } ////////////////////////////////////////////////////////////////////////////////