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 *