diff options
author | Morten Tokle <mortent@yahooinc.com> | 2024-02-13 13:58:51 +0100 |
---|---|---|
committer | Morten Tokle <mortent@yahooinc.com> | 2024-02-13 14:06:27 +0100 |
commit | 05ab6800a9a9d2119aba89b2bf9d15aa29b11a48 (patch) | |
tree | 798fb3ef69c3d9447d04967e8332aefc673e1f6e /vespajlib/src/main/java/com/yahoo/stream/CustomCollectors.java | |
parent | 283af757b42ccb5ac6bfa8339a0a0674ae51c733 (diff) |
Proxy endpoint certificate secrets through EndpointCertificateSecretStore
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/stream/CustomCollectors.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/stream/CustomCollectors.java | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/stream/CustomCollectors.java b/vespajlib/src/main/java/com/yahoo/stream/CustomCollectors.java index 9076846bbf4..c6868fb28f6 100644 --- a/vespajlib/src/main/java/com/yahoo/stream/CustomCollectors.java +++ b/vespajlib/src/main/java/com/yahoo/stream/CustomCollectors.java @@ -3,6 +3,7 @@ package com.yahoo.stream; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Optional; import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.function.Supplier; @@ -70,6 +71,22 @@ public class CustomCollectors { return Collectors.toMap(keyMapper, valueMapper, throwingMerger(), mapSupplier); } + /** + * Returns a {@code Collector} that returns a singleton, or throws an {@code IllegalArgumentException} if there are more than one item. + * + * @return A collector returning an optional element + * @param <T> Type of the input elements. + * @throws IllegalArgumentException if there are more than one element + */ + public static <T> Collector<T, ?, Optional<T>> singleton() { + return Collectors.collectingAndThen( + Collectors.toList(), + list -> { + if (list.size() > 1) throw new IllegalArgumentException("More than one element"); + return list.stream().findAny(); + } + ); + } private static <T> BinaryOperator<T> throwingMerger() { return (u,v) -> { throw new DuplicateKeyException(u); }; |