Package no.digipost
Class DiggOptionals
- java.lang.Object
-
- no.digipost.DiggOptionals
-
-
Constructor Summary
Constructors Constructor Description DiggOptionals()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,A,R>
Rcollect(Optional<T> optional, Collector<T,A,R> collector)static <T> List<T>toList(Optional<T> optional)Unwrap anOptionalinto a zero or one elementunmodifiablelist.static <T> Stream<T>toStream(Supplier<Optional<T>>... optionalResolvers)static <T> Stream<T>toStream(Optional<? extends T>... optionals)
-
-
-
Method Detail
-
toStream
@SafeVarargs public static <T> Stream<T> toStream(Supplier<Optional<T>>... optionalResolvers)
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 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
@SafeVarargs public static <T> Stream<T> toStream(Optional<? extends T>... optionals)
- Type Parameters:
T- The type of the givenOptionals and returnedStream.- Parameters:
optionals- TheOptionals to convert to aStream.- Returns:
- a
Streamcontaining the presentOptionals
-
toList
public static <T> List<T> toList(Optional<T> optional)
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
public static <T,A,R> R collect(Optional<T> optional, Collector<T,A,R> collector)
- 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.
-
-