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

net.dongliu.commons.concurrent.FutureUtils Maven / Gradle / Ivy

The newest version!
package net.dongliu.commons.concurrent;

import net.dongliu.commons.collection.TupleUtils;
import net.dongliu.commons.collection.*;

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

/**
 * utils for CompletableFuture
 *
 * @author Dong Liu [email protected]
 */
public class FutureUtils {

    /**
     * create a future with exception
     */
    public static  CompletableFuture exceptionalFuture(Throwable t) {
        CompletableFuture future = new CompletableFuture<>();
        future.completeExceptionally(t);
        return future;
    }

    /**
     * combine two futures and return one future with result as Tuple2
     */
    public static  CompletableFuture> combine(
            CompletableFuture af, CompletableFuture bf) {
        return af.thenCombine(bf, TupleUtils::of);
    }

    /**
     * combine three futures and return one future with result as Tuple3
     */
    public static  CompletableFuture> combine(
            CompletableFuture af, CompletableFuture bf, CompletableFuture cf) {
        return combine(af, bf).thenCombine(cf, (t2, c) -> new Tuple3<>(t2._1(), t2._2(), c));
    }

    /**
     * combine four futures and return one future with result as Tuple4
     */
    public static  CompletableFuture> combine(
            CompletableFuture af, CompletableFuture bf, CompletableFuture cf,
            CompletableFuture df) {
        return combine(af, bf, cf)
                .thenCombine(df, (t3, d) -> new Tuple4<>(t3._1(), t3._2(), t3._3(), d));
    }

    /**
     * combine five futures and return one future with result as Tuple5
     */
    public static  CompletableFuture> combine(
            CompletableFuture af, CompletableFuture bf, CompletableFuture cf,
            CompletableFuture df, CompletableFuture ef) {
        return combine(af, bf, cf, df)
                .thenCombine(ef, (t4, e) -> new Tuple5<>(t4._1(), t4._2(), t4._3(), t4._4(), e));
    }

    /**
     * combine six futures and return one future with result as Tuple6
     */
    public static  CompletableFuture> combine(
            CompletableFuture af, CompletableFuture bf, CompletableFuture cf,
            CompletableFuture df, CompletableFuture ef, CompletableFuture ff) {
        return combine(af, bf, cf, df, ef)
                .thenCombine(ff, (t, f) -> new Tuple6<>(t._1(), t._2(), t._3(), t._4(), t._5(), f));
    }

    /**
     * combine seven futures and return one future with result as Tuple7
     */
    public static  CompletableFuture> combine(
            CompletableFuture af, CompletableFuture bf, CompletableFuture cf,
            CompletableFuture df, CompletableFuture ef, CompletableFuture ff,
            CompletableFuture gf) {
        return combine(af, bf, cf, df, ef, ff)
                .thenCombine(gf, (t, g) -> new Tuple7<>(t._1(), t._2(), t._3(), t._4(), t._5(),
                        t._6(), g));
    }

    /**
     * combine multi futures, return one future with result as list
     */
    @SafeVarargs
    public static  CompletableFuture> combine(CompletableFuture... futures) {
        return CompletableFuture.allOf(futures)
                .thenApply(e -> ExStream.wrap(futures).map(f -> f.getNow(null)).toList());
    }

    /**
     * combine multi futures, return one future with result as list
     */
    public static 
    CompletableFuture> combine(Collection> futures) {
        return combine(futures.toArray((CompletableFuture[]) new CompletableFuture[futures.size()]));
    }
}