diff options
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); }; |