diff --git a/src/java.base/share/classes/java/util/Optional.java b/src/java.base/share/classes/java/util/Optional.java index 5803d818deb..f4fef8aadef 100644 --- a/src/java.base/share/classes/java/util/Optional.java +++ b/src/java.base/share/classes/java/util/Optional.java @@ -32,8 +32,9 @@ import java.util.stream.Stream; /** * 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 empty and + * {@code isPresent()} returns {@code false}. * *
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 @@ public final class Optional Additional methods that depend on the presence or absence of a contained
* value are provided, such as {@link #orElse(double) orElse()}
@@ -117,14 +118,10 @@ public final class OptionalDouble {
* {@code NoSuchElementException}.
*
* @apiNote
- * The methods {@link #orElse(double) orElse} and
- * {@link #orElseGet(DoubleSupplier) 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 OptionalDouble}
* @throws NoSuchElementException if no value is present
- * @see OptionalDouble#isPresent()
*/
public double getAsDouble() {
if (!isPresent) {
@@ -225,6 +222,21 @@ public final class OptionalDouble {
return isPresent ? value : supplier.getAsDouble();
}
+ /**
+ * If a value is present, returns the value, otherwise throws
+ * {@code NoSuchElementException}.
+ *
+ * @return the value described by this {@code OptionalDouble}
+ * @throws NoSuchElementException if no value is present
+ * @since 10
+ */
+ public double 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.
diff --git a/src/java.base/share/classes/java/util/OptionalInt.java b/src/java.base/share/classes/java/util/OptionalInt.java
index 12e588e56e9..61afe0206d1 100644
--- a/src/java.base/share/classes/java/util/OptionalInt.java
+++ b/src/java.base/share/classes/java/util/OptionalInt.java
@@ -30,9 +30,10 @@ import java.util.function.Supplier;
import java.util.stream.IntStream;
/**
- * A container object which may or may not contain an {@code int} value. If a
- * value is present, {@code isPresent()} returns {@code true} and
- * {@code getAsInt()} returns the value.
+ * A container object which may or may not contain an {@code int} value.
+ * If a value is present, {@code isPresent()} returns {@code true}. If no
+ * value is present, the object is considered empty and
+ * {@code isPresent()} returns {@code false}.
*
* Additional methods that depend on the presence or absence of a contained
* value are provided, such as {@link #orElse(int) orElse()}
@@ -117,14 +118,10 @@ public final class OptionalInt {
* {@code NoSuchElementException}.
*
* @apiNote
- * The methods {@link #orElse(int) orElse} and
- * {@link #orElseGet(IntSupplier) 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 OptionalInt}
* @throws NoSuchElementException if no value is present
- * @see OptionalInt#isPresent()
*/
public int getAsInt() {
if (!isPresent) {
@@ -224,6 +221,21 @@ public final class OptionalInt {
return isPresent ? value : supplier.getAsInt();
}
+ /**
+ * If a value is present, returns the value, otherwise throws
+ * {@code NoSuchElementException}.
+ *
+ * @return the value described by this {@code OptionalInt}
+ * @throws NoSuchElementException if no value is present
+ * @since 10
+ */
+ public int 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.
diff --git a/src/java.base/share/classes/java/util/OptionalLong.java b/src/java.base/share/classes/java/util/OptionalLong.java
index 57dc8e59688..ae8f0a77e3e 100644
--- a/src/java.base/share/classes/java/util/OptionalLong.java
+++ b/src/java.base/share/classes/java/util/OptionalLong.java
@@ -30,9 +30,10 @@ import java.util.function.Supplier;
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 empty and
+ * {@code isPresent()} returns {@code false}.
*
* 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 @@ public final class OptionalLong {
* {@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) {
@@ -224,6 +221,21 @@ public final class OptionalLong {
return isPresent ? value : supplier.getAsLong();
}
+ /**
+ * 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.
diff --git a/test/jdk/java/util/Optional/Basic.java b/test/jdk/java/util/Optional/Basic.java
index b3af86c63ca..0d574d9511f 100644
--- a/test/jdk/java/util/Optional/Basic.java
+++ b/test/jdk/java/util/Optional/Basic.java
@@ -132,6 +132,13 @@ public class Basic {
Boolean got = empty.orElseThrow(ObscureException::new);
}
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyOrElseThrowNoArg() throws Exception {
+ Optional