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

graphql.execution.Async Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.execution;

import graphql.Assert;
import graphql.Internal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

@Internal
@SuppressWarnings("FutureReturnValueIgnored")
public class Async {

    public interface CombinedBuilder {

        void add(CompletableFuture completableFuture);

        CompletableFuture> await();
    }

    /**
     * Combines 0 or more CF into one. It is a wrapper around CompletableFuture.allOf.
     *
     * @param expectedSize how many we expect
     * @param           for two
     *
     * @return a combined builder of CFs
     */
    public static  CombinedBuilder ofExpectedSize(int expectedSize) {
        if (expectedSize == 0) {
            return new Empty<>();
        } else if (expectedSize == 1) {
            return new Single<>();
        } else {
            return new Many<>(expectedSize);
        }
    }

    private static class Empty implements CombinedBuilder {

        private int ix;

        @Override
        public void add(CompletableFuture completableFuture) {
            this.ix++;
        }


        @Override
        public CompletableFuture> await() {
            Assert.assertTrue(ix == 0, () -> "expected size was " + 0 + " got " + ix);
            return typedEmpty();
        }


        // implementation details: infer the type of Completable> from a singleton empty
        private static final CompletableFuture> EMPTY = CompletableFuture.completedFuture(Collections.emptyList());

        @SuppressWarnings("unchecked")
        private static  CompletableFuture typedEmpty() {
            return (CompletableFuture) EMPTY;
        }
    }

    private static class Single implements CombinedBuilder {

        // avoiding array allocation as there is only 1 CF
        private CompletableFuture completableFuture;
        private int ix;

        @Override
        public void add(CompletableFuture completableFuture) {
            this.completableFuture = completableFuture;
            this.ix++;
        }

        @Override
        public CompletableFuture> await() {
            Assert.assertTrue(ix == 1, () -> "expected size was " + 1 + " got " + ix);
            return completableFuture.thenApply(Collections::singletonList);
        }
    }

    private static class Many implements CombinedBuilder {

        private final CompletableFuture[] array;
        private int ix;

        @SuppressWarnings("unchecked")
        private Many(int size) {
            this.array = new CompletableFuture[size];
            this.ix = 0;
        }

        @Override
        public void add(CompletableFuture completableFuture) {
            array[ix++] = completableFuture;
        }

        @Override
        public CompletableFuture> await() {
            Assert.assertTrue(ix == array.length, () -> "expected size was " + array.length + " got " + ix);

            CompletableFuture> overallResult = new CompletableFuture<>();
            CompletableFuture.allOf(array)
                    .whenComplete((ignored, exception) -> {
                        if (exception != null) {
                            overallResult.completeExceptionally(exception);
                            return;
                        }
                        List results = new ArrayList<>(array.length);
                        for (CompletableFuture future : array) {
                            results.add(future.join());
                        }
                        overallResult.complete(results);
                    });
            return overallResult;
        }

    }

    public static  CompletableFuture> each(Collection list, Function> cfFactory) {
        CombinedBuilder futures = ofExpectedSize(list.size());
        for (T t : list) {
            CompletableFuture cf;
            try {
                cf = cfFactory.apply(t);
                Assert.assertNotNull(cf, () -> "cfFactory must return a non null value");
            } catch (Exception e) {
                cf = new CompletableFuture<>();
                // Async.each makes sure that it is not a CompletionException inside a CompletionException
                cf.completeExceptionally(new CompletionException(e));
            }
            futures.add(cf);
        }
        return futures.await();
    }

    public static  CompletableFuture> eachSequentially(Iterable list, BiFunction, CompletableFuture> cfFactory) {
        CompletableFuture> result = new CompletableFuture<>();
        eachSequentiallyImpl(list.iterator(), cfFactory, new ArrayList<>(), result);
        return result;
    }

    private static  void eachSequentiallyImpl(Iterator iterator, BiFunction, CompletableFuture> cfFactory, List tmpResult, CompletableFuture> overallResult) {
        if (!iterator.hasNext()) {
            overallResult.complete(tmpResult);
            return;
        }
        CompletableFuture cf;
        try {
            cf = cfFactory.apply(iterator.next(), tmpResult);
            Assert.assertNotNull(cf, () -> "cfFactory must return a non null value");
        } catch (Exception e) {
            cf = new CompletableFuture<>();
            cf.completeExceptionally(new CompletionException(e));
        }
        cf.whenComplete((cfResult, exception) -> {
            if (exception != null) {
                overallResult.completeExceptionally(exception);
                return;
            }
            tmpResult.add(cfResult);
            eachSequentiallyImpl(iterator, cfFactory, tmpResult, overallResult);
        });
    }


    /**
     * Turns an object T into a CompletableFuture if it's not already
     *
     * @param t   - the object to check
     * @param  for two
     *
     * @return a CompletableFuture
     */
    public static  CompletableFuture toCompletableFuture(T t) {
        if (t instanceof CompletionStage) {
            //noinspection unchecked
            return ((CompletionStage) t).toCompletableFuture();
        } else {
            return CompletableFuture.completedFuture(t);
        }
    }

    public static  CompletableFuture tryCatch(Supplier> supplier) {
        try {
            return supplier.get();
        } catch (Exception e) {
            CompletableFuture result = new CompletableFuture<>();
            result.completeExceptionally(e);
            return result;
        }
    }

    public static  CompletableFuture exceptionallyCompletedFuture(Throwable exception) {
        CompletableFuture result = new CompletableFuture<>();
        result.completeExceptionally(exception);
        return result;
    }

    /**
     * If the passed in CompletableFuture is null then it creates a CompletableFuture that resolves to null
     *
     * @param completableFuture the CF to use
     * @param                for two
     *
     * @return the completableFuture if it's not null or one that always resoles to null
     */
    public static  @NotNull CompletableFuture orNullCompletedFuture(@Nullable CompletableFuture completableFuture) {
        return completableFuture != null ? completableFuture : CompletableFuture.completedFuture(null);
    }
}