src/java.base/share/classes/java/util/Optional.java
changeset 48328 7acf5700d542
parent 47216 71c04702a3d5
child 49823 af4b57a556be
--- a/src/java.base/share/classes/java/util/Optional.java	Wed Dec 13 17:28:24 2017 -0800
+++ b/src/java.base/share/classes/java/util/Optional.java	Wed Dec 13 18:47:20 2017 -0800
@@ -32,8 +32,9 @@
 
 /**
  * A container object which may or may not contain a non-{@code null} value.
- * If a value is present, {@code isPresent()} returns {@code true} and
- * {@code get()} returns the value.
+ * If a value is present, {@code isPresent()} returns {@code true}. If no
+ * value is present, the object is considered <i>empty</i> and
+ * {@code isPresent()} returns {@code false}.
  *
  * <p>Additional methods that depend on the presence or absence of a contained
  * value are provided, such as {@link #orElse(Object) orElse()}
@@ -137,14 +138,10 @@
      * {@code NoSuchElementException}.
      *
      * @apiNote
-     * The methods {@link #orElse(Object) orElse} and
-     * {@link #orElseGet(Supplier) orElseGet}
-     * are generally preferable to this method, as they return a substitute
-     * value if the value is absent, instead of throwing an exception.
+     * The preferred alternative to this method is {@link #orElseThrow()}.
      *
      * @return the non-{@code null} value described by this {@code Optional}
      * @throws NoSuchElementException if no value is present
-     * @see Optional#isPresent()
      */
     public T get() {
         if (value == null) {
@@ -362,6 +359,21 @@
     }
 
     /**
+     * If a value is present, returns the value, otherwise throws
+     * {@code NoSuchElementException}.
+     *
+     * @return the non-{@code null} value described by this {@code Optional}
+     * @throws NoSuchElementException if no value is present
+     * @since 10
+     */
+    public T orElseThrow() {
+        if (value == null) {
+            throw new NoSuchElementException("No value present");
+        }
+        return value;
+    }
+
+    /**
      * If a value is present, returns the value, otherwise throws an exception
      * produced by the exception supplying function.
      *