--- old/src/java.base/share/classes/java/util/Optional.java 2017-12-07 15:57:36.000000000 -0800 +++ new/src/java.base/share/classes/java/util/Optional.java 2017-12-07 15:57:36.000000000 -0800 @@ -137,14 +137,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 +358,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. * --- old/src/java.base/share/classes/java/util/OptionalDouble.java 2017-12-07 15:57:37.000000000 -0800 +++ new/src/java.base/share/classes/java/util/OptionalDouble.java 2017-12-07 15:57:37.000000000 -0800 @@ -117,14 +117,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 +222,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. * --- old/src/java.base/share/classes/java/util/OptionalInt.java 2017-12-07 15:57:38.000000000 -0800 +++ new/src/java.base/share/classes/java/util/OptionalInt.java 2017-12-07 15:57:38.000000000 -0800 @@ -117,14 +117,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 +221,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. * --- old/src/java.base/share/classes/java/util/OptionalLong.java 2017-12-07 15:57:40.000000000 -0800 +++ new/src/java.base/share/classes/java/util/OptionalLong.java 2017-12-07 15:57:39.000000000 -0800 @@ -117,14 +117,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 +221,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. * --- old/test/jdk/java/util/Optional/Basic.java 2017-12-07 15:57:41.000000000 -0800 +++ new/test/jdk/java/util/Optional/Basic.java 2017-12-07 15:57:41.000000000 -0800 @@ -132,6 +132,13 @@ Boolean got = empty.orElseThrow(ObscureException::new); } + @Test(expectedExceptions=NoSuchElementException.class) + public void testEmptyOrElseThrowNoArg() throws Exception { + Optional empty = Optional.empty(); + + Boolean got = empty.orElseThrow(); + } + @Test(groups = "unit") public void testPresent() { Optional empty = Optional.empty(); @@ -147,6 +154,7 @@ assertTrue(!present.toString().equals(presentEmptyString.toString())); assertTrue(-1 != present.toString().indexOf(Boolean.TRUE.toString())); assertSame(Boolean.TRUE, present.get()); + assertSame(Boolean.TRUE, present.orElseThrow()); AtomicBoolean presentCheck = new AtomicBoolean(); present.ifPresent(v -> presentCheck.set(true)); @@ -191,6 +199,7 @@ instance = Optional.ofNullable("Duke"); assertTrue(instance.isPresent()); assertEquals(instance.get(), "Duke"); + assertEquals(instance.orElseThrow(), "Duke"); } @Test(groups = "unit") @@ -214,11 +223,13 @@ result = duke.filter(s -> s.startsWith("D")); assertTrue(result.isPresent()); assertEquals(result.get(), "Duke"); + assertEquals(result.orElseThrow(), "Duke"); Optional emptyString = Optional.of(""); result = emptyString.filter(String::isEmpty); assertTrue(result.isPresent()); assertEquals(result.get(), ""); + assertEquals(result.orElseThrow(), ""); } @Test(groups = "unit") @@ -287,6 +298,7 @@ l = duke.flatMap(s -> Optional.of(s.length())); assertTrue(l.isPresent()); assertEquals(l.get().intValue(), 4); + assertEquals(l.orElseThrow().intValue(), 4); // Verify same instance l = duke.flatMap(s -> fixture); --- old/test/jdk/java/util/Optional/BasicDouble.java 2017-12-07 15:57:42.000000000 -0800 +++ new/test/jdk/java/util/Optional/BasicDouble.java 2017-12-07 15:57:42.000000000 -0800 @@ -124,6 +124,13 @@ double got = empty.orElseThrow(ObscureException::new); } + @Test(expectedExceptions=NoSuchElementException.class) + public void testEmptyOrElseThrowNoArg() throws Exception { + OptionalDouble empty = OptionalDouble.empty(); + + double got = empty.orElseThrow(); + } + @Test(groups = "unit") public void testPresent() { OptionalDouble empty = OptionalDouble.empty(); @@ -137,7 +144,9 @@ assertTrue(Double.hashCode(1.0) == present.hashCode()); assertFalse(present.toString().isEmpty()); assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString())); + assertTrue(-1 != present.toString().indexOf(Double.toString(present.orElseThrow()).toString())); assertEquals(1.0, present.getAsDouble()); + assertEquals(1.0, present.orElseThrow()); AtomicBoolean presentCheck = new AtomicBoolean(); present.ifPresent(v -> presentCheck.set(true)); --- old/test/jdk/java/util/Optional/BasicInt.java 2017-12-07 15:57:43.000000000 -0800 +++ new/test/jdk/java/util/Optional/BasicInt.java 2017-12-07 15:57:43.000000000 -0800 @@ -124,6 +124,13 @@ int got = empty.orElseThrow(ObscureException::new); } + @Test(expectedExceptions=NoSuchElementException.class) + public void testEmptyOrElseThrowNoArg() throws Exception { + OptionalInt empty = OptionalInt.empty(); + + int got = empty.orElseThrow(); + } + @Test(groups = "unit") public void testPresent() { OptionalInt empty = OptionalInt.empty(); @@ -137,7 +144,9 @@ assertTrue(Integer.hashCode(1) == present.hashCode()); assertFalse(present.toString().isEmpty()); assertTrue(-1 != present.toString().indexOf(Integer.toString(present.getAsInt()).toString())); + assertTrue(-1 != present.toString().indexOf(Integer.toString(present.orElseThrow()).toString())); assertEquals(1, present.getAsInt()); + assertEquals(1, present.orElseThrow()); AtomicBoolean presentCheck = new AtomicBoolean(); present.ifPresent(v -> presentCheck.set(true)); --- old/test/jdk/java/util/Optional/BasicLong.java 2017-12-07 15:57:45.000000000 -0800 +++ new/test/jdk/java/util/Optional/BasicLong.java 2017-12-07 15:57:44.000000000 -0800 @@ -124,6 +124,13 @@ long got = empty.orElseThrow(ObscureException::new); } + @Test(expectedExceptions=NoSuchElementException.class) + public void testEmptyOrElseThrowNoArg() throws Exception { + OptionalLong empty = OptionalLong.empty(); + + long got = empty.orElseThrow(); + } + @Test(groups = "unit") public void testPresent() { OptionalLong empty = OptionalLong.empty(); @@ -137,7 +144,9 @@ assertTrue(Long.hashCode(1) == present.hashCode()); assertFalse(present.toString().isEmpty()); assertTrue(-1 != present.toString().indexOf(Long.toString(present.getAsLong()).toString())); + assertTrue(-1 != present.toString().indexOf(Long.toString(present.orElseThrow()).toString())); assertEquals(1L, present.getAsLong()); + assertEquals(1L, present.orElseThrow()); AtomicBoolean presentCheck = new AtomicBoolean(); present.ifPresent(v -> presentCheck.set(true));