# HG changeset patch # User smarks # Date 1513219640 28800 # Node ID 7acf5700d5428e0172cb4c174f6383d5121dbbab # Parent d2a837cf9ff114d58612d4709766db989be6157e 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get() Reviewed-by: alanb, bpb, forax, darcy diff -r d2a837cf9ff1 -r 7acf5700d542 src/java.base/share/classes/java/util/Optional.java --- 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 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 @@ * {@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. * diff -r d2a837cf9ff1 -r 7acf5700d542 src/java.base/share/classes/java/util/OptionalDouble.java --- a/src/java.base/share/classes/java/util/OptionalDouble.java Wed Dec 13 17:28:24 2017 -0800 +++ b/src/java.base/share/classes/java/util/OptionalDouble.java Wed Dec 13 18:47:20 2017 -0800 @@ -30,9 +30,10 @@ import java.util.stream.DoubleStream; /** - * A container object which may or may not contain a {@code double} value. If a - * value is present, {@code isPresent()} returns {@code true} and - * {@code getAsDouble()} returns the value. + * A container object which may or may not contain a {@code double} 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(double) orElse()} @@ -117,14 +118,10 @@ * {@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) { @@ -226,6 +223,21 @@ } /** + * 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 -r d2a837cf9ff1 -r 7acf5700d542 src/java.base/share/classes/java/util/OptionalInt.java --- a/src/java.base/share/classes/java/util/OptionalInt.java Wed Dec 13 17:28:24 2017 -0800 +++ b/src/java.base/share/classes/java/util/OptionalInt.java Wed Dec 13 18:47:20 2017 -0800 @@ -30,9 +30,10 @@ 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 @@ * {@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) { @@ -225,6 +222,21 @@ } /** + * 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 -r d2a837cf9ff1 -r 7acf5700d542 src/java.base/share/classes/java/util/OptionalLong.java --- 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 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 @@
* {@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.
*
diff -r d2a837cf9ff1 -r 7acf5700d542 test/jdk/java/util/Optional/Basic.java
--- a/test/jdk/java/util/Optional/Basic.java Wed Dec 13 17:28:24 2017 -0800
+++ b/test/jdk/java/util/Optional/Basic.java Wed Dec 13 18:47:20 2017 -0800
@@ -132,6 +132,13 @@
Boolean got = empty.orElseThrow(ObscureException::new);
}
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyOrElseThrowNoArg() throws Exception {
+ Optional