Package nva.commons.core
Class SingletonCollector
- java.lang.Object
-
- nva.commons.core.SingletonCollector
-
public final class SingletonCollector extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description static intONLY_ELEMENTstatic intSINGLETONstatic java.lang.StringSINGLETON_EXPECTED_ERROR_TEMPLATEstatic java.lang.StringSINGLETON_OR_NULL_EXPECTED_ERROR_TEMPLATE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.stream.Collector<T,?,T>collect()A utility to collect and return singletons from lists.static <T> java.util.stream.Collector<T,?,T>collectOrElse(T alternative)Returns the single expected value in theStreamor the alternative if nothing was found.static <T,E extends java.lang.Exception>
java.util.stream.Collector<T,?,Try<T>>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.
-
-
-
Field Detail
-
SINGLETON
public static final int SINGLETON
- See Also:
- Constant Field Values
-
ONLY_ELEMENT
public static final int ONLY_ELEMENT
- See Also:
- Constant Field Values
-
SINGLETON_EXPECTED_ERROR_TEMPLATE
public static final java.lang.String SINGLETON_EXPECTED_ERROR_TEMPLATE
- See Also:
- Constant Field Values
-
SINGLETON_OR_NULL_EXPECTED_ERROR_TEMPLATE
public static final java.lang.String SINGLETON_OR_NULL_EXPECTED_ERROR_TEMPLATE
- See Also:
- Constant Field Values
-
-
Method Detail
-
collect
public static <T> java.util.stream.Collector<T,?,T> 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
public static <T> java.util.stream.Collector<T,?,T> collectOrElse(T alternative)
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- Type Parameters:
T- the class of the collected object.- Parameters:
alternative- the alterative value if theStreamis empty.- Returns:
- the single value of the
Streamor the alternative if theStreamis empty.
-
tryCollect
public static <T,E extends java.lang.Exception> java.util.stream.Collector<T,?,Try<T>> 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.
-
-