From 3d833c6e9baa53702553cad40575c339656873a7 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sun, 2 Jun 2024 17:07:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20change=20parameter=20type=20to=20`C?= =?UTF-8?q?ompletableFuture`=20of=20internal=20helper=20method=20`isMinSta?= =?UTF-8?q?geCf`=20=F0=9F=A7=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit safer because only subclass of `CompletableFuture` can be `minimal stage` --- .../main/java/io/foldright/cffu/CffuFactory.java | 8 +++----- .../io/foldright/cffu/CompletableFutureUtils.java | 14 ++++++-------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java index 0a761511..83ad58c8 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java @@ -266,11 +266,9 @@ public Cffu newIncompleteCffu() { @Contract(pure = true) public Cffu toCffu(CompletionStage stage) { requireNonNull(stage, "stage is null"); - - if (CompletableFutureUtils.isMinStageCf(stage)) { - return createMin((CompletableFuture) stage); - } else if (stage instanceof CompletableFuture) { - return create((CompletableFuture) stage); + if (stage instanceof CompletableFuture) { + final CompletableFuture cf = (CompletableFuture) stage; + return CompletableFutureUtils.isMinStageCf(cf) ? createMin(cf) : create(cf); } else if (stage instanceof Cffu) { return ((Cffu) stage).resetCffuFactory(this); } diff --git a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java index 08703cb2..89e55c5d 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java +++ b/cffu-core/src/main/java/io/foldright/cffu/CompletableFutureUtils.java @@ -322,8 +322,8 @@ private static void fill(CompletionStage[] css, * IGNORE the compile-time type check. */ @SuppressWarnings({"unchecked", "rawtypes"}) - private static CompletableFuture f_cast(CompletableFuture f) { - return (CompletableFuture) f; + private static CompletableFuture f_cast(CompletableFuture cf) { + return (CompletableFuture) cf; } /** @@ -393,8 +393,8 @@ private static CompletableFuture toNonMinCfCopy(CompletionStage boolean isMinStageCf(CompletionStage s) { - return "java.util.concurrent.CompletableFuture$MinimalStage".equals(s.getClass().getName()); + static boolean isMinStageCf(CompletableFuture cf) { + return "java.util.concurrent.CompletableFuture$MinimalStage".equals(cf.getClass().getName()); } //////////////////////////////////////////////////////////////////////////////// @@ -1480,8 +1480,7 @@ C exceptionallyAsync(C cf, Function fn, Executor executo * @return a new CompletableFuture * @see #cffuOrTimeout(CompletableFuture, Executor, long, TimeUnit) */ - public static > C cffuOrTimeout( - C cf, long timeout, TimeUnit unit) { + public static > C cffuOrTimeout(C cf, long timeout, TimeUnit unit) { return cffuOrTimeout(cf, AsyncPoolHolder.ASYNC_POOL, timeout, unit); } @@ -1900,8 +1899,7 @@ public static CffuState state(Future cf) { * @param supplier a function returning the value to be used to complete given CompletableFuture * @return the given CompletableFuture */ - public static > - C completeAsync(C cf, Supplier supplier) { + public static > C completeAsync(C cf, Supplier supplier) { return completeAsync(cf, supplier, AsyncPoolHolder.ASYNC_POOL); }