Add CompletableFutureUtils

Convenience util for CompletableFuture.allOf method
This commit is contained in:
chimp1984 2021-11-01 22:49:01 +01:00
parent ca145b0eb3
commit 603e9fde17
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class CompletableFutureUtils {
/**
* @param list List of futures
* @param <T> 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 <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> list) {
CompletableFuture<Void> allFuturesResult = CompletableFuture.allOf(list.toArray(new CompletableFuture[list.size()]));
return allFuturesResult.thenApply(v ->
list.stream().
map(CompletableFuture::join).
collect(Collectors.<T>toList())
);
}
}