Class MoreFutures


  • public class MoreFutures
    extends java.lang.Object
    Utilities to do future programming with Java 8.

    Standards for these utilities:

    • Always allow thrown exceptions, and they should cause futures to complete exceptionally.
    • Always return CompletionStage as a future value.
    • Return CompletableFuture only to the producer of a future value.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  MoreFutures.ExceptionOrResult<T>
      An object that represents either a result or an exceptional termination.
    • Constructor Summary

      Constructors 
      Constructor Description
      MoreFutures()  
    • Constructor Detail

      • MoreFutures

        public MoreFutures()
    • Method Detail

      • get

        public static <T> T get​(java.util.concurrent.CompletionStage<T> future)
                         throws java.lang.InterruptedException,
                                java.util.concurrent.ExecutionException
        Gets the result of the given future.

        This utility is provided so consumers of futures need not even convert to CompletableFuture, an interface that is only suitable for producers of futures.

        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
      • get

        public static <T> T get​(java.util.concurrent.CompletionStage<T> future,
                                long duration,
                                java.util.concurrent.TimeUnit unit)
                         throws java.lang.InterruptedException,
                                java.util.concurrent.ExecutionException,
                                java.util.concurrent.TimeoutException
        Gets the result of the given future.

        This utility is provided so consumers of futures need not even convert to CompletableFuture, an interface that is only suitable for producers of futures.

        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException
      • isDone

        public static boolean isDone​(java.util.concurrent.CompletionStage<?> future)
        Indicates whether the future is done.

        This utility is provided so consumers of futures need not even convert to CompletableFuture, an interface that is only suitable for producers of futures.

      • isCancelled

        public static boolean isCancelled​(java.util.concurrent.CompletionStage<?> future)
        Indicates whether the future is cancelled.

        This utility is provided so consumers of futures need not even convert to CompletableFuture, an interface that is only suitable for producers of futures.

      • supplyAsync

        public static <T> java.util.concurrent.CompletionStage<T> supplyAsync​(ThrowingSupplier<T> supplier,
                                                                              java.util.concurrent.ExecutorService executorService)
        Like CompletableFuture.supplyAsync(Supplier) but for ThrowingSupplier.

        If the ThrowingSupplier throws an exception, the future completes exceptionally.

      • runAsync

        public static java.util.concurrent.CompletionStage<java.lang.Void> runAsync​(ThrowingRunnable runnable,
                                                                                    java.util.concurrent.ExecutorService executorService)
        Like CompletableFuture.runAsync(java.lang.Runnable) but for ThrowingRunnable.

        If the ThrowingRunnable throws an exception, the future completes exceptionally.

      • allAsList

        public static <T> java.util.concurrent.CompletionStage<java.util.List<T>> allAsList​(java.util.Collection<? extends java.util.concurrent.CompletionStage<? extends T>> futures)
        Like CompletableFuture.allOf(java.util.concurrent.CompletableFuture<?>...) but returning the result of constituent futures.