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. *