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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
static final class
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> SingletonCollectors.SingletonCollector
<T, T> Returns aCollector
that accumulates the only element, if any, into a nullableObject
.static <T> SingletonCollectors.SingletonCollector
<T, Optional<T>> Returns aCollector
that accumulates the only element, if any, into anOptional
.
-
Method Details
-
toOptional
Returns aCollector
that accumulates the only element, if any, into anOptional
.- Type Parameters:
T
- the type of the input elements- Returns:
- a
Collector
that accumulates the only element, if any, into anOptional
. - Throws:
IllegalArgumentException
- if more than one element is present
-
toNullable
Returns aCollector
that accumulates the only element, if any, into a nullableObject
.- Type Parameters:
T
- the type of the input elements- Returns:
- a
Collector
that accumulates the only element, if any, into a nullableObject
. - Throws:
IllegalArgumentException
- if more than one element is present
-