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 | |
parent | 283af757b42ccb5ac6bfa8339a0a0674ae51c733 (diff) |
Proxy endpoint certificate secrets through EndpointCertificateSecretStore
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/stream/CustomCollectors.java | 17 | ||||
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java | 17 |
2 files changed, 34 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); }; diff --git a/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java b/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java index fcafb4c6152..880e57683f8 100644 --- a/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java +++ b/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java @@ -7,12 +7,14 @@ import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.function.Function; import static com.yahoo.stream.CustomCollectors.toCustomMap; import static com.yahoo.stream.CustomCollectors.toLinkedMap; import static java.util.function.Function.identity; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; /** @@ -51,6 +53,21 @@ public class CustomCollectorsTest { } } + @Test + public void singleton_collector_throws_when_multiple() { + List<String> items = List.of("foo1", "bar", "foo2"); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> items.stream().filter(s -> s.startsWith("foo")).collect(CustomCollectors.singleton())); + assertEquals("More than one element", exception.getMessage()); + } + + @Test + public void collector_returns_singleton() { + List<String> items = List.of("foo1", "bar", "foo2"); + Optional<String> bar = items.stream().filter(s -> s.startsWith("bar")).collect(CustomCollectors.singleton()); + assertEquals(Optional.of("bar"), bar); + } + private static List<String> numberList() { return List.of("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); } |