diff --git a/common/src/main/java/bisq/common/util/CompletableFutureUtils.java b/common/src/main/java/bisq/common/util/CompletableFutureUtils.java new file mode 100644 index 0000000000..f46cb760e0 --- /dev/null +++ b/common/src/main/java/bisq/common/util/CompletableFutureUtils.java @@ -0,0 +1,37 @@ +package bisq.common.util;/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; + +public class CompletableFutureUtils { + /** + * @param list List of futures + * @param The generic type of the future + * @return Returns a CompletableFuture with a list of the futures we got as parameter once all futures + * are completed (incl. exceptionally completed). + */ + public static CompletableFuture> allOf(List> list) { + CompletableFuture allFuturesResult = CompletableFuture.allOf(list.toArray(new CompletableFuture[list.size()])); + return allFuturesResult.thenApply(v -> + list.stream(). + map(CompletableFuture::join). + collect(Collectors.toList()) + ); + } +}