Class OptionalUtils

  • public final class OptionalUtils
    extends Object
    Utility methods relating to Optionals.
    • Method Detail

      • zip

        public static <A,​B> Optional<io.atlassian.fugue.Pair<A,​B>> zip​(Optional<A> o1,
                                                                                   Optional<B> o2)
        Zips the two given optionals into an optional of a pair. This method can be deleted once we're on Fugue 4.6.0 or later, when its Pair type will contain this method.
        Type Parameters:
        A - the first type
        B - the second type
        o1 - the first optional
        o2 - the second optional
        empty if either or both optionals are empty
      • first

        public static <T,​U> Optional<U> first​(Optional<T> maybeT,
                                                    Function<? super T,​Optional<U>>... mappers)
        Returns the first non-empty value obtained by separately applying the given mappers to the given Optional.
        Type Parameters:
        T - the input type
        U - the output type
        maybeT - the optional to which to apply the mappers
        mappers - the mappers to apply one by one
        empty if the given Optional is empty or all the given mappers return an empty
      • firstNonEmpty

        public static <T> Optional<T> firstNonEmpty​(Supplier<Optional<T>>... lazyOptionals)
        Returns the first of the given Optionals that is non-empty.
        Type Parameters:
        T - the type of value
        lazyOptionals - the optionals to inspect
        empty if all of the given optionals are empty