From 8d1cecfbf3a23aa4e3f18c048f264aad1ac5a5f3 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Mon, 27 Jan 2025 21:11:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20add=20more=20`@Contract`=20QA=20ann?= =?UTF-8?q?otations=20for=20`LLCF`=20=F0=9F=93=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cffu-core/src/main/java/io/foldright/cffu/LLCF.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cffu-core/src/main/java/io/foldright/cffu/LLCF.java b/cffu-core/src/main/java/io/foldright/cffu/LLCF.java index 55dc847b..22fd46d9 100644 --- a/cffu-core/src/main/java/io/foldright/cffu/LLCF.java +++ b/cffu-core/src/main/java/io/foldright/cffu/LLCF.java @@ -47,6 +47,7 @@ public final class LLCF { /** * Force casts CompletableFuture with the value type, IGNORE the compile-time type check. */ + @Contract(pure = true) @SuppressWarnings({"unchecked", "rawtypes"}) public static CompletableFuture f_cast(CompletableFuture cf) { return (CompletableFuture) cf; @@ -59,6 +60,7 @@ public static CompletableFuture f_cast(CompletableFuture cf) { * may be a minimal-stage, MUST NOT be written or read(explicitly) (e.g. {@link CompletableFuture#complete}); * Otherwise, the caller usage of cf may throw UnsupportedOperationException. */ + @Contract(pure = true) @SuppressWarnings("unchecked") public static CompletableFuture f_toCf0(CompletionStage s) { if (s instanceof CompletableFuture) return (CompletableFuture) s; @@ -70,6 +72,7 @@ public static CompletableFuture f_toCf0(CompletionStage s) { * Force converts CompletionStage array to CompletableFuture array, reuse cf instances as many as possible. * This method is NOT type safe! More info see method {@link #f_toCf0(CompletionStage)}. */ + @Contract(pure = true) public static CompletableFuture[] f_toCfArray0(CompletionStage[] stages) { return mapArray(stages, CompletableFuture[]::new, LLCF::f_toCf0); } @@ -85,6 +88,7 @@ public static CompletableFuture[] f_toCfArray0(CompletionStage CompletableFuture f_toCfCopy0(CompletionStage s) { final CompletableFuture f = f_toCf0(s); return IS_JAVA9_PLUS ? f.copy() : f.thenApply(x -> x); @@ -94,6 +98,7 @@ public static CompletableFuture f_toCfCopy0(CompletionStage * Converts CompletionStage array to a CompletableFuture copy array. This method is NOT type safe! * More info see method {@link #f_toCfCopy0(CompletionStage)}. */ + @Contract(pure = true) public static CompletableFuture[] f_toCfCopyArray0(CompletionStage[] stages) { return mapArray(stages, CompletableFuture[]::new, LLCF::f_toCfCopy0); } @@ -104,6 +109,7 @@ public static CompletableFuture[] f_toCfCopyArray0(CompletionStageCAUTION: because reused the CF instance, if the caller need defensive copy * instead of writing it directly, use method {@link #toNonMinCfCopy0(CompletionStage)}). */ + @Contract(pure = true) public static CompletableFuture toNonMinCf0(CompletionStage s) { final CompletableFuture f = f_toCf0(s); return isMinStageCf(f) ? f.toCompletableFuture() : f; @@ -113,6 +119,7 @@ public static CompletableFuture toNonMinCf0(CompletionStage * Converts CompletionStage array to non-minimal-stage CompletableFuture array, * reuse cf instances as many as possible. More info see method {@link #toNonMinCf0(CompletionStage)}. */ + @Contract(pure = true) public static CompletableFuture[] toNonMinCfArray0(CompletionStage[] stages) { return mapArray(stages, CompletableFuture[]::new, LLCF::toNonMinCf0); } @@ -124,6 +131,7 @@ public static CompletableFuture[] toNonMinCfArray0(CompletionStage CompletableFuture toNonMinCfCopy0(CompletionStage s) { final CompletableFuture f = f_toCf0(s); return isMinStageCf(f) ? f.toCompletableFuture() : IS_JAVA9_PLUS ? f.copy() : f.thenApply(x -> x); @@ -133,6 +141,7 @@ public static CompletableFuture toNonMinCfCopy0(CompletionStage CompletableFuture[] toNonMinCfCopyArray0(CompletionStage[] stages) { return mapArray(stages, CompletableFuture[]::new, LLCF::toNonMinCfCopy0); } @@ -146,6 +155,7 @@ public static CompletableFuture[] toNonMinCfCopyArray0(CompletionStage * This type contract for minimal-stage MUST be followed for end users APIs. */ + @Contract(pure = true) public static boolean isMinStageCf(CompletableFuture cf) { return cf.getClass().equals(MIN_STAGE_CLASS); } @@ -213,6 +223,7 @@ public static boolean completeCf0(CompletableFuture cf, @Nullable /** * Null-checks user executor argument, and translates uses of commonPool to ASYNC_POOL in case parallelism disabled. */ + @Contract(pure = true) @SuppressWarnings("resource") public static Executor screenExecutor(Executor e) { // Implementation note: CompletableFuture API methods already call this method internally; Only underlying @@ -282,7 +293,7 @@ private static boolean methodExists(Supplier methodCallCheck) { * * @see CompletableFutureUtils#defaultExecutor(CompletionStage) */ - // field initialization code is copied from CompletableFuture#ASYNC_POOL with adoption. + // field initialization code is copied from CompletableFuture#ASYNC_POOL with small adoption. public static final Executor ASYNC_POOL; private static final Class MIN_STAGE_CLASS;