]> git.basschouten.com Git - openhab-addons.git/blob
c63aa2acf345203672186fe83d20891662dbf6c0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.generic.utils;
14
15 import java.util.HashSet;
16 import java.util.Set;
17 import java.util.concurrent.CompletableFuture;
18 import java.util.function.Supplier;
19 import java.util.stream.Collector;
20
21 /**
22  * Collector to combine a stream of CompletableFutures.
23  *
24  * @author Jochen Klein - Initial contribution
25  *
26  */
27 public class FutureCollector {
28
29     public static <X> Collector<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<Void>> allOf() {
30         return Collector.<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<Void>> of(
31                 (Supplier<Set<CompletableFuture<X>>>) HashSet::new, Set::add, (left, right) -> {
32                     left.addAll(right);
33                     return left;
34                 }, a -> {
35                     return CompletableFuture.allOf(a.toArray(new CompletableFuture[a.size()]));
36                 }, Collector.Characteristics.UNORDERED);
37     }
38 }