Class Option<T>

  • Type Parameters:
    T - The value type.

    public final class Option<T>
    extends java.lang.Object
    The Option type to describe tri-state. Every Option instance is in one of the three states: a state representing a non-null-value, null-value, or no-value.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Option<T> empty()
      Returns an Option with null-value.
      boolean equals​(java.lang.Object obj)
      Indicates whether some other object is "equal to" this Option.
      T getValue()
      Gets the value in the Option.
      int hashCode()
      Returns hash code of the value this Option is initialized with or -1 if in uninitialized state.
      boolean isInitialized()
      Return true if this instance is initialized with a null-value or non-null-value, otherwise false.
      static <T> Option<T> of​(T value)
      Returns an Option with the specified null-value or non-null-value.
      static <T> Option<T> uninitialized()
      Returns an Option instance with no-value.
      • Methods inherited from class java.lang.Object

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

      • of

        public static <T> Option<T> of​(T value)
        Returns an Option with the specified null-value or non-null-value.
        Type Parameters:
        T - The value type.
        Parameters:
        value - the value.
        Returns:
        an Option with the value present.
      • empty

        public static <T> Option<T> empty()
        Returns an Option with null-value.

        Option.empty() is a syntactic sugar for Option.of(null).

        Type Parameters:
        T - The value type.
        Returns:
        an Option with a null-value.
      • uninitialized

        public static <T> Option<T> uninitialized()
        Returns an Option instance with no-value.
        Type Parameters:
        T - Type of the non-existent value.
        Returns:
        An Option type with no-value.
      • isInitialized

        public boolean isInitialized()
        Return true if this instance is initialized with a null-value or non-null-value, otherwise false.
        Returns:
        true if a value has been initialized, otherwise false
      • getValue

        public T getValue()
        Gets the value in the Option.
        Returns:
        The null (null-value) or non-null-value that the Option is initialized with.
        Throws:
        java.util.NoSuchElementException - thrown if the Option is in no-value state.
      • equals

        public boolean equals​(java.lang.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 are not initialized or;
        • both instances are initialized and values are "equal to" each other via equals().
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - an object to be tested for equality
        Returns:
        {code true} if the other object is "equal to" this object otherwise false
      • hashCode

        public int hashCode()
        Returns hash code of the value this Option is initialized with or -1 if in uninitialized state.

        The value 0 will be returned when initialized with null.

        Overrides:
        hashCode in class java.lang.Object
        Returns:
        hash code of the value this Option is initialized with or -1 if in uninitialized state.