Class Option<T>

  • Type Parameters:
    T - The type of the optional value.
    All Implemented Interfaces:
    Iterable<T>, Value<T>, java.io.Serializable, java.lang.Iterable<T>
    Direct Known Subclasses:
    Option.None, Option.Some

    public abstract class Option<T>
    extends java.lang.Object
    implements Iterable<T>, Value<T>, java.io.Serializable
    Replacement for Optional.

    Option is a monadic container type which represents an optional value. Instances of Option are either an instance of Option.Some or the singleton Option.None.

    Most of the API is taken from Optional. A similar type can be found in Haskell and Scala.

    See Also:
    Serialized Form
    • Method Detail

      • of

        public static <T> Option<T> of​(T value)
        Creates a new Option of a given value.
        Type Parameters:
        T - type of the value
        Parameters:
        value - A value
        Returns:
        Some(value) if value is not null, None otherwise
      • sequence

        public static <T> Option<Seq<T>> sequence​(java.lang.Iterable<? extends Option<? extends T>> values)
        Reduces many Options into a single Option by transforming an Iterable<Option<? extends T>> into a Option<Seq<T>>. If any of the Options are Option.None, then this returns Option.None.
        Type Parameters:
        T - type of the Options
        Parameters:
        values - An Iterable of Options
        Returns:
        An Option of a Seq of results
        Throws:
        java.lang.NullPointerException - if values is null
      • traverse

        public static <T,​U> Option<Seq<U>> traverse​(java.lang.Iterable<? extends T> values,
                                                          java.util.function.Function<? super T,​? extends Option<? extends U>> mapper)
        Maps the values of an iterable to a sequence of mapped values into a single Option by transforming an Iterable<? extends T> into a Option<Seq<U>>.

        Type Parameters:
        T - The type of the given values.
        U - The mapped value type.
        Parameters:
        values - An Iterable of values.
        mapper - A mapper of values to Options
        Returns:
        A Option of a Seq of results.
        Throws:
        java.lang.NullPointerException - if values or f is null.
      • some

        public static <T> Option<T> some​(T value)
        Creates a new Some of a given value.

        The only difference to of(Object) is, when called with argument null.

         
         Option.of(null);   // = None
         Option.some(null); // = Some(null)
         
         
        Type Parameters:
        T - type of the value
        Parameters:
        value - A value
        Returns:
        Some(value)
      • none

        public static <T> Option<T> none()
        Returns the single instance of None
        Type Parameters:
        T - component type
        Returns:
        the single instance of None
      • narrow

        public static <T> Option<T> narrow​(Option<? extends T> option)
        Narrows a widened Option<? extends T> to Option<T> by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.
        Type Parameters:
        T - Component type of the Option.
        Parameters:
        option - A Option.
        Returns:
        the given option instance as narrowed type Option<T>.
      • when

        public static <T> Option<T> when​(boolean condition,
                                         java.util.function.Supplier<? extends T> supplier)
        Creates Some of suppliers value if condition is true, or None in other case
        Type Parameters:
        T - type of the optional value
        Parameters:
        condition - A boolean value
        supplier - An optional value supplier, may supply null
        Returns:
        return Some of supplier's value if condition is true, or None in other case
        Throws:
        java.lang.NullPointerException - if the given supplier is null
      • when

        public static <T> Option<T> when​(boolean condition,
                                         T value)
        Creates Some of value if condition is true, or None in other case
        Type Parameters:
        T - type of the optional value
        Parameters:
        condition - A boolean value
        value - An optional value, may be null
        Returns:
        return Some of value if condition is true, or None in other case
      • ofOptional

        public static <T> Option<T> ofOptional​(java.util.Optional<? extends T> optional)
        Wraps a Java Optional to a new Option
        Type Parameters:
        T - type of the value
        Parameters:
        optional - a given optional to wrap in Option
        Returns:
        Some(optional.get()) if value is Java Optional is present, None otherwise
      • collect

        public final <R> Option<R> collect​(PartialFunction<? super T,​? extends R> partialFunction)
        Collects value that is in the domain of the given partialFunction by mapping the value to type R.
        
         partialFunction.isDefinedAt(value)
         
        If the element makes it through that filter, the mapped instance is wrapped in Option
        
         R newValue = partialFunction.apply(value)
         
        Type Parameters:
        R - The new value type
        Parameters:
        partialFunction - A function that is not necessarily defined on value of this option.
        Returns:
        A new Option instance containing value of type R
        Throws:
        java.lang.NullPointerException - if partialFunction is null
      • isEmpty

        public abstract boolean isEmpty()
        Returns true, if this is None, otherwise false, if this is Some.
        Specified by:
        isEmpty in interface Value<T>
        Returns:
        true, if this Option is empty, false otherwise
      • onEmpty

        public final Option<T> onEmpty​(java.lang.Runnable action)
        Runs a Java Runnable passed as parameter if this Option is empty.
        Parameters:
        action - a given Runnable to be run
        Returns:
        this Option
      • isAsync

        public final boolean isAsync()
        An Option's value is computed synchronously.
        Specified by:
        isAsync in interface Value<T>
        Returns:
        false
      • isDefined

        public final boolean isDefined()
        Returns true, if this is Some, otherwise false, if this is None.

        Please note that it is possible to create new Some(null), which is defined.

        Returns:
        true, if this Option has a defined value, false otherwise
      • isLazy

        public final boolean isLazy()
        An Option's value is computed eagerly.
        Specified by:
        isLazy in interface Value<T>
        Returns:
        false
      • isSingleValued

        public final boolean isSingleValued()
        An Option is single-valued.
        Specified by:
        isSingleValued in interface Value<T>
        Returns:
        true
      • get

        public abstract T get()
        Gets the value if this is a Some or throws if this is a None.
        Specified by:
        get in interface Value<T>
        Returns:
        the value
        Throws:
        java.util.NoSuchElementException - if this is a None.
      • getOrElse

        public final T getOrElse​(T other)
        Returns the value if this is a Some or the other value if this is a None.

        Please note, that the other value is eagerly evaluated.

        Specified by:
        getOrElse in interface Value<T>
        Parameters:
        other - An alternative value
        Returns:
        This value, if this Option is defined or the other value, if this Option is empty.
      • orElse

        public final Option<T> orElse​(Option<? extends T> other)
        Returns this Option if it is nonempty, otherwise return the alternative.
        Parameters:
        other - An alternative Option
        Returns:
        this Option if it is nonempty, otherwise return the alternative.
      • orElse

        public final Option<T> orElse​(java.util.function.Supplier<? extends Option<? extends T>> supplier)
        Returns this Option if it is nonempty, otherwise return the result of evaluating supplier.
        Parameters:
        supplier - An alternative Option supplier
        Returns:
        this Option if it is nonempty, otherwise return the result of evaluating supplier.
      • getOrElse

        public final T getOrElse​(java.util.function.Supplier<? extends T> supplier)
        Returns the value if this is a Some, otherwise the other value is returned, if this is a None.

        Please note, that the other value is lazily evaluated.

        Specified by:
        getOrElse in interface Value<T>
        Parameters:
        supplier - An alternative value supplier
        Returns:
        This value, if this Option is defined or the other value, if this Option is empty.
      • getOrElseThrow

        public final <X extends java.lang.Throwable> T getOrElseThrow​(java.util.function.Supplier<X> exceptionSupplier)
                                                               throws X extends java.lang.Throwable
        Returns the value if this is a Some, otherwise throws an exception.
        Specified by:
        getOrElseThrow in interface Value<T>
        Type Parameters:
        X - A throwable
        Parameters:
        exceptionSupplier - An exception supplier
        Returns:
        This value, if this Option is defined, otherwise throws X
        Throws:
        X - a throwable
        X extends java.lang.Throwable
      • filter

        public final Option<T> filter​(java.util.function.Predicate<? super T> predicate)
        Returns Some(value) if this is a Some and the value satisfies the given predicate. Otherwise None is returned.
        Parameters:
        predicate - A predicate which is used to test an optional value
        Returns:
        Some(value) or None as specified
      • filterNot

        public final Option<T> filterNot​(java.util.function.Predicate<? super T> predicate)
        Returns Some(value) if this is a Some and the value not satisfies the given predicate. Otherwise None is returned.
        Parameters:
        predicate - A predicate which is used to test an optional value
        Returns:
        Some(value) or None as specified
      • flatMap

        public final <U> Option<U> flatMap​(java.util.function.Function<? super T,​? extends Option<? extends U>> mapper)
        Maps the value to a new Option if this is a Some, otherwise returns None.
        Type Parameters:
        U - Component type of the resulting Option
        Parameters:
        mapper - A mapper
        Returns:
        a new Option
      • map

        public final <U> Option<U> map​(java.util.function.Function<? super T,​? extends U> mapper)
        Maps the value and wraps it in a new Some if this is a Some, otherwise returns a None.
        Specified by:
        map in interface Value<T>
        Type Parameters:
        U - The new value type
        Parameters:
        mapper - A value mapper
        Returns:
        a new Some containing the mapped value if this Option is defined, otherwise None, if this is empty.
      • fold

        public final <U> U fold​(java.util.function.Supplier<? extends U> ifNone,
                                java.util.function.Function<? super T,​? extends U> f)
        Folds either the None or the Some side of the Option value.
        Type Parameters:
        U - type of the folded value
        Parameters:
        ifNone - maps the left value if this is a None
        f - maps the value if this is a Some
        Returns:
        A value of type U
      • peek

        public final Option<T> peek​(java.util.function.Consumer<? super T> action)
        Applies an action to this value, if this option is defined, otherwise does nothing.
        Specified by:
        peek in interface Value<T>
        Parameters:
        action - An action which can be applied to an optional value
        Returns:
        this Option
      • transform

        public final <U> U transform​(java.util.function.Function<? super Option<T>,​? extends U> f)
        Transforms this Option.
        Type Parameters:
        U - Type of transformation result
        Parameters:
        f - A transformation
        Returns:
        An instance of type U
        Throws:
        java.lang.NullPointerException - if f is null
      • iterator

        public final Iterator<T> iterator()
        Description copied from interface: Iterable
        Returns a rich Iterator that allows us to perform intermediate, sequential operations known from Vavr's collection library.
        Specified by:
        iterator in interface Iterable<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface Value<T>
        Returns:
        an Iterator instance (that might be a singleton in the empty case)