Class SingletonCollector

java.lang.Object
nva.commons.core.SingletonCollector

public final class SingletonCollector extends Object
  • Field Details

  • Method Details

    • collect

      public static <T> 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> 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 Exception> 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.