[Minor] Better exception message on CollectionsHelper.singletonCollector()

This commit is contained in:
Robert von Burg 2018-12-07 11:38:39 +01:00
parent b0e5739034
commit e72ad04805
2 changed files with 25 additions and 6 deletions

View File

@ -134,22 +134,41 @@ public class SearchResult<T> {
}
/**
* 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<T> toSingleton() {
return Optional.of(this.stream.collect(singletonCollector(true)));
public Optional<T> 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<T> toSingletonO(Supplier<String> 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

View File

@ -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) {