/*
 * Licensed to Crate under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.  Crate licenses this file
 * to you under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial
 * agreement.
 */

package io.crate.concurrent;

import io.crate.core.SuppressForbidden;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public final class CompletableFutures {

    private CompletableFutures() {
    }

    /**
     * Creates a {@code CompletableFuture} which will throw the given exception on the invocations
     * of get()
     */
    public static <T> CompletableFuture<T> failedFuture(Throwable t) {
        CompletableFuture<T> future = new CompletableFuture<>();
        future.completeExceptionally(t);
        return future;
    }

    public static <T> CompletableFuture<List<T>> allAsList(Collection<? extends CompletableFuture<? extends T>> futures) {
        return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
            .thenApply(aVoid -> futures.stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList()));
    }

    @SuppressForbidden
    public static <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier,
                                                       Executor executor) {
        try {
            return CompletableFuture.supplyAsync(supplier, executor);
        } catch (Exception e) {
            return failedFuture(e);
        }
    }

}

@@ -1063,16 +1063,44 @@
 idden;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -1267,44 +1267,8 @@
 ier;
-%0Aimport java.util.stream.Collectors;
 %0A%0Apu
@@ -1923,16 +1923,73 @@
 Void -%3E 
+%7B%0A                ArrayList%3CT%3E results = new ArrayList%3C%3E(
 futures.
@@ -1993,15 +1993,15 @@
 es.s
-tream()
+ize());
 %0A   
@@ -2017,12 +2017,12 @@
     
-.map
+for 
 (Com
@@ -2039,61 +2039,153 @@
 ture
-::join)%0A                .collect(Collectors.toList())
+%3C? extends T%3E future : futures) %7B%0A                    results.add(future.join());%0A                %7D%0A                return results;%0A            %7D
 );%0A 

Create results list with correct size in CompletableFutures