Package io.atomix.utils.concurrent
Class Futures
- java.lang.Object
-
- io.atomix.utils.concurrent.Futures
-
public final class Futures extends Object
Utilities for creating completed and exceptional futures.- Author:
- Jordan Halterman
-
-
Constructor Summary
Constructors Constructor Description Futures()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> CompletableFuture<List<T>>allOf(List<CompletableFuture<T>> futures)Returns a new CompletableFuture completed with a list of computed values when all of the given CompletableFuture complete.static <T> CompletableFuture<T>allOf(List<CompletableFuture<T>> futures, BinaryOperator<T> reducer, T emptyValue)Returns a new CompletableFuture completed by reducing a list of computed values when all of the given CompletableFuture complete.static <T> CompletableFuture<Stream<T>>allOf(Stream<CompletableFuture<T>> futures)Returns a new CompletableFuture completed with a list of computed values when all of the given CompletableFuture complete.static <T> CompletableFuture<T>asyncFuture(CompletableFuture<T> future, Executor executor)Returns a wrapped future that will be completed on the given executor.static <T> CompletableFuture<T>completedFuture(T result)Creates a future that is synchronously completed.static <T> CompletableFuture<T>completedFutureAsync(T result, Executor executor)Creates a future that is asynchronously completed.static <T> CompletableFuture<T>exceptionalFuture(Throwable t)Creates a future that is synchronously completed exceptionally.static <T> CompletableFuture<T>exceptionalFutureAsync(Throwable t, Executor executor)Creates a future that is asynchronously completed exceptionally.static <T> Tget(Future<T> future)Gets a future result with a default timeout.static <T> Tget(Future<T> future, long timeout, TimeUnit timeUnit)Gets a future result with a default timeout.static <T> CompletableFuture<T>orderedFuture()Returns a future that completes callbacks in add order.static <T> CompletableFuture<T>orderedFuture(CompletableFuture<T> future)Returns a future that completes callbacks in add order.
-
-
-
Method Detail
-
get
public static <T> T get(Future<T> future)
Gets a future result with a default timeout.- Type Parameters:
T- the future result type- Parameters:
future- the future to block- Returns:
- the future result
- Throws:
RuntimeException- if a future exception occurs
-
get
public static <T> T get(Future<T> future, long timeout, TimeUnit timeUnit)
Gets a future result with a default timeout.- Type Parameters:
T- the future result type- Parameters:
future- the future to blocktimeout- the future timeouttimeUnit- the future timeout time unit- Returns:
- the future result
- Throws:
RuntimeException- if a future exception occurs
-
completedFuture
public static <T> CompletableFuture<T> completedFuture(T result)
Creates a future that is synchronously completed.- Parameters:
result- The future result.- Returns:
- The completed future.
-
completedFutureAsync
public static <T> CompletableFuture<T> completedFutureAsync(T result, Executor executor)
Creates a future that is asynchronously completed.- Parameters:
result- The future result.executor- The executor on which to complete the future.- Returns:
- The completed future.
-
exceptionalFuture
public static <T> CompletableFuture<T> exceptionalFuture(Throwable t)
Creates a future that is synchronously completed exceptionally.- Parameters:
t- The future exception.- Returns:
- The exceptionally completed future.
-
exceptionalFutureAsync
public static <T> CompletableFuture<T> exceptionalFutureAsync(Throwable t, Executor executor)
Creates a future that is asynchronously completed exceptionally.- Parameters:
t- The future exception.executor- The executor on which to complete the future.- Returns:
- The exceptionally completed future.
-
orderedFuture
public static <T> CompletableFuture<T> orderedFuture()
Returns a future that completes callbacks in add order.- Type Parameters:
T- future value type- Returns:
- a new completable future that will complete added callbacks in the order in which they were added
-
orderedFuture
public static <T> CompletableFuture<T> orderedFuture(CompletableFuture<T> future)
Returns a future that completes callbacks in add order.- Type Parameters:
T- future value type- Returns:
- a new completable future that will complete added callbacks in the order in which they were added
-
asyncFuture
public static <T> CompletableFuture<T> asyncFuture(CompletableFuture<T> future, Executor executor)
Returns a wrapped future that will be completed on the given executor.- Type Parameters:
T- the future value type- Parameters:
future- the future to be completed on the given executorexecutor- the executor with which to complete the future- Returns:
- a wrapped future to be completed on the given executor
-
allOf
public static <T> CompletableFuture<Stream<T>> allOf(Stream<CompletableFuture<T>> futures)
Returns a new CompletableFuture completed with a list of computed values when all of the given CompletableFuture complete.- Type Parameters:
T- value type of CompletableFuture- Parameters:
futures- the CompletableFutures- Returns:
- a new CompletableFuture that is completed when all of the given CompletableFutures complete
-
allOf
public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futures)
Returns a new CompletableFuture completed with a list of computed values when all of the given CompletableFuture complete.- Type Parameters:
T- value type of CompletableFuture- Parameters:
futures- the CompletableFutures- Returns:
- a new CompletableFuture that is completed when all of the given CompletableFutures complete
-
allOf
public static <T> CompletableFuture<T> allOf(List<CompletableFuture<T>> futures, BinaryOperator<T> reducer, T emptyValue)
Returns a new CompletableFuture completed by reducing a list of computed values when all of the given CompletableFuture complete.- Type Parameters:
T- value type of CompletableFuture- Parameters:
futures- the CompletableFuturesreducer- reducer for computing the resultemptyValue- zero value to be returned if the input future list is empty- Returns:
- a new CompletableFuture that is completed when all of the given CompletableFutures complete
-
-