Package no.digipost
Class DiggOptionals
java.lang.Object
no.digipost.DiggOptionals
Utilities for working with
Optionals.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,A, R> R static <T> List<T>Unwrap anOptionalinto a zero or one elementunmodifiablelist.static <T> Stream<T>static <T> Stream<T>
-
Constructor Details
-
DiggOptionals
public DiggOptionals()
-
-
Method Details
-
toStream
ConvertsuppliersofOptionalto aStreamconsisting of only thepresentoptionals. The returned stream isBaseStream.sequential(), yielding the present values in the order they are given. Depending on how the stream is consumed, only the necessary optional values will be attempted resolved. E.g. calling.findFirst()will only resolve as manyOptionals needed until the first present one is encountered.Note on parallel streams:
The returned stream will be sequential and appropriate for use to resolve severalOptionals in a deterministic order, or for instance to find the first present value of a prioritized set of possible ways to resolve it. One should be very (as always with streams) careful withparallelizingthe stream, as you generally have no control with how the resolvers will be run. For instance,.parallel().findAny()will indeed return the first resolved value, but still block until the workers already started by the stream has terminated.- Type Parameters:
T- The type of the givenOptionals and returnedStream.- Parameters:
optionalResolvers- The operations resolvingOptionals to convert to aStream.- Returns:
- a
Streamcontaining the presentOptionals
-
toStream
- Type Parameters:
T- The type of the givenOptionals and returnedStream.- Parameters:
optionals- TheOptionals to convert to aStream.- Returns:
- a
Streamcontaining the presentOptionals
-
toList
Unwrap anOptionalinto a zero or one elementunmodifiablelist.- Type Parameters:
T- The type of the optional value.- Parameters:
optional- The optional value to be wrapped in a list.- Returns:
- list containing either exactly one or zero elements depending of the
Optionalbeing present or not.
-
collect
- Type Parameters:
T- The type of the optional value.R- The resulting container type.- Parameters:
optional- The optional value to be wrapped in another container.collector- The collector to use for wrapping into another container.- Returns:
- The new container with either exactly one or zero elements depending of the
Optionalbeing present or not.
-