Class CompletableFutures


  • public final class CompletableFutures
    extends Object
    Utilities for CompletableFuture.
    Author:
    Michael Vorburger.ch
    • Method Detail

      • completedExceptionally

        public static <T> CompletableFuture<T> completedExceptionally​(Throwable throwable)
        Return an immediately exceptional completed CompletableFuture.

        Inspired by Guava's Futures.immediateFailedFuture(Throwable).

        Useful because while CompletableFuture does have a CompletableFuture.completedFuture(Object) static factory method to obtain immediate non-exceptional value completion, it is missing a static factory method like this. This is often useful, e.g. when catching exceptions to transform into CompletableFuture to return, and saves the 3 lines to create a new instance, invoke the non-static CompletableFuture.completeExceptionally(Throwable) and return the completableFuture. (It's not possible to use chained invocation style, because completeExceptionally does not return this but a boolean, which is useless when requiring an immediately completed CompletionStage.)