8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()

Reviewed-by: alanb, bpb, forax, darcy
This commit is contained in:
Stuart Marks 2017-12-13 18:47:20 -08:00
parent 0214135fde
commit feffd15dc2
8 changed files with 118 additions and 31 deletions

View file

@ -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 <i>empty</i> and
* {@code isPresent()} returns {@code false}.
*
* <p>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.