Sho SHIMIZU

Add more convenient version of CompletableFuture.allOf()

Change-Id: I40a2db5de22870adf524a9d0e1895c721291a50f
......@@ -49,6 +49,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
......@@ -532,6 +533,22 @@ public abstract class Tools {
}
/**
* Returns a new CompletableFuture completed with a list of computed values
* when all of the given CompletableFuture complete.
*
* @param futures the CompletableFutures
* @param <T> value type of CompletableFuture
* @return a new CompletableFuture that is completed when all of the given CompletableFutures complete
*/
public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futures) {
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]))
.thenApply(v -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList())
);
}
/**
* Returns the contents of {@code ByteBuffer} as byte array.
* <p>
* WARNING: There is a performance cost due to array copy
......