Class SingletonCollector


  • public final class SingletonCollector
    extends java.lang.Object
    • 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 the Stream or 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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 the Stream or the alternative if nothing was found. It throws an IllegalStateException when more than one results have been collected from the Stream
        Type Parameters:
        T - the class of the collected object.
        Parameters:
        alternative - the alterative value if the Stream is empty.
        Returns:
        the single value of the Stream or the alternative if the Stream is 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.