[New] added RootElementSearchResult.toSingleton()

This commit is contained in:
Robert von Burg 2018-12-03 14:20:25 +01:00
parent b95039c57b
commit 790f7a0415
1 changed files with 15 additions and 0 deletions

View File

@ -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<T extends StrolchRootElement> 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<String> 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
*