Class Futures
- java.lang.Object
-
- com.github.mizool.core.concurrent.Futures
-
public final class Futures extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <V> V
get(@NonNull Future<V> future)
Waits if necessary for the computation to complete, and then retrieves its result.static <V> V
get(@NonNull Future<V> future, @NonNull Duration duration)
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.static com.google.common.util.concurrent.ListenableFuture<Void>
toVoidResult(@NonNull com.google.common.util.concurrent.ListenableFuture<?> future)
Transforms anyListenableFuture
into aListenableFuture
without result.static CompletableFuture<Void>
toVoidResult(@NonNull CompletableFuture<?> future)
Transforms anyCompletableFuture
into aCompletableFuture
without result.
-
-
-
Method Detail
-
get
public static <V> V get(@NonNull @NonNull Future<V> future)
Waits if necessary for the computation to complete, and then retrieves its result.- Returns:
- the computed result
- Throws:
com.google.common.util.concurrent.UncheckedExecutionException
- if the computation threw an exceptionCancellationException
- if the computation was cancelledUncheckedInterruptedException
- if the thread was interrupted while waiting. Note: as required, the thread will be re-interrupted before throwing the exception.NullPointerException
- iffuture
isnull
-
get
public static <V> V get(@NonNull @NonNull Future<V> future, @NonNull @NonNull Duration duration)
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.- Returns:
- the computed result
- Throws:
com.google.common.util.concurrent.UncheckedTimeoutException
- if the computation timed outcom.google.common.util.concurrent.UncheckedExecutionException
- if the computation threw an exceptionCancellationException
- if the computation was cancelledUncheckedInterruptedException
- if the thread was interrupted while waiting. Note: as required, the thread will be re-interrupted before throwing the exception.NullPointerException
- iffuture
orduration
isnull
-
toVoidResult
public static com.google.common.util.concurrent.ListenableFuture<Void> toVoidResult(@NonNull @NonNull com.google.common.util.concurrent.ListenableFuture<?> future)
Transforms anyListenableFuture
into aListenableFuture
without result. Exceptions that are thrown by the original future are handled transparently.
The returned future does not contain a reference to the original future. That allows to reduce the overall memory footprint when working with multiple futures at once without caring for the results.- Parameters:
future
- the future to wrap- Returns:
- a void future
- Throws:
NullPointerException
- iffuture
isnull
-
toVoidResult
public static CompletableFuture<Void> toVoidResult(@NonNull @NonNull CompletableFuture<?> future)
Transforms anyCompletableFuture
into aCompletableFuture
without result. Exceptions that are thrown by the original future are handled transparently.
The returned future does not contain a reference to the original future. That allows to reduce the overall memory footprint when working with multiple futures at once without caring for the results.- Parameters:
future
- the future to wrap- Returns:
- a void future
- Throws:
NullPointerException
- iffuture
isnull
-
-