Class Futures


  • public final class Futures
    extends Object
    Utilities for creating completed and exceptional futures.
    Author:
    Jordan Halterman
    • Constructor Detail

      • Futures

        public Futures()
    • 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 block
        timeout - the future timeout
        timeUnit - 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 executor
        executor - 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 CompletableFutures
        reducer - reducer for computing the result
        emptyValue - 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