All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jashmore.sqs.util.concurrent.CompletableFutureUtils Maven / Gradle / Ivy

package com.jashmore.sqs.util.concurrent;

import static java.util.stream.Collectors.toList;

import lombok.experimental.UtilityClass;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@UtilityClass
public class CompletableFutureUtils {
    /**
     * Creates a new {@link CompletableFuture} that is completed exceptionally with the provided {@link Throwable}.
     *
     * @param throwable the throwable to complete the {@link CompletableFuture} with
     * @param  the type that the future is returning
     * @return a future that is completed exceptionally
     */
    public  CompletableFuture completedExceptionally(final Throwable throwable) {
        final CompletableFuture future = new CompletableFuture<>();
        future.completeExceptionally(throwable);
        return future;
    }

    /**
     * Wait for all of the futures to complete and return a single future containing the list of results.
     *
     * @param futures the futures to wait for
     * @param  the type that the future returns
     * @return a future that will complete if all futures are completed successfully
     */
    public  CompletableFuture> allOf(final List> futures) {
        return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
                .thenApply((ignored) -> futures.stream()
                        .map(CompletableFuture::join)
                        .collect(toList()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy