Class CompletableFutures


  • public class CompletableFutures
    extends java.lang.Object
    Utility methods connecting CompletableFuture futures.
    Since:
    8.0
    Author:
    Dan Berindei
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.concurrent.CompletableFuture[] EMPTY_ARRAY  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.concurrent.CompletionException asCompletionException​(java.lang.Throwable t)  
      static boolean await​(java.util.concurrent.CompletableFuture<?> future, long time, java.util.concurrent.TimeUnit unit)
      It waits until the CompletableFuture is completed.
      static <T> T await​(java.util.concurrent.CompletableFuture<T> future)
      Wait for a long time until the CompletableFuture is completed.
      static java.util.concurrent.CompletionStage<java.lang.Boolean> booleanStage​(boolean trueOrFalse)  
      static <K,​V>
      java.util.concurrent.CompletableFuture<java.util.Map<K,​V>>
      completedEmptyMap()  
      static <T> java.util.concurrent.CompletableFuture<T> completedExceptionFuture​(java.lang.Throwable ex)  
      static java.util.concurrent.CompletableFuture<java.lang.Boolean> completedFalse()  
      static <T> java.util.concurrent.CompletableFuture<T> completedNull()  
      static java.util.concurrent.CompletableFuture<java.lang.Boolean> completedTrue()  
      static <T> java.util.function.Function<T,​java.util.concurrent.CompletionStage<java.lang.Boolean>> composeFalse()  
      static <T,​R>
      java.util.function.Function<T,​java.util.concurrent.CompletionStage<R>>
      composeNull()  
      static <T> java.util.function.Function<T,​java.util.concurrent.CompletionStage<java.lang.Boolean>> composeTrue()  
      static java.lang.Throwable extractException​(java.lang.Throwable t)  
      static void rethrowException​(java.lang.Throwable t)  
      static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> sequence​(java.util.List<java.util.concurrent.CompletableFuture<T>> futures)  
      static <T,​R>
      java.util.function.Function<T,​R>
      toNullFunction()  
      static boolean uncheckedAwait​(java.util.concurrent.CompletableFuture<?> future, long time, java.util.concurrent.TimeUnit unit)
      Same as await(CompletableFuture, long, TimeUnit) but wraps checked exceptions in CacheException
      static <T> T uncheckedAwait​(java.util.concurrent.CompletableFuture<T> future)
      Same as await(CompletableFuture) but wraps checked exceptions in CacheException
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_ARRAY

        public static final java.util.concurrent.CompletableFuture[] EMPTY_ARRAY
    • Constructor Detail

      • CompletableFutures

        public CompletableFutures()
    • Method Detail

      • completedEmptyMap

        public static <K,​V> java.util.concurrent.CompletableFuture<java.util.Map<K,​V>> completedEmptyMap()
      • completedNull

        public static <T> java.util.concurrent.CompletableFuture<T> completedNull()
      • completedTrue

        public static java.util.concurrent.CompletableFuture<java.lang.Boolean> completedTrue()
      • completedFalse

        public static java.util.concurrent.CompletableFuture<java.lang.Boolean> completedFalse()
      • composeTrue

        public static <T> java.util.function.Function<T,​java.util.concurrent.CompletionStage<java.lang.Boolean>> composeTrue()
      • composeFalse

        public static <T> java.util.function.Function<T,​java.util.concurrent.CompletionStage<java.lang.Boolean>> composeFalse()
      • composeNull

        public static <T,​R> java.util.function.Function<T,​java.util.concurrent.CompletionStage<R>> composeNull()
      • booleanStage

        public static java.util.concurrent.CompletionStage<java.lang.Boolean> booleanStage​(boolean trueOrFalse)
      • sequence

        public static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> sequence​(java.util.List<java.util.concurrent.CompletableFuture<T>> futures)
      • completedExceptionFuture

        public static <T> java.util.concurrent.CompletableFuture<T> completedExceptionFuture​(java.lang.Throwable ex)
      • await

        public static boolean await​(java.util.concurrent.CompletableFuture<?> future,
                                    long time,
                                    java.util.concurrent.TimeUnit unit)
                             throws java.lang.InterruptedException
        It waits until the CompletableFuture is completed.

        It ignore if the CompletableFuture is completed normally or exceptionally.

        Parameters:
        future - the CompletableFuture to test.
        time - the timeout.
        unit - the timeout unit.
        Returns:
        true if completed, false if timed out.
        Throws:
        java.lang.InterruptedException - if interrupted while waiting.
        java.lang.NullPointerException - if future or unit is null.
      • uncheckedAwait

        public static boolean uncheckedAwait​(java.util.concurrent.CompletableFuture<?> future,
                                             long time,
                                             java.util.concurrent.TimeUnit unit)
        Same as await(CompletableFuture, long, TimeUnit) but wraps checked exceptions in CacheException
        Parameters:
        future - the CompletableFuture to test.
        time - the timeout.
        unit - the timeout unit.
        Returns:
        true if completed, false if timed out.
        Throws:
        java.lang.NullPointerException - if future or unit is null.
      • await

        public static <T> T await​(java.util.concurrent.CompletableFuture<T> future)
                           throws java.util.concurrent.ExecutionException,
                                  java.lang.InterruptedException
        Wait for a long time until the CompletableFuture is completed.
        Type Parameters:
        T - the return type.
        Parameters:
        future - the CompletableFuture.
        Returns:
        the result value.
        Throws:
        java.util.concurrent.ExecutionException - if the CompletableFuture completed exceptionally.
        java.lang.InterruptedException - if the current thread was interrupted while waiting.
      • uncheckedAwait

        public static <T> T uncheckedAwait​(java.util.concurrent.CompletableFuture<T> future)
        Same as await(CompletableFuture) but wraps checked exceptions in CacheException
        Type Parameters:
        T - the return type.
        Parameters:
        future - the CompletableFuture.
        Returns:
        the result value.
      • asCompletionException

        public static java.util.concurrent.CompletionException asCompletionException​(java.lang.Throwable t)
      • rethrowException

        public static void rethrowException​(java.lang.Throwable t)
      • extractException

        public static java.lang.Throwable extractException​(java.lang.Throwable t)
      • toNullFunction

        public static <T,​R> java.util.function.Function<T,​R> toNullFunction()