From e72ad04805b3b189caeb32226eaaea1ac49be760 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 7 Dec 2018 11:38:39 +0100 Subject: [PATCH] [Minor] Better exception message on CollectionsHelper.singletonCollector() --- .../java/li/strolch/search/SearchResult.java | 29 +++++++++++++++---- .../utils/collections/CollectionsHelper.java | 2 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/li.strolch.agent/src/main/java/li/strolch/search/SearchResult.java b/li.strolch.agent/src/main/java/li/strolch/search/SearchResult.java index aa762a1ec..a01edf522 100644 --- a/li.strolch.agent/src/main/java/li/strolch/search/SearchResult.java +++ b/li.strolch.agent/src/main/java/li/strolch/search/SearchResult.java @@ -134,22 +134,41 @@ public class SearchResult { } /** - * Returns the single element in the stream, or throws an {@link IllegalStateException} if the stream does not - * contain 1 and only 1 element + * Returns the single element in the stream, or throws an {@link IllegalStateException} if the stream contains more + * than 1 element, or the empty {@link Optional} * * @return the single element in the stream * * @throws IllegalStateException - * if not 1 and only 1 element is in the stream + * if there is more than 1 element in the stream */ - public Optional toSingleton() { - return Optional.of(this.stream.collect(singletonCollector(true))); + public Optional toSingletonO() { + return Optional.ofNullable(this.stream.collect(singletonCollector(true))); + } + + /** + * Returns the single element in the stream, or throws an {@link IllegalStateException} if the stream contains more + * * than 1 element, or the empty {@link Optional} + * + * @param errorMsgSupplier + * the supplier for an error message to use if not 1 and only 1 element is in the collection + * + * @return the single element in the stream + * + * @throws IllegalStateException + * if there is more than 1 element in the stream + */ + public Optional toSingletonO(Supplier errorMsgSupplier) { + return Optional.ofNullable(this.stream.collect(singletonCollector(true, errorMsgSupplier))); } /** * Returns the single element in the stream, or throws an {@link IllegalStateException} if the stream does not * contain 1 and only 1 element * + * @param errorMsgSupplier + * the supplier for an error message to use if not 1 and only 1 element is in the collection + * * @return the single element in the stream * * @throws IllegalStateException diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/collections/CollectionsHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/collections/CollectionsHelper.java index 185c62fad..0cb21f913 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/collections/CollectionsHelper.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/collections/CollectionsHelper.java @@ -145,7 +145,7 @@ public class CollectionsHelper { if (errorMsg == null) { throw new IllegalStateException("Expect one element, but received no elements"); } else - throw new IllegalStateException(errorMsg); + throw new IllegalStateException(errorMsg + ": (none)"); } if (list.size() != 1) {