From f3f809af29ed9aa084419502bda4b44eb30dec34 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 22 Feb 2025 18:07:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20wraps=20the=20supplied=20Executor=20to?= =?UTF-8?q?=20`Cffu`/`CffuFactory`=20=F0=9F=A7=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/io/foldright/cffu/Cffu.java | 124 +++++++++--------- .../java/io/foldright/cffu/CffuFactory.java | 76 ++++++----- .../io/foldright/cffu/CffuFactoryBuilder.java | 5 +- .../io/foldright/cffu/CffuFactoryTest.java | 2 - 4 files changed, 107 insertions(+), 100 deletions(-) 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 829869cb..b98a5e4a 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java +++ b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java @@ -15,6 +15,8 @@ import java.util.concurrent.*; import java.util.function.*; +import static io.foldright.cffu.CffuFactoryBuilder.cffuScreenedExecutor; +import static io.foldright.cffu.CffuFactoryBuilder.cffuUnscreenedExecutor; import static java.util.Objects.requireNonNull; @@ -118,7 +120,7 @@ public Cffu thenApplyAsync(Function fn) { @CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `thenAcceptAsync`") @Override public Cffu thenApplyAsync(Function fn, Executor executor) { - return resetCf(cf.thenApplyAsync(fn, executor)); + return resetCf(cf.thenApplyAsync(fn, cffuScreenedExecutor(executor))); } /** @@ -159,7 +161,7 @@ public Cffu thenAcceptAsync(Consumer action) { */ @Override public Cffu thenAcceptAsync(Consumer action, Executor executor) { - return resetCf(cf.thenAcceptAsync(action, executor)); + return resetCf(cf.thenAcceptAsync(action, cffuScreenedExecutor(executor))); } /** @@ -197,7 +199,7 @@ public Cffu thenRunAsync(Runnable action) { */ @Override public Cffu thenRunAsync(Runnable action, Executor executor) { - return resetCf(cf.thenRunAsync(action, executor)); + return resetCf(cf.thenRunAsync(action, cffuScreenedExecutor(executor))); } // endregion @@ -235,7 +237,7 @@ public final Cffu> thenMApplyFailFastAsync(Function Cffu> thenMApplyFailFastAsync(Executor executor, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyFailFastAsync(cf, executor, fns)); + return resetCf(CompletableFutureUtils.thenMApplyFailFastAsync(cf, cffuScreenedExecutor(executor), fns)); } /** @@ -266,7 +268,7 @@ public final Cffu> thenMApplyAllSuccessAsync( @SafeVarargs public final Cffu> thenMApplyAllSuccessAsync( Executor executor, @Nullable U valueIfFailed, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessAsync(cf, executor, valueIfFailed, fns)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessAsync(cf, cffuScreenedExecutor(executor), valueIfFailed, fns)); } /** @@ -293,7 +295,7 @@ public final Cffu> thenMApplyMostSuccessAsync( public final Cffu> thenMApplyMostSuccessAsync( Executor executor, @Nullable U valueIfNotSuccess, long timeout, TimeUnit unit, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyMostSuccessAsync(cf, executor, valueIfNotSuccess, timeout, unit, fns)); + return resetCf(CompletableFutureUtils.thenMApplyMostSuccessAsync(cf, cffuScreenedExecutor(executor), valueIfNotSuccess, timeout, unit, fns)); } /** @@ -320,7 +322,7 @@ public final Cffu> thenMApplyAsync(Function. */ @SafeVarargs public final Cffu> thenMApplyAsync(Executor executor, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyAsync(cf, executor, fns)); + return resetCf(CompletableFutureUtils.thenMApplyAsync(cf, cffuScreenedExecutor(executor), fns)); } /** @@ -347,7 +349,7 @@ public final Cffu thenMApplyAnySuccessAsync(Function Cffu thenMApplyAnySuccessAsync(Executor executor, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyAnySuccessAsync(cf, executor, fns)); + return resetCf(CompletableFutureUtils.thenMApplyAnySuccessAsync(cf, cffuScreenedExecutor(executor), fns)); } /** @@ -374,7 +376,7 @@ public final Cffu thenMApplyAnyAsync(Function... */ @SafeVarargs public final Cffu thenMApplyAnyAsync(Executor executor, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyAnyAsync(cf, executor, fns)); + return resetCf(CompletableFutureUtils.thenMApplyAnyAsync(cf, cffuScreenedExecutor(executor), fns)); } /** @@ -406,7 +408,7 @@ public final Cffu thenMAcceptFailFastAsync(Consumer... actions) */ @SafeVarargs public final Cffu thenMAcceptFailFastAsync(Executor executor, Consumer... actions) { - return resetCf(CompletableFutureUtils.thenMAcceptFailFastAsync(cf, executor, actions)); + return resetCf(CompletableFutureUtils.thenMAcceptFailFastAsync(cf, cffuScreenedExecutor(executor), actions)); } /** @@ -438,7 +440,7 @@ public final Cffu thenMAcceptAsync(Consumer... actions) { */ @SafeVarargs public final Cffu thenMAcceptAsync(Executor executor, Consumer... actions) { - return resetCf(CompletableFutureUtils.thenMAcceptAsync(cf, executor, actions)); + return resetCf(CompletableFutureUtils.thenMAcceptAsync(cf, cffuScreenedExecutor(executor), actions)); } /** @@ -470,7 +472,7 @@ public final Cffu thenMAcceptAnySuccessAsync(Consumer... action */ @SafeVarargs public final Cffu thenMAcceptAnySuccessAsync(Executor executor, Consumer... actions) { - return resetCf(CompletableFutureUtils.thenMAcceptAnySuccessAsync(cf, executor, actions)); + return resetCf(CompletableFutureUtils.thenMAcceptAnySuccessAsync(cf, cffuScreenedExecutor(executor), actions)); } /** @@ -502,7 +504,7 @@ public final Cffu thenMAcceptAnyAsync(Consumer... actions) { */ @SafeVarargs public final Cffu thenMAcceptAnyAsync(Executor executor, Consumer... actions) { - return resetCf(CompletableFutureUtils.thenMAcceptAnyAsync(cf, executor, actions)); + return resetCf(CompletableFutureUtils.thenMAcceptAnyAsync(cf, cffuScreenedExecutor(executor), actions)); } /** @@ -522,7 +524,7 @@ public Cffu thenMRunFailFastAsync(Runnable... actions) { * See the {@link CffuFactory#allFailFastOf allFailFastOf} documentation for the rules of result computation. */ public Cffu thenMRunFailFastAsync(Executor executor, Runnable... actions) { - return resetCf(CompletableFutureUtils.thenMRunFailFastAsync(cf, executor, actions)); + return resetCf(CompletableFutureUtils.thenMRunFailFastAsync(cf, cffuScreenedExecutor(executor), actions)); } /** @@ -542,7 +544,7 @@ public Cffu thenMRunAsync(Runnable... actions) { * See the {@link CffuFactory#allOf allOf} documentation for the rules of result computation. */ public Cffu thenMRunAsync(Executor executor, Runnable... actions) { - return resetCf(CompletableFutureUtils.thenMRunAsync(cf, executor, actions)); + return resetCf(CompletableFutureUtils.thenMRunAsync(cf, cffuScreenedExecutor(executor), actions)); } /** @@ -562,7 +564,7 @@ public Cffu thenMRunAnySuccessAsync(Runnable... actions) { * See the {@link CffuFactory#anySuccessOf anySuccessOf} documentation for the rules of result computation. */ public Cffu thenMRunAnySuccessAsync(Executor executor, Runnable... actions) { - return resetCf(CompletableFutureUtils.thenMRunAnySuccessAsync(cf, executor, actions)); + return resetCf(CompletableFutureUtils.thenMRunAnySuccessAsync(cf, cffuScreenedExecutor(executor), actions)); } /** @@ -582,7 +584,7 @@ public Cffu thenMRunAnyAsync(Runnable... actions) { * See the {@link CffuFactory#anyOf anyOf} documentation for the rules of result computation. */ public Cffu thenMRunAnyAsync(Executor executor, Runnable... actions) { - return resetCf(CompletableFutureUtils.thenMRunAnyAsync(cf, executor, actions)); + return resetCf(CompletableFutureUtils.thenMRunAnyAsync(cf, cffuScreenedExecutor(executor), actions)); } // endregion @@ -603,7 +605,7 @@ public Cffu> thenMApplyTupleFailFastAsync( */ public Cffu> thenMApplyTupleFailFastAsync( Executor executor, Function fn1, Function fn2) { - return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, executor, fn1, fn2)); + return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreenedExecutor(executor), fn1, fn2)); } /** @@ -621,7 +623,7 @@ public Cffu> thenMApplyTupleFailFastAsync( public Cffu> thenMApplyTupleFailFastAsync( Executor executor, Function fn1, Function fn2, Function fn3) { - return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, executor, fn1, fn2, fn3)); + return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3)); } /** @@ -639,7 +641,7 @@ public Cffu> thenMApplyTupleFailFastAsyn public Cffu> thenMApplyTupleFailFastAsync( Executor executor, Function fn1, Function fn2, Function fn3, Function fn4) { - return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, executor, fn1, fn2, fn3, fn4)); + return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4)); } /** @@ -659,7 +661,7 @@ public Cffu> thenMApplyTupleFail Executor executor, Function fn1, Function fn2, Function fn3, Function fn4, Function fn5) { - return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, executor, fn1, fn2, fn3, fn4, fn5)); + return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4, fn5)); } /** @@ -681,7 +683,7 @@ public Cffu> thenMApplyAllSuccessTupleAsync( */ public Cffu> thenMApplyAllSuccessTupleAsync( Executor executor, Function fn1, Function fn2) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, executor, fn1, fn2)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2)); } /** @@ -705,7 +707,7 @@ public Cffu> thenMApplyAllSuccessTupleAsync( public Cffu> thenMApplyAllSuccessTupleAsync( Executor executor, Function fn1, Function fn2, Function fn3) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, executor, fn1, fn2, fn3)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3)); } /** @@ -729,7 +731,7 @@ public Cffu> thenMApplyAllSuccessTupleAs public Cffu> thenMApplyAllSuccessTupleAsync( Executor executor, Function fn1, Function fn2, Function fn3, Function fn4) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, executor, fn1, fn2, fn3, fn4)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4)); } /** @@ -755,7 +757,7 @@ public Cffu> thenMApplyAllSucces Executor executor, Function fn1, Function fn2, Function fn3, Function fn4, Function fn5) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, executor, fn1, fn2, fn3, fn4, fn5)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4, fn5)); } /** @@ -780,7 +782,7 @@ public Cffu> thenMApplyMostSuccessTupleAsync( public Cffu> thenMApplyMostSuccessTupleAsync( Executor executor, long timeout, TimeUnit unit, Function fn1, Function fn2) { - return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, executor, timeout, unit, fn1, fn2)); + return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, cffuScreenedExecutor(executor), timeout, unit, fn1, fn2)); } /** @@ -806,7 +808,7 @@ public Cffu> thenMApplyMostSuccessTupleAsync( public Cffu> thenMApplyMostSuccessTupleAsync( Executor executor, long timeout, TimeUnit unit, Function fn1, Function fn2, Function fn3) { - return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, executor, timeout, unit, fn1, fn2, fn3)); + return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, cffuScreenedExecutor(executor), timeout, unit, fn1, fn2, fn3)); } /** @@ -834,7 +836,7 @@ public Cffu> thenMApplyMostSuccessTupleA Executor executor, long timeout, TimeUnit unit, Function fn1, Function fn2, Function fn3, Function fn4) { - return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, executor, timeout, unit, fn1, fn2, fn3, fn4)); + return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, cffuScreenedExecutor(executor), timeout, unit, fn1, fn2, fn3, fn4)); } /** @@ -863,7 +865,7 @@ public Cffu> thenMApplyMostSucce Function fn2, Function fn3, Function fn4, Function fn5) { return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync( - cf, executor, timeout, unit, fn1, fn2, fn3, fn4, fn5)); + cf, cffuScreenedExecutor(executor), timeout, unit, fn1, fn2, fn3, fn4, fn5)); } /** @@ -879,7 +881,7 @@ public Cffu> thenMApplyTupleAsync( */ public Cffu> thenMApplyTupleAsync( Executor executor, Function fn1, Function fn2) { - return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, executor, fn1, fn2)); + return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2)); } /** @@ -897,7 +899,7 @@ public Cffu> thenMApplyTupleAsync( public Cffu> thenMApplyTupleAsync( Executor executor, Function fn1, Function fn2, Function fn3) { - return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, executor, fn1, fn2, fn3)); + return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3)); } /** @@ -915,7 +917,7 @@ public Cffu> thenMApplyTupleAsync( public Cffu> thenMApplyTupleAsync( Executor executor, Function fn1, Function fn2, Function fn3, Function fn4) { - return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, executor, fn1, fn2, fn3, fn4)); + return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4)); } /** @@ -935,7 +937,7 @@ public Cffu> thenMApplyTupleAsyn Executor executor, Function fn1, Function fn2, Function fn3, Function fn4, Function fn5) { - return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, executor, fn1, fn2, fn3, fn4, fn5)); + return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4, fn5)); } // endregion @@ -999,7 +1001,7 @@ public Cffu thenCombineFailFastAsync( @CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `thenAcceptBothFailFastAsync`") public Cffu thenCombineFailFastAsync( CompletionStage other, BiFunction fn, Executor executor) { - return resetCf(CompletableFutureUtils.thenCombineFailFastAsync(cf, other, fn, executor)); + return resetCf(CompletableFutureUtils.thenCombineFailFastAsync(cf, other, fn, cffuScreenedExecutor(executor))); } /** @@ -1048,7 +1050,7 @@ public Cffu thenAcceptBothFailFastAsync( */ public Cffu thenAcceptBothFailFastAsync( CompletionStage other, BiConsumer action, Executor executor) { - return resetCf(CompletableFutureUtils.thenAcceptBothFailFastAsync(cf, other, action, executor)); + return resetCf(CompletableFutureUtils.thenAcceptBothFailFastAsync(cf, other, action, cffuScreenedExecutor(executor))); } /** @@ -1090,7 +1092,7 @@ public Cffu runAfterBothFailFastAsync(CompletionStage other, Runnable a * @param executor the executor to use for asynchronous execution */ public Cffu runAfterBothFailFastAsync(CompletionStage other, Runnable action, Executor executor) { - return resetCf(CompletableFutureUtils.runAfterBothFailFastAsync(cf, other, action, executor)); + return resetCf(CompletableFutureUtils.runAfterBothFailFastAsync(cf, other, action, cffuScreenedExecutor(executor))); } /** @@ -1145,7 +1147,7 @@ public Cffu thenCombineAsync( @Override public Cffu thenCombineAsync( CompletionStage other, BiFunction fn, Executor executor) { - return resetCf(cf.thenCombineAsync(other, fn, executor)); + return resetCf(cf.thenCombineAsync(other, fn, cffuScreenedExecutor(executor))); } /** @@ -1194,7 +1196,7 @@ public Cffu thenAcceptBothAsync( @Override public Cffu thenAcceptBothAsync( CompletionStage other, BiConsumer action, Executor executor) { - return resetCf(cf.thenAcceptBothAsync(other, action, executor)); + return resetCf(cf.thenAcceptBothAsync(other, action, cffuScreenedExecutor(executor))); } /** @@ -1237,7 +1239,7 @@ public Cffu runAfterBothAsync(CompletionStage other, Runnable action) { */ @Override public Cffu runAfterBothAsync(CompletionStage other, Runnable action, Executor executor) { - return resetCf(cf.runAfterBothAsync(other, action, executor)); + return resetCf(cf.runAfterBothAsync(other, action, cffuScreenedExecutor(executor))); } // endregion @@ -1288,7 +1290,7 @@ public Cffu applyToEitherSuccessAsync(CompletionStage other, @CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `acceptEitherSuccessAsync`") public Cffu applyToEitherSuccessAsync( CompletionStage other, Function fn, Executor executor) { - return resetCf(CompletableFutureUtils.applyToEitherSuccessAsync(cf, other, fn, executor)); + return resetCf(CompletableFutureUtils.applyToEitherSuccessAsync(cf, other, fn, cffuScreenedExecutor(executor))); } /** @@ -1324,7 +1326,7 @@ public Cffu acceptEitherSuccessAsync(CompletionStage other, C */ public Cffu acceptEitherSuccessAsync( CompletionStage other, Consumer action, Executor executor) { - return resetCf(CompletableFutureUtils.acceptEitherSuccessAsync(cf, other, action, executor)); + return resetCf(CompletableFutureUtils.acceptEitherSuccessAsync(cf, other, action, cffuScreenedExecutor(executor))); } /** @@ -1363,7 +1365,7 @@ public Cffu runAfterEitherSuccessAsync(CompletionStage other, Runnable * @param executor the executor to use for asynchronous execution */ public Cffu runAfterEitherSuccessAsync(CompletionStage other, Runnable action, Executor executor) { - return resetCf(CompletableFutureUtils.runAfterEitherSuccessAsync(cf, other, action, executor)); + return resetCf(CompletableFutureUtils.runAfterEitherSuccessAsync(cf, other, action, cffuScreenedExecutor(executor))); } /** @@ -1414,7 +1416,7 @@ public Cffu applyToEitherAsync(CompletionStage other, Functi @Override public Cffu applyToEitherAsync( CompletionStage other, Function fn, Executor executor) { - return resetCf(cf.applyToEitherAsync(other, fn, executor)); + return resetCf(cf.applyToEitherAsync(other, fn, cffuScreenedExecutor(executor))); } /** @@ -1459,7 +1461,7 @@ public Cffu acceptEitherAsync(CompletionStage other, Consumer @Override public Cffu acceptEitherAsync( CompletionStage other, Consumer action, Executor executor) { - return resetCf(cf.acceptEitherAsync(other, action, executor)); + return resetCf(cf.acceptEitherAsync(other, action, cffuScreenedExecutor(executor))); } /** @@ -1502,7 +1504,7 @@ public Cffu runAfterEitherAsync(CompletionStage other, Runnable action) */ @Override public Cffu runAfterEitherAsync(CompletionStage other, Runnable action, Executor executor) { - return resetCf(cf.runAfterEitherAsync(other, action, executor)); + return resetCf(cf.runAfterEitherAsync(other, action, cffuScreenedExecutor(executor))); } // endregion @@ -1573,7 +1575,7 @@ public Cffu catchingAsync( */ public Cffu catchingAsync( Class exceptionType, Function fallback, Executor executor) { - return resetCf(CompletableFutureUtils.catchingAsync(cf, exceptionType, fallback, executor)); + return resetCf(CompletableFutureUtils.catchingAsync(cf, exceptionType, fallback, cffuScreenedExecutor(executor))); } /** @@ -1632,7 +1634,7 @@ public Cffu exceptionallyAsync(Function fn) { */ @Override public Cffu exceptionallyAsync(Function fn, Executor executor) { - return resetCf(CompletableFutureUtils.exceptionallyAsync(cf, fn, executor)); + return resetCf(CompletableFutureUtils.exceptionallyAsync(cf, fn, cffuScreenedExecutor(executor))); } // endregion @@ -1658,7 +1660,7 @@ public Cffu exceptionallyAsync(Function fn, Executor */ public Cffu orTimeout(long timeout, TimeUnit unit) { checkMinimalStage(); - return resetCf(CompletableFutureUtils.cffuOrTimeout(cf, timeout, unit, fac.cffuExecutor)); + return resetCf(CompletableFutureUtils.cffuOrTimeout(cf, timeout, unit, fac.cffuExecutor.screened)); } /** @@ -1715,7 +1717,7 @@ public Cffu unsafeOrTimeout(long timeout, TimeUnit unit) { */ public Cffu completeOnTimeout(@Nullable T value, long timeout, TimeUnit unit) { checkMinimalStage(); - return resetCf(CompletableFutureUtils.cffuCompleteOnTimeout(cf, value, timeout, unit, fac.cffuExecutor)); + return resetCf(CompletableFutureUtils.cffuCompleteOnTimeout(cf, value, timeout, unit, fac.cffuExecutor.screened)); } /** @@ -1817,7 +1819,7 @@ public Cffu thenComposeAsync(Function Cffu thenComposeAsync(Function> fn, Executor executor) { - return resetCf(cf.thenComposeAsync(fn, executor)); + return resetCf(cf.thenComposeAsync(fn, cffuScreenedExecutor(executor))); } /** @@ -1881,7 +1883,7 @@ public Cffu catchingComposeAsync( */ public Cffu catchingComposeAsync( Class exceptionType, Function> fallback, Executor executor) { - return resetCf(CompletableFutureUtils.catchingComposeAsync(cf, exceptionType, fallback, executor)); + return resetCf(CompletableFutureUtils.catchingComposeAsync(cf, exceptionType, fallback, cffuScreenedExecutor(executor))); } /** @@ -1935,7 +1937,7 @@ public Cffu exceptionallyComposeAsync(Function exceptionallyComposeAsync(Function> fn, Executor executor) { - return resetCf(CompletableFutureUtils.exceptionallyComposeAsync(cf, fn, executor)); + return resetCf(CompletableFutureUtils.exceptionallyComposeAsync(cf, fn, cffuScreenedExecutor(executor))); } /** @@ -1989,7 +1991,7 @@ public Cffu handleAsync(BiFunction fn) @CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `peekAsync`") @Override public Cffu handleAsync(BiFunction fn, Executor executor) { - return resetCf(cf.handleAsync(fn, executor)); + return resetCf(cf.handleAsync(fn, cffuScreenedExecutor(executor))); } /** @@ -2062,7 +2064,7 @@ public Cffu whenCompleteAsync(BiConsumer action @CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `peekAsync`") @Override public Cffu whenCompleteAsync(BiConsumer action, Executor executor) { - return resetCf(cf.whenCompleteAsync(action, executor)); + return resetCf(cf.whenCompleteAsync(action, cffuScreenedExecutor(executor))); } /** @@ -2143,7 +2145,7 @@ public Cffu peekAsync(BiConsumer action) { */ @Contract("_, _ -> this") public Cffu peekAsync(BiConsumer action, Executor executor) { - CompletableFutureUtils.peekAsync(cf, action, executor); + CompletableFutureUtils.peekAsync(cf, action, cffuScreenedExecutor(executor)); return this; } @@ -2434,8 +2436,7 @@ public boolean complete(@Nullable T value) { */ @Contract("_ -> this") public Cffu completeAsync(Supplier supplier) { - // NOTE: do NOT translate executor to screened executor; same as CompletableFuture.completeAsync - return completeAsync(supplier, fac.cffuExecutor.unscreened); + return completeAsync(supplier, fac.cffuExecutor); } /** @@ -2449,7 +2450,8 @@ public Cffu completeAsync(Supplier supplier) { @Contract("_, _ -> this") public Cffu completeAsync(Supplier supplier, Executor executor) { checkMinimalStage(); - CompletableFutureUtils.completeAsync(cf, supplier, executor); + // NOTE: do NOT translate executor to screened executor; same as CompletableFuture.completeAsync + CompletableFutureUtils.completeAsync(cf, supplier, cffuUnscreenedExecutor(executor)); return this; } @@ -2473,8 +2475,7 @@ public boolean completeExceptionally(Throwable ex) { */ @Contract("_ -> this") public Cffu completeExceptionallyAsync(Supplier supplier) { - // NOTE: do NOT translate executor to screened executor; same as CompletableFuture.completeAsync - return completeExceptionallyAsync(supplier, fac.cffuExecutor.unscreened); + return completeExceptionallyAsync(supplier, fac.cffuExecutor); } /** @@ -2488,7 +2489,8 @@ public Cffu completeExceptionallyAsync(Supplier supplier @Contract("_, _ -> this") public Cffu completeExceptionallyAsync(Supplier supplier, Executor executor) { checkMinimalStage(); - CompletableFutureUtils.completeExceptionallyAsync(cf, supplier, executor); + // NOTE: do NOT translate executor to screened executor; same as CompletableFuture.completeAsync + CompletableFutureUtils.completeExceptionallyAsync(cf, supplier, cffuUnscreenedExecutor(executor)); return this; } 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 0e5d01f4..843835b8 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -18,6 +18,8 @@ import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import static io.foldright.cffu.CffuFactoryBuilder.cffuScreenedExecutor; +import static io.foldright.cffu.CffuFactoryBuilder.cffuUnscreenedExecutor; import static java.util.Objects.requireNonNull; @@ -132,7 +134,7 @@ public Cffu supplyAsync(Supplier supplier) { */ @CheckReturnValue(explanation = "should use the returned Cffu; otherwise, prefer method `runAsync`") public Cffu supplyAsync(Supplier supplier, Executor executor) { - return create(CompletableFuture.supplyAsync(supplier, executor)); + return create(CompletableFuture.supplyAsync(supplier, cffuScreenedExecutor(executor))); } /** @@ -153,7 +155,7 @@ public Cffu runAsync(Runnable action) { * @param executor the executor to use for asynchronous execution */ public Cffu runAsync(Runnable action, Executor executor) { - return create(CompletableFuture.runAsync(action, executor)); + return create(CompletableFuture.runAsync(action, cffuScreenedExecutor(executor))); } // endregion @@ -183,7 +185,7 @@ public final Cffu> mSupplyFailFastAsync(Supplier... sup */ @SafeVarargs public final Cffu> mSupplyFailFastAsync(Executor executor, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyFailFastAsync(executor, suppliers)); + return create(CompletableFutureUtils.mSupplyFailFastAsync(cffuScreenedExecutor(executor), suppliers)); } /** @@ -207,7 +209,7 @@ public final Cffu> mSupplyAllSuccessAsync( @SafeVarargs public final Cffu> mSupplyAllSuccessAsync( Executor executor, @Nullable T valueIfFailed, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyAllSuccessAsync(executor, valueIfFailed, suppliers)); + return create(CompletableFutureUtils.mSupplyAllSuccessAsync(cffuScreenedExecutor(executor), valueIfFailed, suppliers)); } /** @@ -234,7 +236,7 @@ public final Cffu> mSupplyMostSuccessAsync( public final Cffu> mSupplyMostSuccessAsync( Executor executor, @Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyMostSuccessAsync(executor, valueIfNotSuccess, timeout, unit, suppliers)); + return create(CompletableFutureUtils.mSupplyMostSuccessAsync(cffuScreenedExecutor(executor), valueIfNotSuccess, timeout, unit, suppliers)); } /** @@ -256,7 +258,7 @@ public final Cffu> mSupplyAsync(Supplier... suppliers) @SafeVarargs public final Cffu> mSupplyAsync( Executor executor, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyAsync(executor, suppliers)); + return create(CompletableFutureUtils.mSupplyAsync(cffuScreenedExecutor(executor), suppliers)); } /** @@ -277,7 +279,7 @@ public final Cffu mSupplyAnySuccessAsync(Supplier... supplie */ @SafeVarargs public final Cffu mSupplyAnySuccessAsync(Executor executor, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyAnySuccessAsync(executor, suppliers)); + return create(CompletableFutureUtils.mSupplyAnySuccessAsync(cffuScreenedExecutor(executor), suppliers)); } /** @@ -298,7 +300,7 @@ public final Cffu mSupplyAnyAsync(Supplier... suppliers) { */ @SafeVarargs public final Cffu mSupplyAnyAsync(Executor executor, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyAnyAsync(executor, suppliers)); + return create(CompletableFutureUtils.mSupplyAnyAsync(cffuScreenedExecutor(executor), suppliers)); } /** @@ -317,7 +319,7 @@ public Cffu mRunFailFastAsync(Runnable... actions) { * See the {@link #allFailFastOf allFailFastOf} documentation for the rules of result computation. */ public Cffu mRunFailFastAsync(Executor executor, Runnable... actions) { - return create(CompletableFutureUtils.mRunFailFastAsync(executor, actions)); + return create(CompletableFutureUtils.mRunFailFastAsync(cffuScreenedExecutor(executor), actions)); } /** @@ -335,7 +337,7 @@ public Cffu mRunAsync(Runnable... actions) { * See the {@link #allOf allOf} documentation for the rules of result computation. */ public Cffu mRunAsync(Executor executor, Runnable... actions) { - return create(CompletableFutureUtils.mRunAsync(executor, actions)); + return create(CompletableFutureUtils.mRunAsync(cffuScreenedExecutor(executor), actions)); } /** @@ -354,7 +356,7 @@ public Cffu mRunAnySuccessAsync(Runnable... actions) { * See the {@link #anySuccessOf anySuccessOf} documentation for the rules of result computation. */ public Cffu mRunAnySuccessAsync(Executor executor, Runnable... actions) { - return create(CompletableFutureUtils.mRunAnySuccessAsync(executor, actions)); + return create(CompletableFutureUtils.mRunAnySuccessAsync(cffuScreenedExecutor(executor), actions)); } /** @@ -372,7 +374,7 @@ public Cffu mRunAnyAsync(Runnable... actions) { * See the {@link #anyOf anyOf} documentation for the rules of result computation. */ public Cffu mRunAnyAsync(Executor executor, Runnable... actions) { - return create(CompletableFutureUtils.mRunAnyAsync(executor, actions)); + return create(CompletableFutureUtils.mRunAnyAsync(cffuScreenedExecutor(executor), actions)); } // endregion @@ -393,7 +395,7 @@ public Cffu> mSupplyTupleFailFastAsync( */ public Cffu> mSupplyTupleFailFastAsync( Executor executor, Supplier supplier1, Supplier supplier2) { - return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(executor, supplier1, supplier2)); + return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(cffuScreenedExecutor(executor), supplier1, supplier2)); } /** @@ -410,7 +412,7 @@ public Cffu> mSupplyTupleFailFastAsync( public Cffu> mSupplyTupleFailFastAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3) { - return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(executor, supplier1, supplier2, supplier3)); + return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3)); } /** @@ -428,7 +430,7 @@ public Cffu> mSupplyTupleFailFastAsync( public Cffu> mSupplyTupleFailFastAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { - return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(executor, supplier1, supplier2, supplier3, supplier4)); + return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4)); } /** @@ -447,7 +449,7 @@ public Cffu> mSupplyTupleFailFas Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { return create(CompletableFutureUtils.mSupplyTupleFailFastAsync( - executor, supplier1, supplier2, supplier3, supplier4, supplier5)); + cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); } /** @@ -469,7 +471,7 @@ public Cffu> mSupplyAllSuccessTupleAsync( */ public Cffu> mSupplyAllSuccessTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2) { - return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(executor, supplier1, supplier2)); + return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2)); } /** @@ -492,7 +494,7 @@ public Cffu> mSupplyAllSuccessTupleAsync( public Cffu> mSupplyAllSuccessTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3) { - return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(executor, supplier1, supplier2, supplier3)); + return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3)); } /** @@ -516,7 +518,7 @@ public Cffu> mSupplyAllSuccessTupleAsync public Cffu> mSupplyAllSuccessTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { - return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(executor, supplier1, supplier2, supplier3, supplier4)); + return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4)); } /** @@ -541,7 +543,7 @@ public Cffu> mSupplyAllSuccessTu Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync( - executor, supplier1, supplier2, supplier3, supplier4, supplier5)); + cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); } /** @@ -565,7 +567,7 @@ public Cffu> mSupplyMostSuccessTupleAsync( public Cffu> mSupplyMostSuccessTupleAsync( Executor executor, long timeout, TimeUnit unit, Supplier supplier1, Supplier supplier2) { - return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync(executor, timeout, unit, supplier1, supplier2)); + return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync(cffuScreenedExecutor(executor), timeout, unit, supplier1, supplier2)); } /** @@ -591,7 +593,7 @@ public Cffu> mSupplyMostSuccessTupleAsync( Executor executor, long timeout, TimeUnit unit, Supplier supplier1, Supplier supplier2, Supplier supplier3) { return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync( - executor, timeout, unit, supplier1, supplier2, supplier3)); + cffuScreenedExecutor(executor), timeout, unit, supplier1, supplier2, supplier3)); } /** @@ -617,7 +619,7 @@ public Cffu> mSupplyMostSuccessTupleAsyn Executor executor, long timeout, TimeUnit unit, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync( - executor, timeout, unit, supplier1, supplier2, supplier3, supplier4)); + cffuScreenedExecutor(executor), timeout, unit, supplier1, supplier2, supplier3, supplier4)); } /** @@ -645,7 +647,7 @@ public Cffu> mSupplyMostSuccessT Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync( - executor, timeout, unit, supplier1, supplier2, supplier3, supplier4, supplier5)); + cffuScreenedExecutor(executor), timeout, unit, supplier1, supplier2, supplier3, supplier4, supplier5)); } /** @@ -661,7 +663,7 @@ public Cffu> mSupplyTupleAsync( */ public Cffu> mSupplyTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2) { - return create(CompletableFutureUtils.mSupplyTupleAsync(executor, supplier1, supplier2)); + return create(CompletableFutureUtils.mSupplyTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2)); } /** @@ -678,7 +680,7 @@ public Cffu> mSupplyTupleAsync( public Cffu> mSupplyTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3) { - return create(CompletableFutureUtils.mSupplyTupleAsync(executor, supplier1, supplier2, supplier3)); + return create(CompletableFutureUtils.mSupplyTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3)); } /** @@ -696,7 +698,7 @@ public Cffu> mSupplyTupleAsync( public Cffu> mSupplyTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { - return create(CompletableFutureUtils.mSupplyTupleAsync(executor, supplier1, supplier2, supplier3, supplier4)); + return create(CompletableFutureUtils.mSupplyTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4)); } /** @@ -715,7 +717,7 @@ public Cffu> mSupplyTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { return create(CompletableFutureUtils.mSupplyTupleAsync( - executor, supplier1, supplier2, supplier3, supplier4, supplier5)); + cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); } // endregion @@ -793,7 +795,7 @@ public final Cffu> allSuccessResultsOf( public final Cffu> mostSuccessResultsOf( @Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, CompletionStage... cfs) { return create(CompletableFutureUtils.mostSuccessResultsOf( - cffuExecutor, valueIfNotSuccess, timeout, unit, cfs)); + cffuExecutor.screened, valueIfNotSuccess, timeout, unit, cfs)); } /** @@ -1016,7 +1018,7 @@ public Cffu> allSuccessTupleOf( @Contract(pure = true) public Cffu> mostSuccessTupleOf( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2) { - return create(CompletableFutureUtils.mostSuccessTupleOf(cffuExecutor, timeout, unit, cf1, cf2)); + return create(CompletableFutureUtils.mostSuccessTupleOf(cffuScreenedExecutor(cffuExecutor), timeout, unit, cf1, cf2)); } /** @@ -1030,7 +1032,8 @@ public Cffu> mostSuccessTupleOf( public Cffu> mostSuccessTupleOf( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3) { - return create(CompletableFutureUtils.mostSuccessTupleOf(cffuExecutor, timeout, unit, cf1, cf2, cf3)); + return create(CompletableFutureUtils.mostSuccessTupleOf( + cffuExecutor.screened, timeout, unit, cf1, cf2, cf3)); } /** @@ -1045,7 +1048,8 @@ public Cffu> mostSuccessTupleOf( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4) { - return create(CompletableFutureUtils.mostSuccessTupleOf(cffuExecutor, timeout, unit, cf1, cf2, cf3, cf4)); + return create(CompletableFutureUtils.mostSuccessTupleOf( + cffuExecutor.screened, timeout, unit, cf1, cf2, cf3, cf4)); } /** @@ -1060,7 +1064,8 @@ public Cffu> mostSuccessTupleOf( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4, CompletionStage cf5) { - return create(CompletableFutureUtils.mostSuccessTupleOf(cffuExecutor, timeout, unit, cf1, cf2, cf3, cf4, cf5)); + return create(CompletableFutureUtils.mostSuccessTupleOf( + cffuExecutor.screened, timeout, unit, cf1, cf2, cf3, cf4, cf5)); } /** @@ -1226,7 +1231,7 @@ public final Cffu[] toCffuArray(CompletionStage... stages) { */ @Contract(pure = true) public Executor delayedExecutor(long delay, TimeUnit unit) { - return CompletableFutureUtils.delayedExecutor(delay, unit, cffuExecutor); + return delayedExecutor(delay, unit, cffuExecutor); } /** @@ -1241,7 +1246,8 @@ public Executor delayedExecutor(long delay, TimeUnit unit) { */ @Contract(pure = true) public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) { - return CompletableFutureUtils.delayedExecutor(delay, unit, executor); + // NOTE: do NOT translate executor to screened executor; same as CompletableFuture.delayedExecutor + return CompletableFutureUtils.delayedExecutor(delay, unit, cffuUnscreenedExecutor(executor)); } // endregion diff --git a/cffu-core/src/main/java/io/foldright/cffu/CffuFactoryBuilder.java b/cffu-core/src/main/java/io/foldright/cffu/CffuFactoryBuilder.java index 1b8693b0..338803fb 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactoryBuilder.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactoryBuilder.java @@ -113,11 +113,12 @@ private CffuExecutorWrapper(Executor defaultExecutor) { } /** - * Delegates execution to {@link #screened} to treat this executor same as screened executor. + * This method is never called directly. Instead, use the related {@link #screened}/{@link #unscreened} executor + * from method {@link #cffuScreenedExecutor(Executor)}/{@link #cffuUnscreenedExecutor(Executor)} respectively. */ @Override public void execute(Runnable command) { - screened.execute(command); + throw new UnsupportedOperationException(); } @Override 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 3eb080d9..d4a963ad 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -916,8 +916,6 @@ void test_executorSetting_commonPool() throws Exception { assertEquals(n, fac.supplyAsync(() -> n).get()); } - // FIXME bug to be fixed! - @Disabled @Test void test_executorSetting_MayRunIn_ThreadPerTaskExecutor() throws Exception { final boolean USE_COMMON_POOL = ForkJoinPool.getCommonPoolParallelism() > 1;