Class SingletonCollectors

java.lang.Object
io.blt.util.stream.SingletonCollectors

public final class SingletonCollectors extends Object
Implementations of Collector that reduce to exactly one or zero elements. e.g.,

 var maybeActiveStep = sequentialSteps.stream()
         .filter(SequentialStep::isActive)
         .collect(SingletonCollectors.toOptional());
 

If more than one element is present, then IllegalArgumentException is thrown.

  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> io.blt.util.stream.SingletonCollectors.SingletonCollector<T,T>
    Returns a Collector that accumulates the only element, if any, into a nullable Object.
    static <T> io.blt.util.stream.SingletonCollectors.SingletonCollector<T,Optional<T>>
    Returns a Collector that accumulates the only element, if any, into an Optional.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toOptional

      public static <T> io.blt.util.stream.SingletonCollectors.SingletonCollector<T,Optional<T>> toOptional()
      Returns a Collector that accumulates the only element, if any, into an Optional.
      Type Parameters:
      T - the type of the input elements
      Returns:
      a Collector that accumulates the only element, if any, into an Optional.
      Throws:
      IllegalArgumentException - if more than one element is present
    • toNullable

      public static <T> io.blt.util.stream.SingletonCollectors.SingletonCollector<T,T> toNullable()
      Returns a Collector that accumulates the only element, if any, into a nullable Object.
      Type Parameters:
      T - the type of the input elements
      Returns:
      a Collector that accumulates the only element, if any, into a nullable Object.
      Throws:
      IllegalArgumentException - if more than one element is present