src/java.base/share/classes/java/util/OptionalLong.java
changeset 48328 7acf5700d542
parent 47216 71c04702a3d5
child 49823 af4b57a556be
--- a/src/java.base/share/classes/java/util/OptionalLong.java	Wed Dec 13 17:28:24 2017 -0800
+++ b/src/java.base/share/classes/java/util/OptionalLong.java	Wed Dec 13 18:47:20 2017 -0800
@@ -30,9 +30,10 @@
 import java.util.stream.LongStream;
 
 /**
- * A container object which may or may not contain a {@code long} value.  If a
- * value is present, {@code isPresent()} returns {@code true} and
- * {@code getAsLong()} returns the value.
+ * A container object which may or may not contain a {@code long} 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(long) orElse()}
@@ -117,14 +118,10 @@
      * {@code NoSuchElementException}.
      *
      * @apiNote
-     * The methods {@link #orElse(long) orElse} and
-     * {@link #orElseGet(LongSupplier) 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 value described by this {@code OptionalLong}
      * @throws NoSuchElementException if no value is present
-     * @see OptionalLong#isPresent()
      */
     public long getAsLong() {
         if (!isPresent) {
@@ -225,6 +222,21 @@
     }
 
     /**
+     * If a value is present, returns the value, otherwise throws
+     * {@code NoSuchElementException}.
+     *
+     * @return the value described by this {@code OptionalLong}
+     * @throws NoSuchElementException if no value is present
+     * @since 10
+     */
+    public long orElseThrow() {
+        if (!isPresent) {
+            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.
      *