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 85d12275..16188d58 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/Cffu.java +++ b/cffu-core/src/main/java/io/foldright/cffu/Cffu.java @@ -15,8 +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 io.foldright.cffu.CffuFactoryBuilder.cffuScreened; +import static io.foldright.cffu.CffuFactoryBuilder.cffuUnscreened; import static java.util.Objects.requireNonNull; @@ -120,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, cffuScreenedExecutor(executor))); + return resetCf(cf.thenApplyAsync(fn, cffuScreened(executor))); } /** @@ -161,7 +161,7 @@ public Cffu thenAcceptAsync(Consumer action) { */ @Override public Cffu thenAcceptAsync(Consumer action, Executor executor) { - return resetCf(cf.thenAcceptAsync(action, cffuScreenedExecutor(executor))); + return resetCf(cf.thenAcceptAsync(action, cffuScreened(executor))); } /** @@ -199,7 +199,7 @@ public Cffu thenRunAsync(Runnable action) { */ @Override public Cffu thenRunAsync(Runnable action, Executor executor) { - return resetCf(cf.thenRunAsync(action, cffuScreenedExecutor(executor))); + return resetCf(cf.thenRunAsync(action, cffuScreened(executor))); } // endregion @@ -237,7 +237,7 @@ public final Cffu> thenMApplyFailFastAsync(Function Cffu> thenMApplyFailFastAsync(Executor executor, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyFailFastAsync(cf, cffuScreenedExecutor(executor), fns)); + return resetCf(CompletableFutureUtils.thenMApplyFailFastAsync(cf, cffuScreened(executor), fns)); } /** @@ -268,7 +268,7 @@ public final Cffu> thenMApplyAllSuccessAsync( @SafeVarargs public final Cffu> thenMApplyAllSuccessAsync( Executor executor, @Nullable U valueIfFailed, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessAsync(cf, cffuScreenedExecutor(executor), valueIfFailed, fns)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessAsync(cf, cffuScreened(executor), valueIfFailed, fns)); } /** @@ -295,7 +295,8 @@ public final Cffu> thenMApplyMostSuccessAsync( public final Cffu> thenMApplyMostSuccessAsync( Executor executor, @Nullable U valueIfNotSuccess, long timeout, TimeUnit unit, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyMostSuccessAsync(cf, cffuScreenedExecutor(executor), valueIfNotSuccess, timeout, unit, fns)); + return resetCf(CompletableFutureUtils.thenMApplyMostSuccessAsync( + cf, cffuScreened(executor), valueIfNotSuccess, timeout, unit, fns)); } /** @@ -322,7 +323,7 @@ public final Cffu> thenMApplyAsync(Function. */ @SafeVarargs public final Cffu> thenMApplyAsync(Executor executor, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyAsync(cf, cffuScreenedExecutor(executor), fns)); + return resetCf(CompletableFutureUtils.thenMApplyAsync(cf, cffuScreened(executor), fns)); } /** @@ -349,7 +350,7 @@ public final Cffu thenMApplyAnySuccessAsync(Function Cffu thenMApplyAnySuccessAsync(Executor executor, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyAnySuccessAsync(cf, cffuScreenedExecutor(executor), fns)); + return resetCf(CompletableFutureUtils.thenMApplyAnySuccessAsync(cf, cffuScreened(executor), fns)); } /** @@ -376,7 +377,7 @@ public final Cffu thenMApplyAnyAsync(Function... */ @SafeVarargs public final Cffu thenMApplyAnyAsync(Executor executor, Function... fns) { - return resetCf(CompletableFutureUtils.thenMApplyAnyAsync(cf, cffuScreenedExecutor(executor), fns)); + return resetCf(CompletableFutureUtils.thenMApplyAnyAsync(cf, cffuScreened(executor), fns)); } /** @@ -408,7 +409,7 @@ public final Cffu thenMAcceptFailFastAsync(Consumer... actions) */ @SafeVarargs public final Cffu thenMAcceptFailFastAsync(Executor executor, Consumer... actions) { - return resetCf(CompletableFutureUtils.thenMAcceptFailFastAsync(cf, cffuScreenedExecutor(executor), actions)); + return resetCf(CompletableFutureUtils.thenMAcceptFailFastAsync(cf, cffuScreened(executor), actions)); } /** @@ -440,7 +441,7 @@ public final Cffu thenMAcceptAsync(Consumer... actions) { */ @SafeVarargs public final Cffu thenMAcceptAsync(Executor executor, Consumer... actions) { - return resetCf(CompletableFutureUtils.thenMAcceptAsync(cf, cffuScreenedExecutor(executor), actions)); + return resetCf(CompletableFutureUtils.thenMAcceptAsync(cf, cffuScreened(executor), actions)); } /** @@ -472,7 +473,7 @@ public final Cffu thenMAcceptAnySuccessAsync(Consumer... action */ @SafeVarargs public final Cffu thenMAcceptAnySuccessAsync(Executor executor, Consumer... actions) { - return resetCf(CompletableFutureUtils.thenMAcceptAnySuccessAsync(cf, cffuScreenedExecutor(executor), actions)); + return resetCf(CompletableFutureUtils.thenMAcceptAnySuccessAsync(cf, cffuScreened(executor), actions)); } /** @@ -504,7 +505,7 @@ public final Cffu thenMAcceptAnyAsync(Consumer... actions) { */ @SafeVarargs public final Cffu thenMAcceptAnyAsync(Executor executor, Consumer... actions) { - return resetCf(CompletableFutureUtils.thenMAcceptAnyAsync(cf, cffuScreenedExecutor(executor), actions)); + return resetCf(CompletableFutureUtils.thenMAcceptAnyAsync(cf, cffuScreened(executor), actions)); } /** @@ -524,7 +525,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, cffuScreenedExecutor(executor), actions)); + return resetCf(CompletableFutureUtils.thenMRunFailFastAsync(cf, cffuScreened(executor), actions)); } /** @@ -544,7 +545,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, cffuScreenedExecutor(executor), actions)); + return resetCf(CompletableFutureUtils.thenMRunAsync(cf, cffuScreened(executor), actions)); } /** @@ -564,7 +565,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, cffuScreenedExecutor(executor), actions)); + return resetCf(CompletableFutureUtils.thenMRunAnySuccessAsync(cf, cffuScreened(executor), actions)); } /** @@ -584,7 +585,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, cffuScreenedExecutor(executor), actions)); + return resetCf(CompletableFutureUtils.thenMRunAnyAsync(cf, cffuScreened(executor), actions)); } // endregion @@ -605,7 +606,7 @@ public Cffu> thenMApplyTupleFailFastAsync( */ public Cffu> thenMApplyTupleFailFastAsync( Executor executor, Function fn1, Function fn2) { - return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreenedExecutor(executor), fn1, fn2)); + return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreened(executor), fn1, fn2)); } /** @@ -623,7 +624,7 @@ public Cffu> thenMApplyTupleFailFastAsync( public Cffu> thenMApplyTupleFailFastAsync( Executor executor, Function fn1, Function fn2, Function fn3) { - return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3)); + return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreened(executor), fn1, fn2, fn3)); } /** @@ -641,7 +642,7 @@ public Cffu> thenMApplyTupleFailFastAsyn public Cffu> thenMApplyTupleFailFastAsync( Executor executor, Function fn1, Function fn2, Function fn3, Function fn4) { - return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4)); + return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreened(executor), fn1, fn2, fn3, fn4)); } /** @@ -661,7 +662,7 @@ public Cffu> thenMApplyTupleFail Executor executor, Function fn1, Function fn2, Function fn3, Function fn4, Function fn5) { - return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4, fn5)); + return resetCf(CompletableFutureUtils.thenMApplyTupleFailFastAsync(cf, cffuScreened(executor), fn1, fn2, fn3, fn4, fn5)); } /** @@ -683,7 +684,7 @@ public Cffu> thenMApplyAllSuccessTupleAsync( */ public Cffu> thenMApplyAllSuccessTupleAsync( Executor executor, Function fn1, Function fn2) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreened(executor), fn1, fn2)); } /** @@ -707,7 +708,7 @@ public Cffu> thenMApplyAllSuccessTupleAsync( public Cffu> thenMApplyAllSuccessTupleAsync( Executor executor, Function fn1, Function fn2, Function fn3) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreened(executor), fn1, fn2, fn3)); } /** @@ -731,7 +732,7 @@ public Cffu> thenMApplyAllSuccessTupleAs public Cffu> thenMApplyAllSuccessTupleAsync( Executor executor, Function fn1, Function fn2, Function fn3, Function fn4) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreened(executor), fn1, fn2, fn3, fn4)); } /** @@ -757,7 +758,7 @@ public Cffu> thenMApplyAllSucces Executor executor, Function fn1, Function fn2, Function fn3, Function fn4, Function fn5) { - return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4, fn5)); + return resetCf(CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(cf, cffuScreened(executor), fn1, fn2, fn3, fn4, fn5)); } /** @@ -782,7 +783,8 @@ public Cffu> thenMApplyMostSuccessTupleAsync( public Cffu> thenMApplyMostSuccessTupleAsync( Executor executor, long timeout, TimeUnit unit, Function fn1, Function fn2) { - return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, cffuScreenedExecutor(executor), timeout, unit, fn1, fn2)); + return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync( + cf, cffuScreened(executor), timeout, unit, fn1, fn2)); } /** @@ -808,7 +810,8 @@ public Cffu> thenMApplyMostSuccessTupleAsync( public Cffu> thenMApplyMostSuccessTupleAsync( Executor executor, long timeout, TimeUnit unit, Function fn1, Function fn2, Function fn3) { - return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, cffuScreenedExecutor(executor), timeout, unit, fn1, fn2, fn3)); + return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync( + cf, cffuScreened(executor), timeout, unit, fn1, fn2, fn3)); } /** @@ -836,7 +839,8 @@ public Cffu> thenMApplyMostSuccessTupleA Executor executor, long timeout, TimeUnit unit, Function fn1, Function fn2, Function fn3, Function fn4) { - return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync(cf, cffuScreenedExecutor(executor), timeout, unit, fn1, fn2, fn3, fn4)); + return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync( + cf, cffuScreened(executor), timeout, unit, fn1, fn2, fn3, fn4)); } /** @@ -865,7 +869,7 @@ public Cffu> thenMApplyMostSucce Function fn2, Function fn3, Function fn4, Function fn5) { return resetCf(CompletableFutureUtils.thenMApplyMostSuccessTupleAsync( - cf, cffuScreenedExecutor(executor), timeout, unit, fn1, fn2, fn3, fn4, fn5)); + cf, cffuScreened(executor), timeout, unit, fn1, fn2, fn3, fn4, fn5)); } /** @@ -881,7 +885,7 @@ public Cffu> thenMApplyTupleAsync( */ public Cffu> thenMApplyTupleAsync( Executor executor, Function fn1, Function fn2) { - return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2)); + return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreened(executor), fn1, fn2)); } /** @@ -899,7 +903,7 @@ public Cffu> thenMApplyTupleAsync( public Cffu> thenMApplyTupleAsync( Executor executor, Function fn1, Function fn2, Function fn3) { - return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3)); + return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreened(executor), fn1, fn2, fn3)); } /** @@ -917,7 +921,7 @@ public Cffu> thenMApplyTupleAsync( public Cffu> thenMApplyTupleAsync( Executor executor, Function fn1, Function fn2, Function fn3, Function fn4) { - return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4)); + return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreened(executor), fn1, fn2, fn3, fn4)); } /** @@ -937,7 +941,7 @@ public Cffu> thenMApplyTupleAsyn Executor executor, Function fn1, Function fn2, Function fn3, Function fn4, Function fn5) { - return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreenedExecutor(executor), fn1, fn2, fn3, fn4, fn5)); + return resetCf(CompletableFutureUtils.thenMApplyTupleAsync(cf, cffuScreened(executor), fn1, fn2, fn3, fn4, fn5)); } // endregion @@ -1001,7 +1005,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, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.thenCombineFailFastAsync(cf, other, fn, cffuScreened(executor))); } /** @@ -1050,7 +1054,7 @@ public Cffu thenAcceptBothFailFastAsync( */ public Cffu thenAcceptBothFailFastAsync( CompletionStage other, BiConsumer action, Executor executor) { - return resetCf(CompletableFutureUtils.thenAcceptBothFailFastAsync(cf, other, action, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.thenAcceptBothFailFastAsync(cf, other, action, cffuScreened(executor))); } /** @@ -1092,7 +1096,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, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.runAfterBothFailFastAsync(cf, other, action, cffuScreened(executor))); } /** @@ -1147,7 +1151,7 @@ public Cffu thenCombineAsync( @Override public Cffu thenCombineAsync( CompletionStage other, BiFunction fn, Executor executor) { - return resetCf(cf.thenCombineAsync(other, fn, cffuScreenedExecutor(executor))); + return resetCf(cf.thenCombineAsync(other, fn, cffuScreened(executor))); } /** @@ -1196,7 +1200,7 @@ public Cffu thenAcceptBothAsync( @Override public Cffu thenAcceptBothAsync( CompletionStage other, BiConsumer action, Executor executor) { - return resetCf(cf.thenAcceptBothAsync(other, action, cffuScreenedExecutor(executor))); + return resetCf(cf.thenAcceptBothAsync(other, action, cffuScreened(executor))); } /** @@ -1239,7 +1243,7 @@ public Cffu runAfterBothAsync(CompletionStage other, Runnable action) { */ @Override public Cffu runAfterBothAsync(CompletionStage other, Runnable action, Executor executor) { - return resetCf(cf.runAfterBothAsync(other, action, cffuScreenedExecutor(executor))); + return resetCf(cf.runAfterBothAsync(other, action, cffuScreened(executor))); } // endregion @@ -1290,7 +1294,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, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.applyToEitherSuccessAsync(cf, other, fn, cffuScreened(executor))); } /** @@ -1326,7 +1330,7 @@ public Cffu acceptEitherSuccessAsync(CompletionStage other, C */ public Cffu acceptEitherSuccessAsync( CompletionStage other, Consumer action, Executor executor) { - return resetCf(CompletableFutureUtils.acceptEitherSuccessAsync(cf, other, action, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.acceptEitherSuccessAsync(cf, other, action, cffuScreened(executor))); } /** @@ -1365,7 +1369,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, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.runAfterEitherSuccessAsync(cf, other, action, cffuScreened(executor))); } /** @@ -1416,7 +1420,7 @@ public Cffu applyToEitherAsync(CompletionStage other, Functi @Override public Cffu applyToEitherAsync( CompletionStage other, Function fn, Executor executor) { - return resetCf(cf.applyToEitherAsync(other, fn, cffuScreenedExecutor(executor))); + return resetCf(cf.applyToEitherAsync(other, fn, cffuScreened(executor))); } /** @@ -1461,7 +1465,7 @@ public Cffu acceptEitherAsync(CompletionStage other, Consumer @Override public Cffu acceptEitherAsync( CompletionStage other, Consumer action, Executor executor) { - return resetCf(cf.acceptEitherAsync(other, action, cffuScreenedExecutor(executor))); + return resetCf(cf.acceptEitherAsync(other, action, cffuScreened(executor))); } /** @@ -1504,7 +1508,7 @@ public Cffu runAfterEitherAsync(CompletionStage other, Runnable action) */ @Override public Cffu runAfterEitherAsync(CompletionStage other, Runnable action, Executor executor) { - return resetCf(cf.runAfterEitherAsync(other, action, cffuScreenedExecutor(executor))); + return resetCf(cf.runAfterEitherAsync(other, action, cffuScreened(executor))); } // endregion @@ -1575,7 +1579,7 @@ public Cffu catchingAsync( */ public Cffu catchingAsync( Class exceptionType, Function fallback, Executor executor) { - return resetCf(CompletableFutureUtils.catchingAsync(cf, exceptionType, fallback, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.catchingAsync(cf, exceptionType, fallback, cffuScreened(executor))); } /** @@ -1634,7 +1638,7 @@ public Cffu exceptionallyAsync(Function fn) { */ @Override public Cffu exceptionallyAsync(Function fn, Executor executor) { - return resetCf(CompletableFutureUtils.exceptionallyAsync(cf, fn, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.exceptionallyAsync(cf, fn, cffuScreened(executor))); } // endregion @@ -1660,7 +1664,7 @@ public Cffu exceptionallyAsync(Function fn, Executor */ public Cffu orTimeout(long timeout, TimeUnit unit) { checkMinimalStage(); - return resetCf(CompletableFutureUtils.cffuOrTimeout(cf, timeout, unit, cffuScreenedExecutor(fac.cffuExecutor))); + return resetCf(CompletableFutureUtils.cffuOrTimeout(cf, timeout, unit, cffuScreened(fac.cffuExecutor))); } /** @@ -1717,7 +1721,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, cffuScreenedExecutor(fac.cffuExecutor))); + return resetCf(CompletableFutureUtils.cffuCompleteOnTimeout(cf, value, timeout, unit, cffuScreened(fac.cffuExecutor))); } /** @@ -1819,7 +1823,7 @@ public Cffu thenComposeAsync(Function Cffu thenComposeAsync(Function> fn, Executor executor) { - return resetCf(cf.thenComposeAsync(fn, cffuScreenedExecutor(executor))); + return resetCf(cf.thenComposeAsync(fn, cffuScreened(executor))); } /** @@ -1883,7 +1887,7 @@ public Cffu catchingComposeAsync( */ public Cffu catchingComposeAsync( Class exceptionType, Function> fallback, Executor executor) { - return resetCf(CompletableFutureUtils.catchingComposeAsync(cf, exceptionType, fallback, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.catchingComposeAsync(cf, exceptionType, fallback, cffuScreened(executor))); } /** @@ -1937,7 +1941,7 @@ public Cffu exceptionallyComposeAsync(Function exceptionallyComposeAsync(Function> fn, Executor executor) { - return resetCf(CompletableFutureUtils.exceptionallyComposeAsync(cf, fn, cffuScreenedExecutor(executor))); + return resetCf(CompletableFutureUtils.exceptionallyComposeAsync(cf, fn, cffuScreened(executor))); } /** @@ -1991,7 +1995,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, cffuScreenedExecutor(executor))); + return resetCf(cf.handleAsync(fn, cffuScreened(executor))); } /** @@ -2064,7 +2068,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, cffuScreenedExecutor(executor))); + return resetCf(cf.whenCompleteAsync(action, cffuScreened(executor))); } /** @@ -2145,7 +2149,7 @@ public Cffu peekAsync(BiConsumer action) { */ @Contract("_, _ -> this") public Cffu peekAsync(BiConsumer action, Executor executor) { - CompletableFutureUtils.peekAsync(cf, action, cffuScreenedExecutor(executor)); + CompletableFutureUtils.peekAsync(cf, action, cffuScreened(executor)); return this; } @@ -2451,7 +2455,7 @@ public Cffu completeAsync(Supplier supplier) { public Cffu completeAsync(Supplier supplier, Executor executor) { checkMinimalStage(); // NOTE: do NOT translate executor to screened executor; same as CompletableFuture.completeAsync - CompletableFutureUtils.completeAsync(cf, supplier, cffuUnscreenedExecutor(executor)); + CompletableFutureUtils.completeAsync(cf, supplier, cffuUnscreened(executor)); return this; } @@ -2490,7 +2494,7 @@ public Cffu completeExceptionallyAsync(Supplier supplier public Cffu completeExceptionallyAsync(Supplier supplier, Executor executor) { checkMinimalStage(); // NOTE: do NOT translate executor to screened executor; same as CompletableFuture.completeAsync - CompletableFutureUtils.completeExceptionallyAsync(cf, supplier, cffuUnscreenedExecutor(executor)); + CompletableFutureUtils.completeExceptionallyAsync(cf, supplier, cffuUnscreened(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 471e6d42..317e405f 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -18,8 +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 io.foldright.cffu.CffuFactoryBuilder.cffuScreened; +import static io.foldright.cffu.CffuFactoryBuilder.cffuUnscreened; import static java.util.Objects.requireNonNull; @@ -134,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, cffuScreenedExecutor(executor))); + return create(CompletableFuture.supplyAsync(supplier, cffuScreened(executor))); } /** @@ -155,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, cffuScreenedExecutor(executor))); + return create(CompletableFuture.runAsync(action, cffuScreened(executor))); } // endregion @@ -185,7 +185,7 @@ public final Cffu> mSupplyFailFastAsync(Supplier... sup */ @SafeVarargs public final Cffu> mSupplyFailFastAsync(Executor executor, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyFailFastAsync(cffuScreenedExecutor(executor), suppliers)); + return create(CompletableFutureUtils.mSupplyFailFastAsync(cffuScreened(executor), suppliers)); } /** @@ -209,7 +209,7 @@ public final Cffu> mSupplyAllSuccessAsync( @SafeVarargs public final Cffu> mSupplyAllSuccessAsync( Executor executor, @Nullable T valueIfFailed, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyAllSuccessAsync(cffuScreenedExecutor(executor), valueIfFailed, suppliers)); + return create(CompletableFutureUtils.mSupplyAllSuccessAsync(cffuScreened(executor), valueIfFailed, suppliers)); } /** @@ -236,7 +236,8 @@ public final Cffu> mSupplyMostSuccessAsync( public final Cffu> mSupplyMostSuccessAsync( Executor executor, @Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyMostSuccessAsync(cffuScreenedExecutor(executor), valueIfNotSuccess, timeout, unit, suppliers)); + return create(CompletableFutureUtils.mSupplyMostSuccessAsync( + cffuScreened(executor), valueIfNotSuccess, timeout, unit, suppliers)); } /** @@ -258,7 +259,7 @@ public final Cffu> mSupplyAsync(Supplier... suppliers) @SafeVarargs public final Cffu> mSupplyAsync( Executor executor, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyAsync(cffuScreenedExecutor(executor), suppliers)); + return create(CompletableFutureUtils.mSupplyAsync(cffuScreened(executor), suppliers)); } /** @@ -279,7 +280,7 @@ public final Cffu mSupplyAnySuccessAsync(Supplier... supplie */ @SafeVarargs public final Cffu mSupplyAnySuccessAsync(Executor executor, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyAnySuccessAsync(cffuScreenedExecutor(executor), suppliers)); + return create(CompletableFutureUtils.mSupplyAnySuccessAsync(cffuScreened(executor), suppliers)); } /** @@ -300,7 +301,7 @@ public final Cffu mSupplyAnyAsync(Supplier... suppliers) { */ @SafeVarargs public final Cffu mSupplyAnyAsync(Executor executor, Supplier... suppliers) { - return create(CompletableFutureUtils.mSupplyAnyAsync(cffuScreenedExecutor(executor), suppliers)); + return create(CompletableFutureUtils.mSupplyAnyAsync(cffuScreened(executor), suppliers)); } /** @@ -319,7 +320,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(cffuScreenedExecutor(executor), actions)); + return create(CompletableFutureUtils.mRunFailFastAsync(cffuScreened(executor), actions)); } /** @@ -337,7 +338,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(cffuScreenedExecutor(executor), actions)); + return create(CompletableFutureUtils.mRunAsync(cffuScreened(executor), actions)); } /** @@ -356,7 +357,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(cffuScreenedExecutor(executor), actions)); + return create(CompletableFutureUtils.mRunAnySuccessAsync(cffuScreened(executor), actions)); } /** @@ -374,7 +375,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(cffuScreenedExecutor(executor), actions)); + return create(CompletableFutureUtils.mRunAnyAsync(cffuScreened(executor), actions)); } // endregion @@ -395,7 +396,7 @@ public Cffu> mSupplyTupleFailFastAsync( */ public Cffu> mSupplyTupleFailFastAsync( Executor executor, Supplier supplier1, Supplier supplier2) { - return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(cffuScreenedExecutor(executor), supplier1, supplier2)); + return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(cffuScreened(executor), supplier1, supplier2)); } /** @@ -412,7 +413,7 @@ public Cffu> mSupplyTupleFailFastAsync( public Cffu> mSupplyTupleFailFastAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3) { - return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3)); + return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(cffuScreened(executor), supplier1, supplier2, supplier3)); } /** @@ -430,7 +431,8 @@ public Cffu> mSupplyTupleFailFastAsync( public Cffu> mSupplyTupleFailFastAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { - return create(CompletableFutureUtils.mSupplyTupleFailFastAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4)); + return create(CompletableFutureUtils.mSupplyTupleFailFastAsync( + cffuScreened(executor), supplier1, supplier2, supplier3, supplier4)); } /** @@ -449,7 +451,7 @@ public Cffu> mSupplyTupleFailFas Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { return create(CompletableFutureUtils.mSupplyTupleFailFastAsync( - cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); + cffuScreened(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); } /** @@ -471,7 +473,7 @@ public Cffu> mSupplyAllSuccessTupleAsync( */ public Cffu> mSupplyAllSuccessTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2) { - return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2)); + return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(cffuScreened(executor), supplier1, supplier2)); } /** @@ -494,7 +496,7 @@ public Cffu> mSupplyAllSuccessTupleAsync( public Cffu> mSupplyAllSuccessTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3) { - return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3)); + return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(cffuScreened(executor), supplier1, supplier2, supplier3)); } /** @@ -518,7 +520,8 @@ public Cffu> mSupplyAllSuccessTupleAsync public Cffu> mSupplyAllSuccessTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { - return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4)); + return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync( + cffuScreened(executor), supplier1, supplier2, supplier3, supplier4)); } /** @@ -543,7 +546,7 @@ public Cffu> mSupplyAllSuccessTu Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { return create(CompletableFutureUtils.mSupplyAllSuccessTupleAsync( - cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); + cffuScreened(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); } /** @@ -567,7 +570,8 @@ public Cffu> mSupplyMostSuccessTupleAsync( public Cffu> mSupplyMostSuccessTupleAsync( Executor executor, long timeout, TimeUnit unit, Supplier supplier1, Supplier supplier2) { - return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync(cffuScreenedExecutor(executor), timeout, unit, supplier1, supplier2)); + return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync( + cffuScreened(executor), timeout, unit, supplier1, supplier2)); } /** @@ -593,7 +597,7 @@ public Cffu> mSupplyMostSuccessTupleAsync( Executor executor, long timeout, TimeUnit unit, Supplier supplier1, Supplier supplier2, Supplier supplier3) { return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync( - cffuScreenedExecutor(executor), timeout, unit, supplier1, supplier2, supplier3)); + cffuScreened(executor), timeout, unit, supplier1, supplier2, supplier3)); } /** @@ -619,7 +623,7 @@ public Cffu> mSupplyMostSuccessTupleAsyn Executor executor, long timeout, TimeUnit unit, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync( - cffuScreenedExecutor(executor), timeout, unit, supplier1, supplier2, supplier3, supplier4)); + cffuScreened(executor), timeout, unit, supplier1, supplier2, supplier3, supplier4)); } /** @@ -647,7 +651,7 @@ public Cffu> mSupplyMostSuccessT Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { return create(CompletableFutureUtils.mSupplyMostSuccessTupleAsync( - cffuScreenedExecutor(executor), timeout, unit, supplier1, supplier2, supplier3, supplier4, supplier5)); + cffuScreened(executor), timeout, unit, supplier1, supplier2, supplier3, supplier4, supplier5)); } /** @@ -663,7 +667,7 @@ public Cffu> mSupplyTupleAsync( */ public Cffu> mSupplyTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2) { - return create(CompletableFutureUtils.mSupplyTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2)); + return create(CompletableFutureUtils.mSupplyTupleAsync(cffuScreened(executor), supplier1, supplier2)); } /** @@ -680,7 +684,7 @@ public Cffu> mSupplyTupleAsync( public Cffu> mSupplyTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3) { - return create(CompletableFutureUtils.mSupplyTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3)); + return create(CompletableFutureUtils.mSupplyTupleAsync(cffuScreened(executor), supplier1, supplier2, supplier3)); } /** @@ -698,7 +702,8 @@ public Cffu> mSupplyTupleAsync( public Cffu> mSupplyTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4) { - return create(CompletableFutureUtils.mSupplyTupleAsync(cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4)); + return create(CompletableFutureUtils.mSupplyTupleAsync( + cffuScreened(executor), supplier1, supplier2, supplier3, supplier4)); } /** @@ -717,7 +722,7 @@ public Cffu> mSupplyTupleAsync( Executor executor, Supplier supplier1, Supplier supplier2, Supplier supplier3, Supplier supplier4, Supplier supplier5) { return create(CompletableFutureUtils.mSupplyTupleAsync( - cffuScreenedExecutor(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); + cffuScreened(executor), supplier1, supplier2, supplier3, supplier4, supplier5)); } // endregion @@ -795,7 +800,7 @@ public final Cffu> allSuccessResultsOf( public final Cffu> mostSuccessResultsOf( @Nullable T valueIfNotSuccess, long timeout, TimeUnit unit, CompletionStage... cfs) { return create(CompletableFutureUtils.mostSuccessResultsOf( - cffuScreenedExecutor(cffuExecutor), valueIfNotSuccess, timeout, unit, cfs)); + cffuScreened(cffuExecutor), valueIfNotSuccess, timeout, unit, cfs)); } /** @@ -1018,7 +1023,7 @@ public Cffu> allSuccessTupleOf( @Contract(pure = true) public Cffu> mostSuccessTupleOf( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2) { - return create(CompletableFutureUtils.mostSuccessTupleOf(cffuScreenedExecutor(cffuExecutor), timeout, unit, cf1, cf2)); + return create(CompletableFutureUtils.mostSuccessTupleOf(cffuScreened(cffuExecutor), timeout, unit, cf1, cf2)); } /** @@ -1032,7 +1037,8 @@ public Cffu> mostSuccessTupleOf( public Cffu> mostSuccessTupleOf( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3) { - return create(CompletableFutureUtils.mostSuccessTupleOf(cffuScreenedExecutor(cffuExecutor), timeout, unit, cf1, cf2, cf3)); + return create(CompletableFutureUtils.mostSuccessTupleOf( + cffuScreened(cffuExecutor), timeout, unit, cf1, cf2, cf3)); } /** @@ -1047,7 +1053,8 @@ public Cffu> mostSuccessTupleOf( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4) { - return create(CompletableFutureUtils.mostSuccessTupleOf(cffuScreenedExecutor(cffuExecutor), timeout, unit, cf1, cf2, cf3, cf4)); + return create(CompletableFutureUtils.mostSuccessTupleOf( + cffuScreened(cffuExecutor), timeout, unit, cf1, cf2, cf3, cf4)); } /** @@ -1062,7 +1069,8 @@ public Cffu> mostSuccessTupleOf( long timeout, TimeUnit unit, CompletionStage cf1, CompletionStage cf2, CompletionStage cf3, CompletionStage cf4, CompletionStage cf5) { - return create(CompletableFutureUtils.mostSuccessTupleOf(cffuScreenedExecutor(cffuExecutor), timeout, unit, cf1, cf2, cf3, cf4, cf5)); + return create(CompletableFutureUtils.mostSuccessTupleOf( + cffuScreened(cffuExecutor), timeout, unit, cf1, cf2, cf3, cf4, cf5)); } /** @@ -1244,7 +1252,7 @@ public Executor delayedExecutor(long delay, TimeUnit unit) { @Contract(pure = true) public Executor delayedExecutor(long delay, TimeUnit unit, Executor executor) { // NOTE: do NOT translate executor to screened executor; same as CompletableFuture.delayedExecutor - return CompletableFutureUtils.delayedExecutor(delay, unit, cffuUnscreenedExecutor(executor)); + return CompletableFutureUtils.delayedExecutor(delay, unit, cffuUnscreened(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 338803fb..7c9183ab 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactoryBuilder.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactoryBuilder.java @@ -3,7 +3,6 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.foldright.cffu.spi.ExecutorWrapperProvider; import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.VisibleForTesting; import javax.annotation.concurrent.ThreadSafe; import java.util.List; @@ -34,7 +33,7 @@ public final class CffuFactoryBuilder { private volatile boolean forbidObtrudeMethods = false; CffuFactoryBuilder(Executor defaultExecutor) { - this.cffuExecutor = makeCffuExecutorWrapper(defaultExecutor); + this.cffuExecutor = new CffuExecutorWrapper(defaultExecutor); } // endregion @@ -85,27 +84,24 @@ private static CffuFactory _poisonObject() { @Contract(pure = true) static CffuFactory withDefaultExecutor(CffuFactory fac, Executor defaultExecutor) { if (fac.cffuExecutor.original == defaultExecutor) return fac; - else return new CffuFactory(makeCffuExecutorWrapper(defaultExecutor), fac.forbidObtrudeMethods()); - } - - private static CffuExecutorWrapper makeCffuExecutorWrapper(final Executor defaultExecutor) { - assert !(defaultExecutor instanceof CffuExecutorWrapper) : "input defaultExecutor should never be a CffuExecutorWrapper"; - assert !(defaultExecutor instanceof CffuMadeExecutor) : "input defaultExecutor should never be a CffuMadeExecutor"; - - requireNonNull(defaultExecutor, "defaultExecutor is null"); - return new CffuExecutorWrapper(defaultExecutor); + else return new CffuFactory(new CffuExecutorWrapper(defaultExecutor), fac.forbidObtrudeMethods()); } static class CffuExecutorWrapper implements Executor { final Executor original; - @VisibleForTesting final Executor unscreened; - @VisibleForTesting final Executor screened; + @SuppressFBWarnings("CT_CONSTRUCTOR_THROW") private CffuExecutorWrapper(Executor defaultExecutor) { + requireNonNull(defaultExecutor, "defaultExecutor is null"); + if (defaultExecutor instanceof CffuExecutorWrapper) + throw new IllegalArgumentException("input defaultExecutor should never be a CffuExecutorWrapper"); + if (defaultExecutor instanceof CffuMadeExecutor) + throw new IllegalArgumentException("input defaultExecutor should never be a CffuMadeExecutor"); + original = defaultExecutor; - unscreened = wrapExecutorWithProviders(defaultExecutor, defaultExecutor);; + unscreened = wrapExecutorWithProviders(defaultExecutor, defaultExecutor); Executor screenedExecutor = LLCF.screenExecutor(defaultExecutor); if (screenedExecutor == defaultExecutor) screened = unscreened; @@ -114,7 +110,7 @@ private CffuExecutorWrapper(Executor defaultExecutor) { /** * This method is never called directly. Instead, use the related {@link #screened}/{@link #unscreened} executor - * from method {@link #cffuScreenedExecutor(Executor)}/{@link #cffuUnscreenedExecutor(Executor)} respectively. + * from method {@link #cffuScreened(Executor)}/{@link #cffuUnscreened(Executor)} respectively. */ @Override public void execute(Runnable command) { @@ -127,21 +123,24 @@ public String toString() { } } - static Executor cffuScreenedExecutor(Executor executor) { - assert !(executor instanceof CffuMadeExecutor) : "input executor should never be a CffuMadeExecutor"; - + @SuppressWarnings("ConstantValue") + static Executor cffuScreened(Executor executor) { + if (executor == null) return null; if (executor instanceof CffuExecutorWrapper) return ((CffuExecutorWrapper) executor).screened; return wrapExecutorWithProviders(LLCF.screenExecutor(executor), executor); } - static Executor cffuUnscreenedExecutor(Executor executor) { - assert !(executor instanceof CffuMadeExecutor) : "input executor should never be a CffuMadeExecutor"; - + @SuppressWarnings("ConstantValue") + static Executor cffuUnscreened(Executor executor) { + if (executor == null) return null; if (executor instanceof CffuExecutorWrapper) return ((CffuExecutorWrapper) executor).unscreened; return wrapExecutorWithProviders(executor, executor); } private static CffuMadeExecutor wrapExecutorWithProviders(Executor executor, Executor original) { + if (executor instanceof CffuMadeExecutor) + throw new IllegalArgumentException("input executor should never be a CffuMadeExecutor"); + for (ExecutorWrapperProvider provider : EXECUTOR_WRAPPER_PROVIDERS) { Supplier msg = () -> provider + "(class: " + provider.getClass().getName() + ") return null"; executor = requireNonNull(provider.wrap(executor), msg); @@ -157,9 +156,9 @@ private static List loadExecutorWrapperProviders() { } /** - * A class for avoiding re-wrapping. + * A little class to prevent duplicate wrapping of executors. */ - private static class CffuMadeExecutor implements Executor { + private static final class CffuMadeExecutor implements Executor { final Executor wrappedExecutor; final Executor original; diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryBuilderTest.kt b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryBuilderTest.kt new file mode 100644 index 00000000..15e946be --- /dev/null +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryBuilderTest.kt @@ -0,0 +1,33 @@ +package io.foldright.cffu + +import io.foldright.test_utils.testCffuFac +import io.kotest.assertions.throwables.shouldThrowExactly +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.nulls.shouldBeNull +import io.kotest.matchers.shouldBe +import io.kotest.matchers.string.shouldContain +import io.kotest.matchers.string.shouldStartWith + +@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") +class CffuFactoryBuilderTest : FunSpec({ + test("cffu (un)screened") { + CffuFactoryBuilder.cffuScreened(null).shouldBeNull() + CffuFactoryBuilder.cffuUnscreened(null).shouldBeNull() + + shouldThrowExactly { + CffuFactoryBuilder.cffuScreened(testCffuFac.cffuExecutor.screened) + }.message shouldBe "input executor should never be a CffuMadeExecutor" + shouldThrowExactly { + CffuFactoryBuilder.cffuUnscreened(testCffuFac.cffuExecutor.screened) + }.message shouldBe "input executor should never be a CffuMadeExecutor" + + testCffuFac.cffuExecutor.screened.toString().shouldStartWith("CffuMadeExecutor, wrappedExecutor: ") + .shouldContain(" original: ") + testCffuFac.cffuExecutor.unscreened.toString().shouldStartWith("CffuMadeExecutor, wrappedExecutor: ") + .shouldContain(" original: ") + + shouldThrowExactly { + testCffuFac.cffuExecutor.execute(null) + } + } +}) 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 d4a963ad..4bbe956b 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuFactoryTest.java @@ -877,6 +877,7 @@ void test_cffuListToArray() { void test_getter() { assertSame(testExecutor, testCffuFac.defaultExecutor()); assertThat(testCffuFac.cffuExecutor.toString()).startsWith("CffuExecutorWrapper, original: "); + assertSame(testCffuFac.cffuExecutor.screened, testCffuFac.cffuExecutor.unscreened); assertSame(testExecutor, testCffuFac.defaultExecutor()); assertFalse(testCffuFac.forbidObtrudeMethods()); @@ -889,9 +890,16 @@ void test_getter() { assertSame(dummyExecutor, fac2.defaultExecutor()); assertEquals(testCffuFac.forbidObtrudeMethods(), fac2.forbidObtrudeMethods()); - final CffuFactory fac3 = testCffuFac.withDefaultExecutor(fac2.defaultExecutor()); - assertSame(fac2.defaultExecutor(), fac3.defaultExecutor()); - assertEquals(fac2.forbidObtrudeMethods(), fac3.forbidObtrudeMethods()); + assertSame(fac2, fac2.withDefaultExecutor(fac2.defaultExecutor())); + + final CffuFactory facCp = CffuFactory.builder(commonPool()).forbidObtrudeMethods(false).build(); + if (CffuTestHelper.useCommonPool()) assertSame(facCp.cffuExecutor.screened, facCp.cffuExecutor.unscreened); + else assertNotSame(facCp.cffuExecutor.screened, facCp.cffuExecutor.unscreened); + + assertEquals("input defaultExecutor should never be a CffuExecutorWrapper", assertThrowsExactly( + IllegalArgumentException.class, () -> fac2.withDefaultExecutor(fac2.cffuExecutor)).getMessage()); + assertEquals("input defaultExecutor should never be a CffuMadeExecutor", assertThrowsExactly( + IllegalArgumentException.class, () -> fac2.withDefaultExecutor(fac2.cffuExecutor.screened)).getMessage()); } @Test diff --git a/cffu-core/src/test/java/io/foldright/cffu/CffuTestHelper.kt b/cffu-core/src/test/java/io/foldright/cffu/CffuTestHelper.kt index 2640c099..c57645c7 100644 --- a/cffu-core/src/test/java/io/foldright/cffu/CffuTestHelper.kt +++ b/cffu-core/src/test/java/io/foldright/cffu/CffuTestHelper.kt @@ -21,9 +21,10 @@ fun CffuFactory.getUnscreenedExecutor(): Executor = cffuExecutor.unscreened class FooCs(cf: CompletableFuture) : CompletionStage by cf +fun useCommonPool() = ForkJoinPool.getCommonPoolParallelism() > 1 + fun assertIsCfDefaultExecutor(executor: Executor) { - val useCommonPool = ForkJoinPool.getCommonPoolParallelism() > 1 - if (useCommonPool) { + if (useCommonPool()) { executor shouldBeSameInstanceAs ForkJoinPool.commonPool() } else { executor.javaClass.name shouldEndWith "\$ThreadPerTaskExecutor"