Class LLCF
CompletableFutureUtils class.
In general, you should NEVER use this class unless you understand the underlying logic of CompletableFuture and need to hack it. Because the methods are Low Level, use below the method name convention intentionally:
- methods with
f_prefix means not type-safe, e.g.- return type CompletableFuture that may be a minimal-stage
- forcefully cast to
CompletableFuture<T>from anyCompletableFuture<?>
- methods with
0suffix means no parameter validation, e.g.- no null check
- Author:
- Jerry Lee (oldratlee at gmail dot com)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ExecutorDefault executor of CompletableFuture(NOT including the customized subclasses of CompletableFuture). -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleancompleteCf0(CompletableFuture<? super T> cf, T value, Throwable ex) Completes the given CompletableFuture with the exception (if non-null), otherwise with the value.static <T> CompletableFuture<T> copy0(CompletableFuture<T> cf) Equivalent method ofCompletableFuture.copy()with {Java 8} backwards compatibility.static <T> CompletionStage<T> covariantExceptionally0(CompletionStage<? extends T> stage, Function<Throwable, ? extends T> fn) Safely relax type argument restriction of input CompletionStage when usingCompletionStage.exceptionally(Function).static <T> CompletableFuture<T> f_cast(CompletableFuture<?> cf) Forcefully casts CompletableFuture with the value type, IGNORE the compile-time type check.static <F extends CompletionStage<?>>
Ff_selfTypeDownCast(CompletionStage<?> stage) Forcefully casts a CompletionStage to a subclass, enabling a "self-type" cast.static <T> CompletableFuture<T> f_toCf0(CompletionStage<? extends T> stage) Forcefully converts CompletionStage to CompletableFuture, reuse cf instances as many as possible.static <T> CompletableFuture<T>[]f_toCfArray0(CompletionStage<? extends T>[] stages) Forcefully converts CompletionStage array to CompletableFuture array, reuse cf instances as many as possible.static <T> CompletableFuture<T> f_toCfCopy0(CompletionStage<? extends T> stage) Converts CompletionStage to a CompletableFuture copy.static <T> CompletableFuture<T>[]f_toCfCopyArray0(CompletionStage<? extends T>[] stages) Converts CompletionStage array to a CompletableFuture copy array.static booleanisMinStageCf0(CompletableFuture<?> cf) Checks if the givenCompletableFutureinstance is a minimal-stage.static <T, F extends CompletionStage<? extends T>>
Fpeek0(F cfThis, BiConsumer<? super T, ? super Throwable> action, String where) Peeks the result by executing the given action when the given stage completes.static <T, F extends CompletionStage<? extends T>>
FpeekAsync0(F cfThis, BiConsumer<? super T, ? super Throwable> action, String where, Executor executor) Peeks the result by executing the given action using the supplied executor when the given stage completes.static <T, F extends CompletionStage<?>>
FrelayAsync0(CompletionStage<? extends T> cfThis, Function<? super CompletableFuture<T>, F> relayComputations, Executor executor) Provides the "relay async" way to arrange execution of a new stage's computations that guarantees the execution of the new stage's computations not in the caller thread and minimizes thread switching.static voidsafeAddSuppressedEx(Throwable suppressed, Throwable target) Adds a suppressed exception to a target exception, first unwrapping the target exception if it is a CompletionException or ExecutionException.static ExecutorNull-checks user executor argument and translates uses of commonPool to ASYNC_POOL in case parallelism disabled.static <F extends CompletableFuture<?>>
FswitchExecutorIfTriggersInCfDelayerThread(F cf, Executor executor) Returns a new CompletableFuture which guarantees that the execution of subsequent stage's computations not in the cf delayer thread.static <T> CompletableFuture<T> toNonMinCf0(CompletionStage<? extends T> stage) Converts CompletionStage to non-minimal-stage CompletableFuture, reuse cf instances as many as possible.static <T> CompletableFuture<T>[]toNonMinCfArray0(CompletionStage<? extends T>[] stages) Converts CompletionStage array to a non-minimal-stage CompletableFuture array, reuse cf instances as many as possible.static <T> CompletableFuture<T> toNonMinCfCopy0(CompletionStage<? extends T> stage) Converts CompletionStage to a non-minimal-stage CompletableFuture copy.static <T> CompletableFuture<T>[]toNonMinCfCopyArray0(CompletionStage<? extends T>[] stages) Converts CompletionStage array to a non-minimal-stage CompletableFuture copy array.
-
Field Details
-
ASYNC_POOL
Default executor of CompletableFuture(NOT including the customized subclasses of CompletableFuture). Before Java 25:ForkJoinPool.commonPool()when its parallelism is greater than 1, otherwise an executor using one thread per async task. Since Java 25: alwaysForkJoinPool.commonPool()(ForkJoinPool.asyncCommonPool()raises parallelism from 0 to 2); there is no per-task thread fallback.- See Also:
-
-
Method Details
-
f_cast
Forcefully casts CompletableFuture with the value type, IGNORE the compile-time type check. -
f_toCf0
@Contract(pure=true) public static <T> CompletableFuture<T> f_toCf0(CompletionStage<? extends T> stage) Forcefully converts CompletionStage to CompletableFuture, reuse cf instances as many as possible.CAUTION: This method is NOT type safe! Because reused the CF instance, The returned cf may be a minimal-stage, MUST NOT be written or read(explicitly) (e.g.
CompletableFuture.complete(T)); Otherwise, the caller usage of cf may throw UnsupportedOperationException. -
f_toCfArray0
@Contract(pure=true) public static <T> CompletableFuture<T>[] f_toCfArray0(CompletionStage<? extends T>[] stages) Forcefully converts CompletionStage array to CompletableFuture array, reuse cf instances as many as possible. This method is NOT type safe! For more information, see methodf_toCf0(CompletionStage). -
f_toCfCopy0
@Contract(pure=true) public static <T> CompletableFuture<T> f_toCfCopy0(CompletionStage<? extends T> stage) Converts CompletionStage to a CompletableFuture copy.CAUTION: This method is NOT type safe! Because reused the CF instance, The returned cf may be a minimal-stage, MUST NOT be written or read(explicitly) (e.g.
CompletableFuture.complete(T)); Otherwise, the caller usage of cf may throw UnsupportedOperationException. -
f_toCfCopyArray0
@Contract(pure=true) public static <T> CompletableFuture<T>[] f_toCfCopyArray0(CompletionStage<? extends T>[] stages) Converts CompletionStage array to a CompletableFuture copy array. This method is NOT type safe! For more information, see methodf_toCfCopy0(CompletionStage). -
toNonMinCf0
@Contract(pure=true) public static <T> CompletableFuture<T> toNonMinCf0(CompletionStage<? extends T> stage) Converts CompletionStage to non-minimal-stage CompletableFuture, reuse cf instances as many as possible.CAUTION: because reused the CF instance, if the caller need defensive copy instead of writing it directly, use method
toNonMinCfCopy0(CompletionStage)). -
toNonMinCfArray0
@Contract(pure=true) public static <T> CompletableFuture<T>[] toNonMinCfArray0(CompletionStage<? extends T>[] stages) Converts CompletionStage array to a non-minimal-stage CompletableFuture array, reuse cf instances as many as possible. For more information, see methodtoNonMinCf0(CompletionStage). -
toNonMinCfCopy0
@Contract(pure=true) public static <T> CompletableFuture<T> toNonMinCfCopy0(CompletionStage<? extends T> stage) Converts CompletionStage to a non-minimal-stage CompletableFuture copy. This method is type safe.Implementation Note: The returned instances of calling
copymethods (CompletableFuture.copy()) on minimal-stage instances is still minimal-stage (e.g.minimalCompletionStage().copy(),completedStage().copy()). -
toNonMinCfCopyArray0
@Contract(pure=true) public static <T> CompletableFuture<T>[] toNonMinCfCopyArray0(CompletionStage<? extends T>[] stages) Converts CompletionStage array to a non-minimal-stage CompletableFuture copy array. This method is type safe. For more information, see methodtoNonMinCfCopy0(CompletionStage). -
isMinStageCf0
Checks if the givenCompletableFutureinstance is a minimal-stage.Implementation Note: While minimal-stage is implemented as a private subclass of CompletableFuture, the CompletableFuture API consistently uses CompletionStage type for minimal-stage instances and reserves CompletableFuture type for non-minimal-stage instances only.
This contract for minimal-stage type MUST be followed for end users APIs.
-
f_selfTypeDownCast
@Contract(pure=true) public static <F extends CompletionStage<?>> F f_selfTypeDownCast(CompletionStage<?> stage) Forcefully casts a CompletionStage to a subclass, enabling a "self-type" cast. To maintain generic type safety, the generic type argument of the input stage and the return value should be identical. For more on the "self-type", see "Item 2: Consider a builder when faced with many constructor parameters" in "Effective Java, Third Edition".Example codes:
CompletionStage<? extends T> input1 = ...; CompletableFuture<? extends T> output1 = f_selfTypeDownCast(input1); CompletionStage<?> input2 = ...; Cffu<?> output2 = f_selfTypeDownCast(input2); -
peek0
@Contract("_, _, _ -> param1") public static <T, F extends CompletionStage<? extends T>> F peek0(F cfThis, BiConsumer<? super T, ? super Throwable> action, String where) Peeks the result by executing the given action when the given stage completes. This method is guaranteed to return the given stage without modifying it; The uncaught exceptions thrown by the action are reported.CAUTION: Since this method returns the input stage directly, the execution order between the given action and other actions added to the input stage cannot be guaranteed. The action should be treated as "fire and forget" - do not make any assumptions about timing or execution sequence.
- See Also:
-
peekAsync0
@Contract("_, _, _, _ -> param1") public static <T, F extends CompletionStage<? extends T>> F peekAsync0(F cfThis, BiConsumer<? super T, ? super Throwable> action, String where, Executor executor) Peeks the result by executing the given action using the supplied executor when the given stage completes. This method is guaranteed to return the given stage without modifying it; The uncaught exceptions thrown by the action are reported.CAUTION: Since this method returns the input stage directly, the execution order between the given action and other actions added to the input stage cannot be guaranteed. The action should be treated as "fire and forget" - do not make any assumptions about timing or execution sequence.
- See Also:
-
completeCf0
public static <T> boolean completeCf0(CompletableFuture<? super T> cf, @Nullable T value, @Nullable Throwable ex) Completes the given CompletableFuture with the exception (if non-null), otherwise with the value. In general, you should NEVER use this method in application codes, useCompletableFuture.complete(Object)orCompletableFuture.completeExceptionally(Throwable)instead. -
copy0
Equivalent method ofCompletableFuture.copy()with {Java 8} backwards compatibility.Implementation Note: The returned instances of calling
copy/thenApplymethods (CompletableFuture.copy()) on minimal-stage instances is still minimal-stage (e.g.minimalCompletionStage().copy(),completedStage().thenApply(...)).- See Also:
-
covariantExceptionally0
public static <T> CompletionStage<T> covariantExceptionally0(CompletionStage<? extends T> stage, Function<Throwable, ? extends T> fn) Safely relax type argument restriction of input CompletionStage when usingCompletionStage.exceptionally(Function).Example code:
CompletionStage<? extend T> stage = ...; Function<Throwable, ? extends T> fn = ...; CompletionStage<T> s1 = stage.exceptionally(fn); // compile ERROR CompletionStage<T> s2 = covariantExceptionally0(stage, fn); // compile success -
relayAsync0
public static <T, F extends CompletionStage<?>> F relayAsync0(CompletionStage<? extends T> cfThis, Function<? super CompletableFuture<T>, F> relayComputations, Executor executor) Provides the "relay async" way to arrange execution of a new stage's computations that guarantees the execution of the new stage's computations not in the caller thread and minimizes thread switching.In
CompletionStage(including subclassCompletableFuture), execution of a new stage's computations may be arranged in any of three ways:- "default execution",
- default "asynchronous execution" (using methods with suffix async that employ the stage's default asynchronous execution facility),
- or custom "asynchronous execution" (via a supplied Executor).
CompletionStageThis
relayAsync0method introduces the fourth way "relay async" to arrange execution of a new stage's computations:- If input cf is COMPLETED when computations execute, use "asynchronous execution" way (via supplied Executor); Guarantee that a new stage's computations won't block the sequential codes of caller.
- Otherwise, use the "default execution" way; Save one thread switching.
CAUTION: Because one more thread switching generally won't lead to performance problems and using "asynchronous execution" (methods with suffix async) is simpler, make wise use of "relay async" way when necessary.
More info about the "relay async" way (including more description and example codes) see
RelayAsyncDescriptionAndExample.java- Parameters:
cfThis- the input stage (including CompletableFuture)relayComputations- the computations to be arranged after the input stageexecutor- the executor used for asynchronous execution- Returns:
- the return value of function
relayComputations
-
switchExecutorIfTriggersInCfDelayerThread
@Contract(pure=true) public static <F extends CompletableFuture<?>> F switchExecutorIfTriggersInCfDelayerThread(F cf, Executor executor) Returns a new CompletableFuture which guarantees that the execution of subsequent stage's computations not in the cf delayer thread.- Parameters:
executor- used to trigger subsequent stage's computations if input CompletableFuture is trigger in cf delayer thread
-
safeAddSuppressedEx
Adds a suppressed exception to a target exception, first unwrapping the target exception if it is a CompletionException or ExecutionException.Unwrapping the target exception is necessary to ensures suppressed exceptions are properly preserved. Because CompletableFuture internally wraps exceptions in CompletionException / ExecutionException, which can later be unwrapped and discarded during CompletableFuture processing (e.g.
CompletableFuture.exceptionNow()), any suppressed exceptions attached to the wrapper are potentially lost.- Parameters:
suppressed- the exception to be added as a suppressed exception. If null, no action is takentarget- the target exception to add the suppressed exception to- See Also:
-
screenExecutor
-