Class Optionals


  • public final class Optionals
    extends Object
    Utility functions for Optional.
    • Method Detail

      • any

        public static boolean any​(Optional<?>... optionals)
        Checks if any of the optionals is present.
        Parameters:
        optionals - the optionals
        Returns:
        if at least one is present
      • any

        public static boolean any​(Iterable<Optional<?>> optionals)
        Checks if any of the optionals is present.
        Parameters:
        optionals - the optionals
        Returns:
        if at least one is present
      • any

        public static boolean any​(Stream<Optional<?>> optionals)
        Checks if any of the optionals is present.
        Parameters:
        optionals - the optionals
        Returns:
        if at least one is present
      • nullsLast

        public static <U> Comparator<Optional<U>> nullsLast​(Comparator<? super U> comparator)
        Creates a comparator wrapping comparator to sort by the optional's value, nulls last.
        Type Parameters:
        U - the optional's type
        Parameters:
        comparator - the comparator
        Returns:
        the new comparator
      • nullsLast

        public static <U extends Comparable<? super U>> Comparator<Optional<U>> nullsLast()
        Creates a comparator to sort by the optional's value, nulls last.
        Type Parameters:
        U - the optional's type
        Returns:
        the new comparator
      • nullsFirst

        public static <U> Comparator<Optional<U>> nullsFirst​(Comparator<? super U> comparator)
        Creates a comparator wrapping comparator to sort by the optional's value, nulls first.
        Type Parameters:
        U - the optional's type
        Parameters:
        comparator - the comparator
        Returns:
        the new comparator
      • nullsFirst

        public static <U extends Comparable<? super U>> Comparator<Optional<U>> nullsFirst()
        Creates a comparator to sort by the optional's value, nulls first.
        Type Parameters:
        U - the optional's type
        Returns:
        the new comparator
      • orElseNull

        public static <U> U orElseNull​(Optional<U> optional)
        Gets the value of optional if it is present, null otherwise.
        Type Parameters:
        U - the optional's type
        Parameters:
        optional - the optional
        Returns:
        the optional's value or null
      • ifPresentOrElse

        public static <U> Optional<U> ifPresentOrElse​(Optional<U> optional,
                                                      Consumer<U> consumer,
                                                      Runnable ifNotPresent)
        Feeds {code optional}'s value to consumer if it is present or else runs ifNotPresent.
        Type Parameters:
        U - the optional's type
        Parameters:
        optional - the optional
        consumer - the consumer
        ifNotPresent - the runnable
        Returns:
        the optional
      • mapOrElse

        public static <U,​T> Optional<U> mapOrElse​(Optional<T> optional,
                                                        Function<? super T,​? extends U> mapper,
                                                        Runnable ifNotPresent)
        Maps the optional using mapper if it is present or runs ifNotPresent.
        Type Parameters:
        T - the resulting optional's type
        U - the optional's type
        Parameters:
        optional - the optinal
        mapper - the mapper
        ifNotPresent - the runnable
        Returns:
        the new optional
      • or

        public static <U> Optional<U> or​(Optional<U> a,
                                         Supplier<Optional<U>> b)
        Gets the value of a if it is present or else the value of b.
        Type Parameters:
        U - the optional's type
        Parameters:
        a - the first optional
        b - a supplier for the second optional
        Returns:
        the value
      • or

        public static <U> Optional<U> or​(Optional<U> a,
                                         Optional<U> b)
        Gets the value of a if it is present or else the value of b.
        Type Parameters:
        U - the optional's type
        Parameters:
        a - the first optional
        b - a the second optional
        Returns:
        the value
      • or

        @SafeVarargs
        public static <U> Optional<U> or​(Optional<U>... optionals)
        Gets the first value of the first optional that is present.
        Type Parameters:
        U - the optional's type
        Parameters:
        optionals - the optionals
        Returns:
        the first present value
      • or

        @SafeVarargs
        public static <U> Optional<U> or​(Supplier<Optional<U>>... optionals)
        Gets the first value of the first optional that is present.
        Type Parameters:
        U - the optional's type
        Parameters:
        optionals - the optionals
        Returns:
        the first present value
      • or

        public static <U> Optional<U> or​(Stream<Supplier<Optional<U>>> optionals)
        Gets the first value of the first optional that is present.
        Type Parameters:
        U - the optional's type
        Parameters:
        optionals - the optionals
        Returns:
        the first present value