Skip to content

Commit

Permalink
refactor: add more @Contract QA annotations for LLCF 📝
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jan 27, 2025
1 parent dd8841f commit 8d1cecf
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cffu-core/src/main/java/io/foldright/cffu/LLCF.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> CompletableFuture<T> f_cast(CompletableFuture<?> cf) {
return (CompletableFuture) cf;
Expand All @@ -59,6 +60,7 @@ public static <T> CompletableFuture<T> 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 <T> CompletableFuture<T> f_toCf0(CompletionStage<? extends T> s) {
if (s instanceof CompletableFuture) return (CompletableFuture<T>) s;
Expand All @@ -70,6 +72,7 @@ public static <T> CompletableFuture<T> f_toCf0(CompletionStage<? extends T> 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 <T> CompletableFuture<T>[] f_toCfArray0(CompletionStage<? extends T>[] stages) {
return mapArray(stages, CompletableFuture[]::new, LLCF::f_toCf0);
}
Expand All @@ -85,6 +88,7 @@ public static <T> CompletableFuture<T>[] f_toCfArray0(CompletionStage<? extends
* ({@link CompletableFuture#copy}) on minimal-stage instances is still minimal-stage
* (e.g. {@code minimalCompletionStage().copy()}, {@code completedStage().copy()})
*/
@Contract(pure = true)
public static <T> CompletableFuture<T> f_toCfCopy0(CompletionStage<? extends T> s) {
final CompletableFuture<T> f = f_toCf0(s);
return IS_JAVA9_PLUS ? f.copy() : f.thenApply(x -> x);
Expand All @@ -94,6 +98,7 @@ public static <T> CompletableFuture<T> f_toCfCopy0(CompletionStage<? extends T>
* 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 <T> CompletableFuture<T>[] f_toCfCopyArray0(CompletionStage<? extends T>[] stages) {
return mapArray(stages, CompletableFuture[]::new, LLCF::f_toCfCopy0);
}
Expand All @@ -104,6 +109,7 @@ public static <T> CompletableFuture<T>[] f_toCfCopyArray0(CompletionStage<? exte
* <strong>CAUTION:</strong> 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 <T> CompletableFuture<T> toNonMinCf0(CompletionStage<? extends T> s) {
final CompletableFuture<T> f = f_toCf0(s);
return isMinStageCf(f) ? f.toCompletableFuture() : f;
Expand All @@ -113,6 +119,7 @@ public static <T> CompletableFuture<T> toNonMinCf0(CompletionStage<? extends T>
* 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 <T> CompletableFuture<T>[] toNonMinCfArray0(CompletionStage<? extends T>[] stages) {
return mapArray(stages, CompletableFuture[]::new, LLCF::toNonMinCf0);
}
Expand All @@ -124,6 +131,7 @@ public static <T> CompletableFuture<T>[] toNonMinCfArray0(CompletionStage<? exte
* ({@link CompletableFuture#copy}) on minimal-stage instances is still minimal-stage
* (e.g. {@code minimalCompletionStage().copy()}, {@code completedStage().copy()}).
*/
@Contract(pure = true)
public static <T> CompletableFuture<T> toNonMinCfCopy0(CompletionStage<? extends T> s) {
final CompletableFuture<T> f = f_toCf0(s);
return isMinStageCf(f) ? f.toCompletableFuture() : IS_JAVA9_PLUS ? f.copy() : f.thenApply(x -> x);
Expand All @@ -133,6 +141,7 @@ public static <T> CompletableFuture<T> toNonMinCfCopy0(CompletionStage<? extends
* Converts CompletionStage array to a non-minimal-stage CompletableFuture copy array. This method is type safe.
* More info see method {@link #toNonMinCfCopy0(CompletionStage)}.
*/
@Contract(pure = true)
public static <T> CompletableFuture<T>[] toNonMinCfCopyArray0(CompletionStage<? extends T>[] stages) {
return mapArray(stages, CompletableFuture[]::new, LLCF::toNonMinCfCopy0);
}
Expand All @@ -146,6 +155,7 @@ public static <T> CompletableFuture<T>[] toNonMinCfCopyArray0(CompletionStage<?
* <p>
* 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);
}
Expand Down Expand Up @@ -213,6 +223,7 @@ public static <T> boolean completeCf0(CompletableFuture<? super T> 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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8d1cecf

Please sign in to comment.