Class CompletableFutureUtils
java.lang.Object
dev.langchain4j.internal.CompletableFutureUtils
Utility methods for working with
CompletableFuture.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> CompletableFuture<T> failOnThrow(Object target, String method, Supplier<CompletableFuture<T>> operation) LikefailOnThrow(Supplier), but anullfuture fails with anIllegalStateExceptionthat names the method that returned it: the class name oftarget, thenmethod, for example"<class name of target>.doChatAsync(ChatRequest) returned null".static <T> CompletableFuture<T> failOnThrow(Supplier<CompletableFuture<T>> operation) Calls an asynchronous operation so that the caller never sees it throw, which is the rule for every method that returns aCompletableFuture: anything the operation throws, including anError, is returned as a failed future, and anullfuture as a future failed with anIllegalStateException.static voidpropagateCancellation(CompletableFuture<?> from, CompletableFuture<?> to) Forwards cancellation from one future to another.
-
Method Details
-
propagateCancellation
Forwards cancellation from one future to another.CompletableFuture.cancel(boolean)does not propagate to the stages a future was derived from, so whenfromrepresents the future handed to a caller andtorepresents the underlying work (e.g. an in-flight HTTP request), cancellingfromwould otherwise leavetorunning. This method bridges that gap: oncefromcompletes, if it was cancelled,tois cancelled too.- Parameters:
from- the future whose cancellation should be propagated; ignored ifnull.to- the future to cancel whenfromis cancelled.
-
failOnThrow
Calls an asynchronous operation so that the caller never sees it throw, which is the rule for every method that returns aCompletableFuture: anything the operation throws, including anError, is returned as a failed future, and anullfuture as a future failed with anIllegalStateException.Use
failOnThrow(Object, String, Supplier)when the operation can returnnull, so that the message names the method that did.- Parameters:
operation- the operation that returns the future- Returns:
- the future returned by the operation, or a failed future
-
failOnThrow
public static <T> CompletableFuture<T> failOnThrow(Object target, String method, Supplier<CompletableFuture<T>> operation) LikefailOnThrow(Supplier), but anullfuture fails with anIllegalStateExceptionthat names the method that returned it: the class name oftarget, thenmethod, for example"<class name of target>.doChatAsync(ChatRequest) returned null".- Parameters:
target- the object whose method the operation callsmethod- the method the operation calls, for example"doChatAsync(ChatRequest)"operation- the operation that returns the future- Returns:
- the future returned by the operation, or a failed future
-