Class Optional<T>

  • Type Parameters:
    T - the expected type of the nullable value.

    public class Optional<T>
    extends java.lang.Object
    Optional wrapper can be to pass nullable value safely through RxJava2 stream. Though Android supports this construct since API level 24, using it is not viable because the SDK has to support lower API levels.
    • Constructor Summary

      Constructors 
      Constructor Description
      Optional​(T value)
      Constructs a new optional value
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Optional<T> empty()
      Returns an empty Optional instance.
      boolean equals​(java.lang.Object rhs)  
      static <T> Optional<T> from​(T value)  
      T get()
      Gets the optional value if present.
      int hashCode()  
      boolean isPresent()
      Checks if a non-null value is present.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Optional

        public Optional​(@Nullable
                        T value)
        Constructs a new optional value
        Parameters:
        value - the optional value
    • Method Detail

      • from

        public static <T> Optional<T> from​(T value)
      • isPresent

        public boolean isPresent()
        Checks if a non-null value is present.
        Returns:
        true if there is a value present, otherwise false.
      • get

        @NonNull
        public T get()
        Gets the optional value if present.
        Returns:
        reference to the optional value.
        Throws:
        java.util.NoSuchElementException - if the value is null.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object rhs)
        Overrides:
        equals in class java.lang.Object
      • empty

        public static <T> Optional<T> empty()
        Returns an empty Optional instance. No value is present for this Optional.
        Type Parameters:
        T - Type of the optional value
        Returns:
        an empty Optional