Class Optional<T>

  • Type Parameters:
    T - Type of the value

    public final class Optional<T>
    extends java.lang.Object
    A container object that may or may not contain a value.
    Since:
    1.8
    Author:
    Eugen Neufeld
    • 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.
      static <T> Optional<T> fromJavaOptional​(java.util.Optional<T> javaOptional)
      Creates a new Optional from the given java.util.Optional.
      T get()
      If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
      boolean isPresent()
      Return true if there is a value present, otherwise false.
      static <T> Optional<T> of​(T value)
      Returns an Optional with the specified present non-null value.
      static <T> Optional<T> ofNullable​(T value)
      Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
      java.lang.Object orNull()
      Returns the wrapped value, if present, otherwise null.
      • Methods inherited from class java.lang.Object

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

      • 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 non-existent value
        Returns:
        an empty Optional
      • of

        public static <T> Optional<T> of​(T value)
        Returns an Optional with the specified present non-null value.
        Type Parameters:
        T - the class of the value
        Parameters:
        value - the value to be present, which must be non-null
        Returns:
        an Optional with the value present
      • ofNullable

        public static <T> Optional<T> ofNullable​(T value)
        Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
        Type Parameters:
        T - the class of the value
        Parameters:
        value - the possibly-null value to describe
        Returns:
        an Optional with a present value if the specified value is non-null, otherwise an empty Optional
      • fromJavaOptional

        public static <T> Optional<T> fromJavaOptional​(java.util.Optional<T> javaOptional)
        Creates a new Optional from the given java.util.Optional. If the java Optional is empty, an empty Optional is returned. Otherwise, the value is re-wrapped in this Optional.
        Type Parameters:
        T - the class of the value
        Parameters:
        javaOptional - The java.util.Optional to convert to an EMF Forms Optional
        Returns:
        An EMF Forms Optional equivalent to the given Java Optional
        Since:
        1.20
      • isPresent

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

        public T get()
        If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
        Returns:
        the non-null value held by this Optional
        See Also:
        isPresent()
      • orNull

        public java.lang.Object orNull()
        Returns the wrapped value, if present, otherwise null.
        Returns:
        the wrapped value, if present, otherwise null
        Since:
        1.13