Package com.ocient.util
Class CompletableFutures
java.lang.Object
com.ocient.util.CompletableFutures
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classCompletableFutures.FailedResult<T,E extends Exception> A Result that represents a failed operation.static interfaceA specialization of theCompletableFutures.Resultinterface that represents the result of an I/O operation.static interfaceCompletableFutures.Result<T,E extends Exception> A Result represents the result of a operation that can either succeed with a value of type T, or fail with a SQLException.static interfaceA specialization of theCompletableFutures.Resultinterface that represents the result of executing a SQL query.static classCompletableFutures.SuccessResult<T,E extends Exception> A Result that represents a successful operation T. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TblockingGet(CompletableFuture<T> future) Returns the result value when complete.static <T> TblockingGet(CompletableFuture<T> future, long timeout, TimeUnit unit) Returns the result value when complete.static <T> TblockingGet(CompletionStage<T> stage) static <T> CompletableFutures.IOResult<T> Blocks until the specifiedCompletionStagecompletes and returns its result as anCompletableFutures.IOResultinstance.static <T,E extends Exception>
CompletableFutures.Result<T, E> blockingGetResult(CompletionStage<? extends CompletableFutures.Result<T, E>> stage, Function<Throwable, E> onMarshal) Blocks until the specifiedCompletionStagecompletes and returns its result as aCompletableFutures.Resultinstance.static <T> CompletableFutures.SQLResult<T> Blocks until the specifiedCompletionStagecompletes and returns its result as aCompletableFutures.SQLResultinstance.static <T> TransportResult<T> Blocks until the specifiedCompletionStagecompletes and returns its result as anTransportResultinstance.static <T,E extends Exception>
CompletableFutures.Result<T, E> fail(E e) Returns a new instance ofCompletableFutures.Resultthat represents a failed computation with the specified exception.static SQLExceptionMarshals a givenThrowableinto anSQLException.static <E extends Exception>
EmarshalThrowable(Throwable e, Class<? extends E> clazz, Function<Throwable, E> f) Marshals a givenThrowableinto anE.static CompletionStage<Void> Returns a future that will complete after the specified amount of time.static <T,E extends Exception>
CompletableFutures.Result<T, E> success(T item) Returns a new instance ofCompletableFutures.Resultthat represents a successful computation with the specified result.static ThrowableunwrapThrowable(Throwable t, Class<?>... clazz) Unwraps theThrowablet, returning the underlying root cause.static CompletableFuture<Void>
-
Method Details
-
voidfuture
-
success
Returns a new instance ofCompletableFutures.Resultthat represents a successful computation with the specified result.- Type Parameters:
T- the type of the resultE- the type of the exception that may be thrown during the computation- Parameters:
item- the result of the computation- Returns:
- a new instance of
Resultthat represents a successful computation with the specified result
-
fail
Returns a new instance ofCompletableFutures.Resultthat represents a failed computation with the specified exception.- Type Parameters:
T- the type of the resultE- the type of the exception that caused the computation to fail- Parameters:
e- the exception that caused the computation to fail- Returns:
- a new instance of
Resultthat represents a failed computation with the specified exception
-
marshalSQLException
Marshals a givenThrowableinto anSQLException. If the givenThrowableis already anSQLException, it is returned as-is. Otherwise, a newSQLExceptionis created with the givenThrowableas its cause.- Parameters:
e- theThrowableto marshal into anSQLException- Returns:
- the
SQLExceptionmarshaled from the givenThrowable
-
marshalThrowable
public static <E extends Exception> E marshalThrowable(Throwable e, Class<? extends E> clazz, Function<Throwable, E> f) Marshals a givenThrowableinto anE. If the givenThrowableis already anE, it is returned as-is. Otherwise, a newEis created with the givenThrowableas its cause.- Parameters:
e- theThrowableto marshal into anException- Returns:
- the
Exceptionmarshaled from the givenThrowable
-
unwrapThrowable
Unwraps theThrowablet, returning the underlying root cause.Returns
tif:tis not wrapped in aclazzthrowable- OR no root cause is found
- Parameters:
t- the throwable to unwrapclazz- the class representing the wrapping throwable type- Returns:
- the unwrapped
-
blockingGet
public static <T> T blockingGet(CompletableFuture<T> future, long timeout, TimeUnit unit) throws IOException, SQLException Returns the result value when complete. If the future completes exceptionally, anIOExceptionis thrown.- Type Parameters:
T- the result type- Parameters:
future- the futuretimeout- the timeout valueunit- the timeout unit- Returns:
- the result
- Throws:
IOExceptionCompletionExceptionCancellationExceptionSQLException
-
blockingGet
- Throws:
IOExceptionSQLException
-
blockingGet
Returns the result value when complete. If the future completes exceptionally, anIOExceptionis thrown.- Type Parameters:
T- the result type- Parameters:
future- the future- Returns:
- the result
- Throws:
IOExceptionCompletionExceptionCancellationExceptionSQLException
-
blockingGetResult
public static <T,E extends Exception> CompletableFutures.Result<T,E> blockingGetResult(CompletionStage<? extends CompletableFutures.Result<T, E>> stage, Function<Throwable, E> onMarshal) Blocks until the specifiedCompletionStagecompletes and returns its result as aCompletableFutures.Resultinstance. If theCompletionStageresults in an exception, the exception is propagated as the cause of a failed result.- Type Parameters:
T- the type of the resultE- the type of the exception- Parameters:
stage- theCompletionStageto block ononMarshal- a function that takes the caught exception and returns an instance ofE- Returns:
- the result of the
CompletionStageas aResultinstance, or a failedResultinstance if the completion of theCompletionStageresulted in an exception
-
blockingGetSQLResult
public static <T> CompletableFutures.SQLResult<T> blockingGetSQLResult(CompletionStage<CompletableFutures.SQLResult<T>> stage) Blocks until the specifiedCompletionStagecompletes and returns its result as aCompletableFutures.SQLResultinstance. If the completion of theCompletionStageresults in an exception, the exception is caught and used as the cause of a failedSQLResultinstance.- Type Parameters:
T- the type of the result- Parameters:
stage- theCompletionStageto block on- Returns:
- the result of the
CompletionStageas aSQLResultinstance, or a failedSQLResultinstance if the completion of theCompletionStageresulted in an exception
-
blockingGetIOResult
public static <T> CompletableFutures.IOResult<T> blockingGetIOResult(CompletionStage<CompletableFutures.IOResult<T>> stage) Blocks until the specifiedCompletionStagecompletes and returns its result as anCompletableFutures.IOResultinstance. If the completion of theCompletionStageresults in an exception, the exception is caught and used as the cause of a failedIOResultinstance.- Type Parameters:
T- the type of the result- Parameters:
stage- theCompletionStageto block on- Returns:
- the result of the
CompletionStageas anIOResultinstance, or a failedIOResultinstance if the completion of theCompletionStageresulted in an exception
-
blockingGetTransportResult
public static <T> TransportResult<T> blockingGetTransportResult(CompletionStage<TransportResult<T>> stage) Blocks until the specifiedCompletionStagecompletes and returns its result as anTransportResultinstance. If the completion of theCompletionStageresults in an exception, the exception is caught and used as the cause of a failedTransportResultinstance.- Type Parameters:
T- the type of the result- Parameters:
stage- theCompletionStageto block on- Returns:
- the result of the
CompletionStageas anTransportResultinstance, or a failedTransportResultinstance if the completion of theCompletionStageresulted in an exception
-
sleep
Returns a future that will complete after the specified amount of time.
-