Package nva.commons.core
Class SingletonCollector
java.lang.Object
nva.commons.core.SingletonCollector
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Collector<T,?, T> collect()A utility to collect and return singletons from lists.static <T> Collector<T,?, T> collectOrElse(T alternative) Returns the single expected value in theStreamor the alternative if nothing was found.A utility to return a singleton from a list that is expected to contain one and only one item, throwing supplied exception if the list is empty or does not contain one element.
-
Field Details
-
SINGLETON
public static final int SINGLETON- See Also:
-
ONLY_ELEMENT
public static final int ONLY_ELEMENT- See Also:
-
SINGLETON_EXPECTED_ERROR_TEMPLATE
- See Also:
-
SINGLETON_OR_NULL_EXPECTED_ERROR_TEMPLATE
- See Also:
-
-
Method Details
-
collect
A utility to collect and return singletons from lists.- Type Parameters:
T- the type of input elements to the reduction operation.- Returns:
- a singleton of type T.
-
collectOrElse
Returns the single expected value in theStreamor the alternative if nothing was found. It throws anIllegalStateExceptionwhen more than one results have been collected from theStream -
tryCollect
A utility to return a singleton from a list that is expected to contain one and only one item, throwing supplied exception if the list is empty or does not contain one element.- Type Parameters:
T- The type of input elements to the reduction operation.E- The type of the exception to be thrown.- Returns:
- A type of the singleton.
-