Package no.digipost

Class DiggOptionals


  • public final class DiggOptionals
    extends Object
    Utilities for working with Optionals.
    • Constructor Detail

      • DiggOptionals

        public DiggOptionals()
    • Method Detail

      • toStream

        @SafeVarargs
        public static <T> Stream<T> toStream​(Supplier<Optional<T>>... optionalResolvers)
        Convert suppliers of Optional to a Stream consisting of only the present optionals. The returned stream is BaseStream.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 many Optionals needed until the first present one is encountered.

        Note on parallel streams

        The returned stream will be sequential and appropriate for use to resolve several Optionals 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 with parallelizing the 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 given Optionals and returned Stream.
        Parameters:
        optionalResolvers - The operations resolving Optionals to convert to a Stream.
        Returns:
        a Stream containing the present Optionals
      • toStream

        @SafeVarargs
        public static <T> Stream<T> toStream​(Optional<? extends T>... optionals)
        Convert Optionals to a Stream consisting of only the present optionals.
        Type Parameters:
        T - The type of the given Optionals and returned Stream.
        Parameters:
        optionals - The Optionals to convert to a Stream.
        Returns:
        a Stream containing the present Optionals
      • toList

        public static <T> List<T> toList​(Optional<T> optional)
        Unwrap an Optional into a zero or one element unmodifiable list.
        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 Optional being present or not.
      • collect

        public static <T,​A,​R> R collect​(Optional<T> optional,
                                                    Collector<T,​A,​R> collector)
        Unwrap an Optional into another container using a 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 Optional being present or not.