Class Option<T>

  • Direct Known Subclasses:
    Option.None, Option.Some

    public abstract class Option<T>
    extends java.lang.Object
    Implementation of the Option type pattern
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Option.None<T>
      The None type, representing an absent value (instead of "null")
      static class  Option.Some<T>
      The Some type, representing an existence of some value
    • Constructor Summary

      Constructors 
      Constructor Description
      Option()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Option<T> fromNullable​(T value)
      Wraps value in an Option type, depending on whether or not value is null
      abstract T get()
      Get the value of the Option (if it is defined)
      abstract boolean isDefined()
      Whether the Option is defined or not
      static <T> Option.None<T> none()  
      T or​(T other)
      Get the contained value (if defined) or else return a default value
      static <T> Option.Some<T> some​(T value)
      Wrap value in a Some type (NB!
      • Methods inherited from class java.lang.Object

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

      • Option

        public Option()
    • Method Detail

      • isDefined

        public abstract boolean isDefined()
        Whether the Option is defined or not
        Returns:
        true if the Option is defined (of type Some) false if the Option is not defined (of type None)
      • get

        public abstract T get()
        Get the value of the Option (if it is defined)
        Returns:
        the value
        Throws:
        java.lang.IllegalStateException - if called on a None
      • or

        public T or​(T other)
        Get the contained value (if defined) or else return a default value
        Parameters:
        other - what to return if the value is not defined (a None)
        Returns:
        either the value, or other if the value is not defined
      • fromNullable

        public static <T> Option<T> fromNullable​(T value)
        Wraps value in an Option type, depending on whether or not value is null
        Type Parameters:
        T - type of value
        Parameters:
        value -
        Returns:
        Some(value) if value is not null, None if value is null
      • some

        public static <T> Option.Some<T> some​(T value)
        Wrap value in a Some type (NB! value must not be null!)
        Type Parameters:
        T - type of value
        Parameters:
        value -
        Returns:
        a new Some(value)