< prev index next >

src/java.base/share/classes/java/util/OptionalDouble.java

Print this page
rev 48216 : 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
Reviewed-by: XXX

*** 115,132 **** /** * If a value is present, returns the value, otherwise throws * {@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. * * @return the value described by this {@code OptionalDouble} * @throws NoSuchElementException if no value is present - * @see OptionalDouble#isPresent() */ public double getAsDouble() { if (!isPresent) { throw new NoSuchElementException("No value present"); } --- 115,128 ---- /** * If a value is present, returns the value, otherwise throws * {@code NoSuchElementException}. * * @apiNote ! * The preferred alternative to this method is {@link #orElseThrow()}. * * @return the value described by this {@code OptionalDouble} * @throws NoSuchElementException if no value is present */ public double getAsDouble() { if (!isPresent) { throw new NoSuchElementException("No value present"); }
*** 224,233 **** --- 220,244 ---- public double orElseGet(DoubleSupplier supplier) { 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. * * @apiNote * A method reference to the exception constructor with an empty argument
< prev index next >