Class Option<T>


  • public final class Option<T>
    extends Object
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Optional<T> asOptional()
      returns the value as an optional
      Ref<T> asReference()
      returns the value as an reference
      static <T> Option<T> empty()
      Returns an empty Option instance.
      boolean equals​(Object obj)
      Indicates whether some other object is "equal to" this Option.
      Option<T> filter​(Predicate<? super T> predicate)
      If a value is present, and the value matches the given predicate, returns an Option describing the value, otherwise returns an empty Option.
      <U> Option<U> flatMap​(Function<? super T,​? extends Option<? extends U>> mapper)
      If a value is present, returns the result of applying the given Option-bearing mapping function to the value, otherwise returns an empty Option.
      T get()
      returns the value
      int hashCode()
      Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
      void ifAbsent​(Runnable action)
      If a value is absent, performs the given action with the value, otherwise does nothing.
      void ifPresent​(Consumer<? super T> action)
      If a value is present, performs the given action with the value, otherwise does nothing.
      void ifPresentOrElse​(Consumer<? super T> action, Runnable emptyAction)
      If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
      boolean isEmpty()
      If a value is not present, returns true, otherwise false.
      boolean isPresent()
      If a value is present, returns true, otherwise false.
      <U> Option<U> map​(Function<? super T,​? extends U> mapper)
      If a value is present, returns an Option describing the result of applying the given mapping function to the value, otherwise returns an empty Option.
      static <T> Option<T> of​(T value)
      Returns an Option describing the given value.
      Option<T> or​(Supplier<? extends Option<? extends T>> supplier)
      If a value is present, returns an Option describing the value, otherwise returns an Option produced by the supplying function.
      T orElse​(T other)
      If a value is present, returns the value, otherwise returns other.
      T orElseGet​(Supplier<? extends T> supplier)
      If a value is present, returns the value, otherwise returns the result produced by the supplying function.
      T orElseRun​(Runnable runnable)
      If a value is present, returns the value, otherwise runs runnable
      T orElseThrow()
      If a value is present, returns the value, otherwise throws NoSuchElementException.
      <X extends Throwable>
      T
      orElseThrow​(Supplier<? extends X> exceptionSupplier)
      If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
      Stream<T> stream()
      If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
      String toString()
      Returns a non-empty string representation of this Option suitable for debugging.
    • Method Detail

      • empty

        public static <T> Option<T> empty()
        Returns an empty Option instance. No value is present for this Option.
        Type Parameters:
        T - The type of the non-existent value
        Returns:
        an empty Option
      • of

        public static <T> Option<T> of​(T value)
        Returns an Option describing the given value.
        Type Parameters:
        T - the type of the value
        Parameters:
        value - the value to describe
        Returns:
        an Option with the value
      • asOptional

        public Optional<T> asOptional()
        returns the value as an optional
        Returns:
        the optional
      • asReference

        public Ref<T> asReference()
        returns the value as an reference
        Returns:
        the reference
      • get

        public T get()
        returns the value
        Returns:
        the value described by this Option
      • isPresent

        public boolean isPresent()
        If a value is present, returns true, otherwise false.
        Returns:
        true if a value is present, otherwise false
      • isEmpty

        public boolean isEmpty()
        If a value is not present, returns true, otherwise false.
        Returns:
        true if a value is not present, otherwise false
      • ifPresent

        public void ifPresent​(Consumer<? super T> action)
        If a value is present, performs the given action with the value, otherwise does nothing.
        Parameters:
        action - the action to be performed, if a value is present
        Throws:
        NullPointerException - if value is present and the given action is null
      • ifPresentOrElse

        public void ifPresentOrElse​(Consumer<? super T> action,
                                    Runnable emptyAction)
        If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
        Parameters:
        action - the action to be performed, if a value is present
        emptyAction - the empty-based action to be performed, if no value is present
        Throws:
        NullPointerException - if a value is present and the given action is null, or no value is present and the given empty-based action is null.
      • ifAbsent

        public void ifAbsent​(Runnable action)
        If a value is absent, performs the given action with the value, otherwise does nothing.
        Parameters:
        action - the action to be performed, if a value is absent
        Throws:
        NullPointerException - if value is absent and the given action is null
      • filter

        public Option<T> filter​(Predicate<? super T> predicate)
        If a value is present, and the value matches the given predicate, returns an Option describing the value, otherwise returns an empty Option.
        Parameters:
        predicate - the predicate to apply to a value, if present
        Returns:
        an Option describing the value of this Option, if a value is present and the value matches the given predicate, otherwise an empty Option
        Throws:
        NullPointerException - if the predicate is null
      • map

        public <U> Option<U> map​(Function<? super T,​? extends U> mapper)
        If a value is present, returns an Option describing the result of applying the given mapping function to the value, otherwise returns an empty Option.

        If the mapping function returns a null result then this method returns an empty Option.

        Type Parameters:
        U - The type of the value returned from the mapping function
        Parameters:
        mapper - the mapping function to apply to a value, if present
        Returns:
        an Option describing the result of applying a mapping function to the value of this Option, if a value is present, otherwise an empty Option
        Throws:
        NullPointerException - if the mapping function is null
      • flatMap

        public <U> Option<U> flatMap​(Function<? super T,​? extends Option<? extends U>> mapper)
        If a value is present, returns the result of applying the given Option-bearing mapping function to the value, otherwise returns an empty Option.

        This method is similar to map(Function), but the mapping function is one whose result is already an Option, and if invoked, flatMap does not wrap it within an additional Option.

        Type Parameters:
        U - The type of value of the Option returned by the mapping function
        Parameters:
        mapper - the mapping function to apply to a value, if present
        Returns:
        the result of applying an Option-bearing mapping function to the value of this Option, if a value is present, otherwise an empty Option
        Throws:
        NullPointerException - if the mapping function is null or returns a null result
      • or

        public Option<T> or​(Supplier<? extends Option<? extends T>> supplier)
        If a value is present, returns an Option describing the value, otherwise returns an Option produced by the supplying function.
        Parameters:
        supplier - the supplying function that produces an Option to be returned
        Returns:
        returns an Option describing the value of this Option, if a value is present, otherwise an Option produced by the supplying function.
        Throws:
        NullPointerException - if the supplying function is null or produces a null result
      • stream

        public Stream<T> stream()
        If a value is present, returns a sequential Stream containing only that value, otherwise returns an empty Stream.
        Returns:
        the optional value as a Stream
      • orElse

        public T orElse​(T other)
        If a value is present, returns the value, otherwise returns other.
        Parameters:
        other - the value to be returned, if no value is present. May be null.
        Returns:
        the value, if present, otherwise other
      • orElseGet

        public T orElseGet​(Supplier<? extends T> supplier)
        If a value is present, returns the value, otherwise returns the result produced by the supplying function.
        Parameters:
        supplier - the supplying function that produces a value to be returned
        Returns:
        the value, if present, otherwise the result produced by the supplying function
        Throws:
        NullPointerException - if no value is present and the supplying function is null
      • orElseRun

        public T orElseRun​(Runnable runnable)
        If a value is present, returns the value, otherwise runs runnable
        Parameters:
        runnable - the runnable to be execute if no value is present
        Returns:
        the value described by this Option
        Throws:
        NullPointerException - if no value is present and the runnable is null
      • orElseThrow

        public T orElseThrow()
        If a value is present, returns the value, otherwise throws NoSuchElementException.
        Returns:
        the non-null value described by this Option
        Throws:
        NoSuchElementException - if no value is present
      • orElseThrow

        public <X extends ThrowableT orElseThrow​(Supplier<? extends X> exceptionSupplier)
                                            throws X extends Throwable
        If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.
        Type Parameters:
        X - Type of the exception to be thrown
        Parameters:
        exceptionSupplier - the supplying function that produces an exception to be thrown
        Returns:
        the value, if present
        Throws:
        X - if no value is present
        NullPointerException - if no value is present and the exception supplying function is null
        X extends Throwable
      • equals

        public boolean equals​(Object obj)
        Indicates whether some other object is "equal to" this Option. The other object is considered equal if:
        • it is also an Option and;
        • both instances have no value present or;
        • the present values are "equal to" each other via equals().
        Overrides:
        equals in class Object
        Parameters:
        obj - an object to be tested for equality
        Returns:
        true if the other object is "equal to" this object otherwise false
      • hashCode

        public int hashCode()
        Returns the hash code of the value, if present, otherwise 0 (zero) if no value is present.
        Overrides:
        hashCode in class Object
        Returns:
        hash code value of the present value or 0 if no value is present
      • toString

        public String toString()
        Returns a non-empty string representation of this Option suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.
        Overrides:
        toString in class Object
        Returns:
        the string representation of this instance