From 790f7a0415f9fdfe69d9b2f545974868f1c89763 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 3 Dec 2018 14:20:25 +0100 Subject: [PATCH] [New] added RootElementSearchResult.toSingleton() --- .../strolch/search/RootElementSearchResult.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/li.strolch.agent/src/main/java/li/strolch/search/RootElementSearchResult.java b/li.strolch.agent/src/main/java/li/strolch/search/RootElementSearchResult.java index 9e2f06e55..99b1b1439 100644 --- a/li.strolch.agent/src/main/java/li/strolch/search/RootElementSearchResult.java +++ b/li.strolch.agent/src/main/java/li/strolch/search/RootElementSearchResult.java @@ -1,12 +1,14 @@ package li.strolch.search; import java.util.Comparator; +import java.util.function.Supplier; import java.util.stream.Stream; import li.strolch.model.StrolchElement; import li.strolch.model.StrolchRootElement; import li.strolch.model.parameter.Parameter; import li.strolch.model.visitor.StrolchRootElementVisitor; +import li.strolch.utils.collections.CollectionsHelper; /** * A search result for {@link StrolchSearch} for {@link StrolchRootElement} adding methods specific to root element @@ -101,6 +103,19 @@ public class RootElementSearchResult extends Searc return this; } + /** + * Returns the single element in the stream, or throws an {@link IllegalStateException} if the stream does not + * contain 1 and only 1 element + * + * @return the single element in the stream + * + * @throws IllegalStateException + * if not 1 and only 1 element is in the stream + */ + public T toSingleton(Supplier errorMsgSupplier) throws IllegalStateException { + return this.stream.collect(CollectionsHelper.singletonCollector(errorMsgSupplier)); + } + /** * Transforms this search result to be a search result returning elements with the given visitor *