src/share/classes/java/util/Optional.java

Print this page
rev 8107 : 8025610: Add explicit @throws NPE documentation to Optional constructor and Optional.of
Reviewed-by: duke


  68      * Optional.
  69      *
  70      * @apiNote Though it may be tempting to do so, avoid testing if an object
  71      * is empty by comparing with {@code ==} against instances returned by
  72      * {@code Option.empty()}. There is no guarantee that it is a singleton.
  73      * Instead, use {@link #isPresent()}.
  74      *
  75      * @param <T> Type of the non-existent value
  76      * @return an empty {@code Optional}
  77      */
  78     public static<T> Optional<T> empty() {
  79         @SuppressWarnings("unchecked")
  80         Optional<T> t = (Optional<T>) EMPTY;
  81         return t;
  82     }
  83 
  84     /**
  85      * Constructs an instance with the value present.
  86      *
  87      * @param value the non-null value to be present

  88      */
  89     private Optional(T value) {
  90         this.value = Objects.requireNonNull(value);
  91     }
  92 
  93     /**
  94      * Returns an {@code Optional} with the specified present non-null value.
  95      *
  96      * @param <T> the class of the value
  97      * @param value the value to be present, which must be non-null
  98      * @return an {@code Optional} with the value present

  99      */
 100     public static <T> Optional<T> of(T value) {
 101         return new Optional<>(value);
 102     }
 103 
 104     /**
 105      * Returns an {@code Optional} describing the specified value, if non-null,
 106      * otherwise returns an empty {@code Optional}.
 107      *
 108      * @param <T> the class of the value
 109      * @param value the possibly-null value to describe
 110      * @return an {@code Optional} with a present value if the specified value
 111      * is non-null, otherwise an empty {@code Optional}
 112      */
 113     public static <T> Optional<T> ofNullable(T value) {
 114         return value == null ? empty() : of(value);
 115     }
 116 
 117     /**
 118      * If a value is present in this {@code Optional}, returns the value,




  68      * Optional.
  69      *
  70      * @apiNote Though it may be tempting to do so, avoid testing if an object
  71      * is empty by comparing with {@code ==} against instances returned by
  72      * {@code Option.empty()}. There is no guarantee that it is a singleton.
  73      * Instead, use {@link #isPresent()}.
  74      *
  75      * @param <T> Type of the non-existent value
  76      * @return an empty {@code Optional}
  77      */
  78     public static<T> Optional<T> empty() {
  79         @SuppressWarnings("unchecked")
  80         Optional<T> t = (Optional<T>) EMPTY;
  81         return t;
  82     }
  83 
  84     /**
  85      * Constructs an instance with the value present.
  86      *
  87      * @param value the non-null value to be present
  88      * @throws NullPointerException if value is null
  89      */
  90     private Optional(T value) {
  91         this.value = Objects.requireNonNull(value);
  92     }
  93 
  94     /**
  95      * Returns an {@code Optional} with the specified present non-null value.
  96      *
  97      * @param <T> the class of the value
  98      * @param value the value to be present, which must be non-null
  99      * @return an {@code Optional} with the value present
 100      * @throws NullPointerException if value is null
 101      */
 102     public static <T> Optional<T> of(T value) {
 103         return new Optional<>(value);
 104     }
 105 
 106     /**
 107      * Returns an {@code Optional} describing the specified value, if non-null,
 108      * otherwise returns an empty {@code Optional}.
 109      *
 110      * @param <T> the class of the value
 111      * @param value the possibly-null value to describe
 112      * @return an {@code Optional} with a present value if the specified value
 113      * is non-null, otherwise an empty {@code Optional}
 114      */
 115     public static <T> Optional<T> ofNullable(T value) {
 116         return value == null ? empty() : of(value);
 117     }
 118 
 119     /**
 120      * If a value is present in this {@code Optional}, returns the value,