Interface Option<T>

Type Parameters:
T - The type of the optional value.
All Superinterfaces:
Iterable<T>, Serializable, Value<T>
All Known Implementing Classes:
Option.None, Option.Some

public interface Option<T> extends Value<T>, 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.

  • Field Details

  • Method Details

    • of

      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

      static <T> Option<Seq<T>> sequence(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:
      NullPointerException - if values is null
    • traverse

      static <T, U> Option<Seq<U>> traverse(Iterable<? extends T> values, 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:
      NullPointerException - if values or f is null.
    • some

      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

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

      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

      static <T> Option<T> when(boolean condition, 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:
      NullPointerException - if the given supplier is null
    • when

      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

      static <T> Option<T> ofOptional(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

      default <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:
      NullPointerException - if partialFunction is null
    • isEmpty

      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

      default Option<T> onEmpty(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

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

      default 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

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

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

      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:
      NoSuchElementException - if this is a None.
    • getOrElse

      default 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

      default 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

      default Option<T> orElse(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

      default T getOrElse(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

      default <X extends Throwable> T getOrElseThrow(Supplier<X> exceptionSupplier) throws X
      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
    • filter

      default Option<T> filter(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
    • flatMap

      default <U> Option<U> flatMap(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

      default <U> Option<U> map(Function<? super T,? extends U> mapper)
      Maps the value and wraps it in a new Some if this is a Some, returns 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

      default <U> U fold(Supplier<? extends U> ifNone, 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

      default Option<T> peek(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

      default <U> U transform(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:
      NullPointerException - if f is null
    • iterator

      default Iterator<T> iterator()
      Description copied from interface: Value
      Returns a rich io.vavr.collection.Iterator.
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface Value<T>
      Returns:
      A new Iterator
    • equals

      boolean equals(Object o)
      Description copied from interface: Value
      Clarifies that values have a proper equals() method implemented.

      See Object.equals(Object).

      Specified by:
      equals in interface Value<T>
      Overrides:
      equals in class Object
      Parameters:
      o - An object
      Returns:
      true, if this equals o, false otherwise
    • hashCode

      int hashCode()
      Description copied from interface: Value
      Clarifies that values have a proper hashCode() method implemented.

      See Object.hashCode().

      Specified by:
      hashCode in interface Value<T>
      Overrides:
      hashCode in class Object
      Returns:
      The hashcode of this object
    • toString

      String toString()
      Description copied from interface: Value
      Clarifies that values have a proper toString() method implemented.

      See Object.toString().

      Specified by:
      toString in interface Value<T>
      Overrides:
      toString in class Object
      Returns:
      A String representation of this object