]> git.basschouten.com Git - openhab-addons.git/blob
4ac55d6a09c24075403a5735d7169467596bad6a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * Collector to combine a stream of CompletableFutures.
26  *
27  * @author Jochen Klein - Initial contribution
28  *
29  */
30 @NonNullByDefault
31 public class FutureCollector {
32
33     public static <X> Collector<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<@Nullable Void>> allOf() {
34         return Collector.<CompletableFuture<X>, Set<CompletableFuture<X>>, CompletableFuture<@Nullable Void>> of(
35                 (Supplier<Set<CompletableFuture<X>>>) HashSet::new, Set::add, (left, right) -> {
36                     left.addAll(right);
37                     return left;
38                 }, a -> CompletableFuture.allOf(a.toArray(new CompletableFuture[a.size()])),
39                 Collector.Characteristics.UNORDERED);
40     }
41 }