Class CompletableFuture

  • All Implemented Interfaces:
    java.util.concurrent.Future

    
    public class CompletableFuture<T>
     implements Future<V>
                        

    A stripped-down, Android-21-compatible version of java.util.concurrent.CompletableFuture.

    • Constructor Detail

      • CompletableFuture

        CompletableFuture()
    • Method Detail

      • cancel

         boolean cancel(boolean mayInterruptIfRunning)
      • isDone

         synchronized boolean isDone()
      • get

         synchronized T get()
      • thenApply

         <U> CompletableFuture<U> thenApply(Function<in T, out U> fn)

        Returns a future that will complete with the applied function applied to this future's completion value.

        If this future completes exceptionally, the exception will be propagated to the returned future. If this future completes normally but the applied function throws, the returned future will complete exceptionally with the thrown exception.

        Note: Unlike the standard CompletableFuture implementation, cancellation propagates both downstream and upstream. If this future or the returned future is cancelled, all futures in the chain will be cancelled.

      • thenCompose

         <U> CompletableFuture<U> thenCompose(Function<in T, out CompletableFuture<U>> fn)

        Returns a future that will complete with the completion result of the future produced by applying a function to this future's completion value.

        If this future completes exceptionally, the exception will be propagated to the returned future. If this future completes normally but the applied function throws, the returned future will complete exceptionally with the thrown exception. If the future produced by function application completes exceptionally, its error value will be propagated to the returned future.

        Note: Unlike the standard CompletableFuture implementation, cancellation propagates both downstream and upstream. If this future or the returned future is cancelled, all futures in the chain will be cancelled.

      • whenComplete

         CompletableFuture<T> whenComplete(BiConsumer<in T, Throwable> fn)

        Returns a future of the same type that will execute an action when the original future completes, successfully or not.

        The action will be invoked with (value, null) for successful completion of the source future, and (null, throwable) otherwise. If the source future completes exceptionally, the exception will be propagated to the returned future after executing the provided action. Any exceptions thrown by action itself are ignored in this case. If the source future succeeds but provided action throws an exception, this exception will be used to complete the resulting future exceptionally.

        Note: Unlike the standard CompletableFuture implementation, cancellation propagates both downstream and upstream. If this future or the returned future is cancelled, all futures in the chain will be cancelled.

      • handle

         <U> CompletableFuture<U> handle(BiFunction<in T, Throwable, out U> fn)

        Returns a future that, when this future completes (either normally or exceptionally), is completed with the result of invoking the given function.

        The function receives this future's value (or null if the future completed exceptionally) and the throwable that caused the failure (or null on success). Whatever the function returns becomes the completion value of the returned future.

        If the function itself throws, the returned future completes exceptionally with that thrown exception.

        Note: Unlike the standard CompletableFuture implementation, cancellation propagates both downstream and upstream. If this future or the returned future is cancelled, all futures in the chain will be cancelled.