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

public interface Option<T> extends ReadOnlyCollection<T>
An Option is a read-only collection of one element or of zero elements.

Instances of Option are either an instance of Option.Some or of Option.None.

This class has been derived from 'vavr' Option.java.

Option.java. Copyright 2023 (c) vavr. MIT License.
github.com
  • Method Details

    • none

      static <T> Option<T> none()
    • some

      static <T> Option<T> some(T value)
    • of

      static <T> Option<T> of(T value)
      Creates a new Option of a given value.
      
       // = Some(3), an Option which contains the value 3
       Option<Integer> option = Option.of(3);
      
       // = None, the empty Option
       Option<Integer> none = Option.of(null);
       
      Type Parameters:
      T - type of the value
      Parameters:
      value - A value
      Returns:
      Some(value) if value is not null, None otherwise
    • isEmpty

      boolean isEmpty()
      Returns true, if this is None, otherwise false, if this is Some.
      
       // Prints "false"
       System.out.println(Option.of(10).isEmpty());
      
       // Prints "true"
       System.out.println(Option.none().isEmpty());
       
      Specified by:
      isEmpty in interface ReadOnlyCollection<T>
      Returns:
      true, if this Option is empty, false otherwise
    • orElse

      @NonNull Option<T> orElse(@NonNull Option<? extends T> other)
      Returns this Option if it is nonempty, otherwise return the alternative.
      
       Option<String> other = Option.of("Other");
      
       // = Some("Hello World")
       Option.of("Hello World").orElse(other);
      
       // = Some("Other")
       Option.none().orElse(other);
       
      Parameters:
      other - An alternative Option
      Returns:
      this Option if it is nonempty, otherwise return the alternative.
    • getOrElse

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

      
       // Prints "Hello"
       System.out.println(Option.of("Hello").getOrElse("World"));
      
       // Prints "World"
       Option.none().getOrElse("World");
       
      Parameters:
      other - An alternative value
      Returns:
      This value, if this Option is defined or the other value, if this Option is empty.
    • get

      @Nullable T get()
      Gets the value if this is a Some or throws if this is a None.
      
       // Prints "57"
       System.out.println(Option.of(57).get());
      
       // Throws a NoSuchElementException
       Option.none().get();
       
      Returns:
      the value
      Throws:
      NoSuchElementException - if this is a None.
    • orNull

      @Nullable T orNull()
      Returns this Option if this is defined, or null if it is empty.
      
       // = Some("Hello World")
       Option.of("Hello World").orNull();
      
       // = null
       Option.none().orNull();
       
      Returns:
      this value if it is defined, or null if it is empty.
    • orThrow

      @Nullable T orThrow()
      Returns this Option if this is defined, or throws a NoSuchElementException if it is empty.
      
       // = Some("Hello World")
       Option.of("Hello World").orThrow();
      
       // = null
       Option.none().orThrow();
       
      Returns:
      this value if it is defined, or null if it is empty.