Class Option<T>

  • Type Parameters:
    T - element type

    public class Option<T>
    extends java.lang.Object
    Class Option stores a single value which may also be null. This is different from Optional which does not support null values.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static Option EMPTY  
      private boolean hasValue  
      private T value  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Option()
      Prevent construction (use EMPTY)
        Option​(T value)
      Construct option with specified value.
    • Field Detail

      • EMPTY

        private static final Option EMPTY
      • hasValue

        private boolean hasValue
      • value

        private T value
    • Constructor Detail

      • Option

        private Option()
        Prevent construction (use EMPTY)
      • Option

        public Option​(T value)
        Construct option with specified value.
        Parameters:
        value - value
    • Method Detail

      • empty

        public static <EE> Option<EE> empty()
        Returns:
        unmodifiable empty instance
      • of

        public static <T> Option<T> of​(T value)
      • hasValue

        public boolean hasValue()
        Returns true if a value is stored, false otherwise
      • getValueOrNull

        public T getValueOrNull()
        Returns stored value of null if no value is stored. It is therefore not possible to distinguish between a null value stored or no value stored.
      • getValue

        public T getValue()
        Returns stored value. If no value is stored, an exception is thrown.
        Returns:
        stored value
        Throws:
        java.lang.IllegalArgumentException - if no value is stored
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object