Package io.blt.util.stream
Class SingletonCollectors
java.lang.Object
io.blt.util.stream.SingletonCollectors
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 TypeMethodDescriptionstatic <T> io.blt.util.stream.SingletonCollectors.SingletonCollector<T,T> Returns aCollectorthat accumulates the only element, if any, into a nullableObject.static <T> io.blt.util.stream.SingletonCollectors.SingletonCollector<T,Optional<T>> Returns aCollectorthat accumulates the only element, if any, into anOptional.
-
Method Details
-
toOptional
public static <T> io.blt.util.stream.SingletonCollectors.SingletonCollector<T,Optional<T>> toOptional()Returns aCollectorthat accumulates the only element, if any, into anOptional.- Type Parameters:
T- the type of the input elements- Returns:
- a
Collectorthat accumulates the only element, if any, into anOptional. - Throws:
IllegalArgumentException- if more than one element is present
-
toNullable
public static <T> io.blt.util.stream.SingletonCollectors.SingletonCollector<T,T> toNullable()Returns aCollectorthat accumulates the only element, if any, into a nullableObject.- Type Parameters:
T- the type of the input elements- Returns:
- a
Collectorthat accumulates the only element, if any, into a nullableObject. - Throws:
IllegalArgumentException- if more than one element is present
-