8080418: Add Optional.or()
authorpsandoz
Mon, 19 Oct 2015 11:28:50 -0700
changeset 33238 80720cbea36f
parent 33237 566786747ea9
child 33239 8e750d9dc3ec
child 33274 f5cfc8204b6a
child 33654 bf582aee36a4
8080418: Add Optional.or() Reviewed-by: chegar, forax, scolebourne
jdk/src/java.base/share/classes/java/util/Optional.java
jdk/src/java.base/share/classes/java/util/OptionalDouble.java
jdk/src/java.base/share/classes/java/util/OptionalInt.java
jdk/src/java.base/share/classes/java/util/OptionalLong.java
jdk/test/java/util/Optional/Basic.java
--- a/jdk/src/java.base/share/classes/java/util/Optional.java	Mon Oct 19 19:14:29 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/util/Optional.java	Mon Oct 19 11:28:50 2015 -0700
@@ -31,21 +31,22 @@
 import java.util.stream.Stream;
 
 /**
- * A container object which may or may not contain a non-null value.
- * If a value is present, {@code isPresent()} will return {@code true} and
- * {@code get()} will return the value.
+ * 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.
  *
  * <p>Additional methods that depend on the presence or absence of a contained
  * value are provided, such as {@link #orElse(java.lang.Object) orElse()}
- * (return a default value if value not present) and
- * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (perform an
- * action if the value is present).
+ * (returns a default value if no value is present) and
+ * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (performs an
+ * action if a value is present).
  *
  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  * class; use of identity-sensitive operations (including reference equality
  * ({@code ==}), identity hash code, or synchronization) on instances of
  * {@code Optional} may have unpredictable results and should be avoided.
  *
+ * @param <T> the type of value
  * @since 1.8
  */
 public final class Optional<T> {
@@ -71,14 +72,15 @@
 
     /**
      * Returns an empty {@code Optional} instance.  No value is present for this
-     * Optional.
+     * {@code Optional}.
      *
-     * @apiNote Though it may be tempting to do so, avoid testing if an object
-     * is empty by comparing with {@code ==} against instances returned by
-     * {@code Option.empty()}. There is no guarantee that it is a singleton.
+     * @apiNote
+     * Though it may be tempting to do so, avoid testing if an object is empty
+     * by comparing with {@code ==} against instances returned by
+     * {@code Optional.empty()}.  There is no guarantee that it is a singleton.
      * Instead, use {@link #isPresent()}.
      *
-     * @param <T> Type of the non-existent value
+     * @param <T> The type of the non-existent value
      * @return an empty {@code Optional}
      */
     public static<T> Optional<T> empty() {
@@ -88,47 +90,47 @@
     }
 
     /**
-     * Constructs an instance with the value present.
+     * Constructs an instance with the described value.
      *
-     * @param value the non-null value to be present
-     * @throws NullPointerException if value is null
+     * @param value the non-{@code null} value to describe
+     * @throws NullPointerException if value is {@code null}
      */
     private Optional(T value) {
         this.value = Objects.requireNonNull(value);
     }
 
     /**
-     * Returns an {@code Optional} with the specified present non-null value.
+     * Returns an {@code Optional} describing the given non-{@code null}
+     * value.
      *
-     * @param <T> the class of the value
-     * @param value the value to be present, which must be non-null
+     * @param value the value to describe, which must be non-{@code null}
+     * @param <T> the type of the value
      * @return an {@code Optional} with the value present
-     * @throws NullPointerException if value is null
+     * @throws NullPointerException if value is {@code null}
      */
     public static <T> Optional<T> of(T value) {
         return new Optional<>(value);
     }
 
     /**
-     * Returns an {@code Optional} describing the specified value, if non-null,
-     * otherwise returns an empty {@code Optional}.
+     * Returns an {@code Optional} describing the given value, if
+     * non-{@code null}, otherwise returns an empty {@code Optional}.
      *
-     * @param <T> the class of the value
-     * @param value the possibly-null value to describe
+     * @param value the possibly-{@code null} value to describe
+     * @param <T> the type of the value
      * @return an {@code Optional} with a present value if the specified value
-     * is non-null, otherwise an empty {@code Optional}
+     *         is non-{@code null}, otherwise an empty {@code Optional}
      */
     public static <T> Optional<T> ofNullable(T value) {
         return value == null ? empty() : of(value);
     }
 
     /**
-     * If a value is present in this {@code Optional}, returns the value,
-     * otherwise throws {@code NoSuchElementException}.
+     * If a value is present, returns the value, otherwise throws
+     * {@code NoSuchElementException}.
      *
-     * @return the non-null value held by this {@code Optional}
-     * @throws NoSuchElementException if there is no value present
-     *
+     * @return the non-{@code null} value described by this {@code Optional}
+     * @throws NoSuchElementException if no value is present
      * @see Optional#isPresent()
      */
     public T get() {
@@ -139,21 +141,21 @@
     }
 
     /**
-     * Return {@code true} if there is a value present, otherwise {@code false}.
+     * If a value is present, returns {@code true}, otherwise {@code false}.
      *
-     * @return {@code true} if there is a value present, otherwise {@code false}
+     * @return {@code true} if a value is present, otherwise {@code false}
      */
     public boolean isPresent() {
         return value != null;
     }
 
     /**
-     * If a value is present, perform the given action with the value,
-     * otherwise do nothing.
+     * If a value is present, performs the given action with the value,
+     * otherwise does nothing.
      *
-     * @param action the action to be performed if a value is present
-     * @throws NullPointerException if a value is present and {@code action} is
-     * null
+     * @param action the action to be performed, if a value is present
+     * @throws NullPointerException if value is present and the given action is
+     *         {@code null}
      */
     public void ifPresent(Consumer<? super T> action) {
         if (value != null) {
@@ -162,15 +164,16 @@
     }
 
     /**
-     * If a value is present, perform the given action with the value,
-     * otherwise perform the given empty-based action.
+     * If a value is present, performs the given action with the value,
+     * otherwise performs the given empty-based action.
      *
-     * @param action the action to be performed if a value is present
-     * @param emptyAction the empty-based action to be performed if a value is
-     * not present
-     * @throws NullPointerException if a value is present and {@code action} is
-     * null, or a value is not present and {@code emptyAction} is null.
-     * @since 1.9
+     * @param action the action to be performed, if a value is present
+     * @param emptyAction the empty-based action to be performed, if no value is
+     *        present
+     * @throws NullPointerException if a value is present and the given action
+     *         is {@code null}, or no value is present and the given empty-based
+     *         action is {@code null}.
+     * @since 9
      */
     public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
         if (value != null) {
@@ -182,14 +185,14 @@
 
     /**
      * If a value is present, and the value matches the given predicate,
-     * return an {@code Optional} describing the value, otherwise return an
+     * returns an {@code Optional} describing the value, otherwise returns an
      * empty {@code Optional}.
      *
-     * @param predicate a predicate to apply to the value, if present
-     * @return an {@code Optional} describing the value of this {@code Optional}
-     * if a value is present and the value matches the given predicate,
-     * otherwise an empty {@code Optional}
-     * @throws NullPointerException if the predicate is null
+     * @param predicate the predicate to apply to a value, if present
+     * @return an {@code Optional} describing the value of this
+     *         {@code Optional}, if a value is present and the value matches the
+     *         given predicate, otherwise an empty {@code Optional}
+     * @throws NullPointerException if the predicate is {@code null}
      */
     public Optional<T> filter(Predicate<? super T> predicate) {
         Objects.requireNonNull(predicate);
@@ -201,14 +204,18 @@
     }
 
     /**
-     * If a value is present, apply the provided mapping function to it,
-     * and if the result is non-null, return an {@code Optional} describing the
-     * result.  Otherwise return an empty {@code Optional}.
+     * If a value is present, returns an {@code Optional} describing (as if by
+     * {@link #ofNullable}) the result of applying the given mapping function to
+     * the value, otherwise returns an empty {@code Optional}.
      *
-     * @apiNote This method supports post-processing on optional values, without
+     * <p>If the mapping function returns a {@code null} result then this method
+     * returns an empty {@code Optional}.
+     *
+     * @apiNote
+     * This method supports post-processing on {@code Optional} values, without
      * the need to explicitly check for a return status.  For example, the
-     * following code traverses a stream of file names, selects one that has
-     * not yet been processed, and then opens that file, returning an
+     * following code traverses a stream of file names, selects one that has not
+     * yet been processed, and then opens that file, returning an
      * {@code Optional<FileInputStream>}:
      *
      * <pre>{@code
@@ -222,12 +229,12 @@
      * {@code map} returns an {@code Optional<FileInputStream>} for the desired
      * file if one exists.
      *
-     * @param <U> The type of the result of the mapping function
-     * @param mapper a mapping function to apply to the value, if present
+     * @param mapper the mapping function to apply to a value, if present
+     * @param <U> The type of the value returned from the mapping function
      * @return an {@code Optional} describing the result of applying a mapping
-     * function to the value of this {@code Optional}, if a value is present,
-     * otherwise an empty {@code Optional}
-     * @throws NullPointerException if the mapping function is null
+     *         function to the value of this {@code Optional}, if a value is
+     *         present, otherwise an empty {@code Optional}
+     * @throws NullPointerException if the mapping function is {@code null}
      */
     public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
         Objects.requireNonNull(mapper);
@@ -239,21 +246,23 @@
     }
 
     /**
-     * If a value is present, apply the provided {@code Optional}-bearing
-     * mapping function to it, return that result, otherwise return an empty
-     * {@code Optional}.  This method is similar to {@link #map(Function)},
-     * but the provided mapper is one whose result is already an {@code Optional},
-     * and if invoked, {@code flatMap} does not wrap it with an additional
+     * If a value is present, returns the result of applying the given
+     * {@code Optional}-bearing mapping function to the value, otherwise returns
+     * an empty {@code Optional}.
+     *
+     * <p>This method is similar to {@link #map(Function)}, but the mapping
+     * function is one whose result is already an {@code Optional}, and if
+     * invoked, {@code flatMap} does not wrap it within an additional
      * {@code Optional}.
      *
-     * @param <U> The type parameter to the {@code Optional} returned by
-     * @param mapper a mapping function to apply to the value, if present
-     *           the mapping function
+     * @param <U> The type of value of the {@code Optional} returned by the
+     *            mapping function
+     * @param mapper the mapping function to apply to a value, if present
      * @return the result of applying an {@code Optional}-bearing mapping
-     * function to the value of this {@code Optional}, if a value is present,
-     * otherwise an empty {@code Optional}
-     * @throws NullPointerException if the mapping function is null or returns
-     * a null result
+     *         function to the value of this {@code Optional}, if a value is
+     *         present, otherwise an empty {@code Optional}
+     * @throws NullPointerException if the mapping function is {@code null} or
+     *         returns a {@code null} result
      */
     public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
         Objects.requireNonNull(mapper);
@@ -265,19 +274,41 @@
     }
 
     /**
-     * If a value is present return a sequential {@link Stream} containing only
-     * that value, otherwise return an empty {@code Stream}.
+     * If a value is present, returns an {@code Optional} describing the value,
+     * otherwise returns an {@code Optional} produced by the supplying function.
      *
-     * @apiNote This method can be used to transform a {@code Stream} of
-     * optional elements to a {@code Stream} of present value elements:
+     * @param supplier the supplying function that produces an {@code Optional}
+     *        to be returned
+     * @return returns an {@code Optional} describing the value of this
+     *         {@code Optional}, if a value is present, otherwise an
+     *         {@code Optional} produced by the supplying function.
+     * @throws NullPointerException if the supplying function is {@code null} or
+     *         produces a {@code null} result
+     * @since 9
+     */
+    public Optional<T> or(Supplier<Optional<T>> supplier) {
+        Objects.requireNonNull(supplier);
+        if (isPresent()) {
+            return this;
+        } else {
+            return Objects.requireNonNull(supplier.get());
+        }
+    }
+
+    /**
+     * If a value is present, returns a sequential {@link Stream} containing
+     * only that value, otherwise returns an empty {@code Stream}.
      *
+     * @apiNote
+     * This method can be used to transform a {@code Stream} of optional
+     * elements to a {@code Stream} of present value elements:
      * <pre>{@code
      *     Stream<Optional<T>> os = ..
      *     Stream<T> s = os.flatMap(Optional::stream)
      * }</pre>
      *
      * @return the optional value as a {@code Stream}
-     * @since 1.9
+     * @since 9
      */
     public Stream<T> stream() {
         if (!isPresent()) {
@@ -288,10 +319,11 @@
     }
 
     /**
-     * Return the value if present, otherwise return {@code other}.
+     * If a value is present, returns the value, otherwise returns
+     * {@code other}.
      *
-     * @param other the value to be returned if there is no value present, may
-     * be null
+     * @param other the value to be returned, if no value is present.
+     *        May be {@code null}.
      * @return the value, if present, otherwise {@code other}
      */
     public T orElse(T other) {
@@ -299,34 +331,35 @@
     }
 
     /**
-     * Return the value if present, otherwise invoke {@code other} and return
-     * the result of that invocation.
+     * If a value is present, returns the value, otherwise returns the result
+     * produced by the supplying function.
      *
-     * @param other a {@code Supplier} whose result is returned if no value
-     * is present
-     * @return the value if present otherwise the result of {@code other.get()}
-     * @throws NullPointerException if value is not present and {@code other} is
-     * null
+     * @param supplier the supplying function that produces a value to be returned
+     * @return the value, if present, otherwise the result produced by the
+     *         supplying function
+     * @throws NullPointerException if no value is present and the supplying
+     *         function is {@code null}
      */
-    public T orElseGet(Supplier<? extends T> other) {
-        return value != null ? value : other.get();
+    public T orElseGet(Supplier<? extends T> supplier) {
+        return value != null ? value : supplier.get();
     }
 
     /**
-     * Return the contained value, if present, otherwise throw an exception
-     * to be created by the provided supplier.
+     * 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 list can be used as the supplier. For example,
+     * @apiNote
+     * A method reference to the exception constructor with an empty argument
+     * list can be used as the supplier. For example,
      * {@code IllegalStateException::new}
      *
      * @param <X> Type of the exception to be thrown
-     * @param exceptionSupplier The supplier which will return the exception to
-     * be thrown
-     * @return the present value
-     * @throws X if there is no value present
-     * @throws NullPointerException if no value is present and
-     * {@code exceptionSupplier} is null
+     * @param exceptionSupplier the supplying function that produces an
+     *        exception to be thrown
+     * @return the value, if present
+     * @throws X if no value is present
+     * @throws NullPointerException if no value is present and the exception
+     *          supplying function is {@code null}
      */
     public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
         if (value != null) {
@@ -337,8 +370,8 @@
     }
 
     /**
-     * Indicates whether some other object is "equal to" this Optional. The
-     * other object is considered equal if:
+     * Indicates whether some other object is "equal to" this {@code Optional}.
+     * The other object is considered equal if:
      * <ul>
      * <li>it is also an {@code Optional} and;
      * <li>both instances have no value present or;
@@ -347,7 +380,7 @@
      *
      * @param obj an object to be tested for equality
      * @return {@code true} if the other object is "equal to" this object
-     * otherwise {@code false}
+     *         otherwise {@code false}
      */
     @Override
     public boolean equals(Object obj) {
@@ -364,10 +397,11 @@
     }
 
     /**
-     * Returns the hash code value of the present value, if any, or 0 (zero) if
-     * no value is present.
+     * Returns the hash code of the value, if present, otherwise {@code 0}
+     * (zero) if no value is present.
      *
-     * @return hash code value of the present value or 0 if no value is present
+     * @return hash code value of the present value or {@code 0} if no value is
+     *         present
      */
     @Override
     public int hashCode() {
@@ -375,13 +409,14 @@
     }
 
     /**
-     * Returns a non-empty string representation of this Optional suitable for
-     * debugging. The exact presentation format is unspecified and may vary
-     * between implementations and versions.
+     * Returns a non-empty string representation of this {@code Optional}
+     * suitable for debugging.  The exact presentation format is unspecified and
+     * may vary between implementations and versions.
      *
-     * @implSpec If a value is present the result must include its string
-     * representation in the result. Empty and present Optionals must be
-     * unambiguously differentiable.
+     * @implSpec
+     * If a value is present the result must include its string representation
+     * in the result.  Empty and present {@code Optional}s must be unambiguously
+     * differentiable.
      *
      * @return the string representation of this instance
      */
--- a/jdk/src/java.base/share/classes/java/util/OptionalDouble.java	Mon Oct 19 19:14:29 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/util/OptionalDouble.java	Mon Oct 19 11:28:50 2015 -0700
@@ -30,15 +30,15 @@
 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()} will return {@code true} and
- * {@code getAsDouble()} will return 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} and
+ * {@code getAsDouble()} returns the value.
  *
  * <p>Additional methods that depend on the presence or absence of a contained
  * value are provided, such as {@link #orElse(double) orElse()}
- * (return a default value if value not present) and
- * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (perform an
- * action if the value is present).
+ * (returns a default value if no value is present) and
+ * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (performs
+ * an action if a value is present).
  *
  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  * class; use of identity-sensitive operations (including reference equality
@@ -71,12 +71,13 @@
     }
 
     /**
-     * Returns an empty {@code OptionalDouble} instance.  No value is present for this
-     * OptionalDouble.
+     * Returns an empty {@code OptionalDouble} instance.  No value is present
+     * for this {@code OptionalDouble}.
      *
-     * @apiNote Though it may be tempting to do so, avoid testing if an object
-     * is empty by comparing with {@code ==} against instances returned by
-     * {@code Option.empty()}. There is no guarantee that it is a singleton.
+     * @apiNote
+     * Though it may be tempting to do so, avoid testing if an object is empty
+     * by comparing with {@code ==} against instances returned by
+     * {@code OptionalDouble.empty()}.  There is no guarantee that it is a singleton.
      * Instead, use {@link #isPresent()}.
      *
      *  @return an empty {@code OptionalDouble}.
@@ -86,9 +87,9 @@
     }
 
     /**
-     * Construct an instance with the value present.
+     * Construct an instance with the described value.
      *
-     * @param value the double value to be present.
+     * @param value the double value to describe.
      */
     private OptionalDouble(double value) {
         this.isPresent = true;
@@ -96,9 +97,9 @@
     }
 
     /**
-     * Return an {@code OptionalDouble} with the specified value present.
+     * Returns an {@code OptionalDouble} describing the given value.
      *
-     * @param value the value to be present
+     * @param value the value to describe
      * @return an {@code OptionalDouble} with the value present
      */
     public static OptionalDouble of(double value) {
@@ -106,12 +107,11 @@
     }
 
     /**
-     * If a value is present in this {@code OptionalDouble}, returns the value,
-     * otherwise throws {@code NoSuchElementException}.
+     * If a value is present, returns the value, otherwise throws
+     * {@code NoSuchElementException}.
      *
-     * @return the value held by this {@code OptionalDouble}
-     * @throws NoSuchElementException if there is no value present
-     *
+     * @return the value described by this {@code OptionalDouble}
+     * @throws NoSuchElementException if no value is present
      * @see OptionalDouble#isPresent()
      */
     public double getAsDouble() {
@@ -122,21 +122,21 @@
     }
 
     /**
-     * Return {@code true} if there is a value present, otherwise {@code false}.
+     * If a value is present, returns {@code true}, otherwise {@code false}.
      *
-     * @return {@code true} if there is a value present, otherwise {@code false}
+     * @return {@code true} if a value is present, otherwise {@code false}
      */
     public boolean isPresent() {
         return isPresent;
     }
 
     /**
-     * If a value is present, perform the given action with the value,
-     * otherwise do nothing.
+     * If a value is present, performs the given action with the value,
+     * otherwise does nothing.
      *
-     * @param action the action to be performed if a value is present
-     * @throws NullPointerException if a value is present and {@code action} is
-     * null
+     * @param action the action to be performed, if a value is present
+     * @throws NullPointerException if value is present and the given action is
+     *         {@code null}
      */
     public void ifPresent(DoubleConsumer action) {
         if (isPresent) {
@@ -145,15 +145,16 @@
     }
 
     /**
-     * If a value is present, perform the given action with the value,
-     * otherwise perform the given empty-based action.
+     * If a value is present, performs the given action with the value,
+     * otherwise performs the given empty-based action.
      *
-     * @param action the action to be performed if a value is present
-     * @param emptyAction the empty-based action to be performed if a value is
-     * not present
-     * @throws NullPointerException if a value is present and {@code action} is
-     * null, or a value is not present and {@code emptyAction} is null.
-     * @since 1.9
+     * @param action the action to be performed, if a value is present
+     * @param emptyAction the empty-based action to be performed, if no value is
+     * present
+     * @throws NullPointerException if a value is present and the given action
+     *         is {@code null}, or no value is present and the given empty-based
+     *         action is {@code null}.
+     * @since 9
      */
     public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
         if (isPresent) {
@@ -164,19 +165,20 @@
     }
 
     /**
-     * If a value is present return a sequential {@link DoubleStream} containing
-     * only that value, otherwise return an empty {@code DoubleStream}.
+     * If a value is present, returns a sequential {@link DoubleStream}
+     * containing only that value, otherwise returns an empty
+     * {@code DoubleStream}.
      *
-     * @apiNote This method can be used to transform a {@code Stream} of
-     * optional doubles to a {@code DoubleStream} of present doubles:
-     *
+     * @apiNote
+     * This method can be used to transform a {@code Stream} of optional doubles
+     * to a {@code DoubleStream} of present doubles:
      * <pre>{@code
      *     Stream<OptionalDouble> os = ..
      *     DoubleStream s = os.flatMapToDouble(OptionalDouble::stream)
      * }</pre>
      *
      * @return the optional value as a {@code DoubleStream}
-     * @since 1.9
+     * @since 9
      */
     public DoubleStream stream() {
         if (isPresent) {
@@ -187,9 +189,10 @@
     }
 
     /**
-     * Return the value if present, otherwise return {@code other}.
+     * If a value is present, returns the value, otherwise returns
+     * {@code other}.
      *
-     * @param other the value to be returned if there is no value present
+     * @param other the value to be returned, if no value is present
      * @return the value, if present, otherwise {@code other}
      */
     public double orElse(double other) {
@@ -197,34 +200,35 @@
     }
 
     /**
-     * Return the value if present, otherwise invoke {@code other} and return
-     * the result of that invocation.
+     * If a value is present, returns the value, otherwise returns the result
+     * produced by the supplying function.
      *
-     * @param other a {@code DoubleSupplier} whose result is returned if no value
-     * is present
-     * @return the value if present otherwise the result of {@code other.getAsDouble()}
-     * @throws NullPointerException if value is not present and {@code other} is
-     * null
+     * @param supplier the supplying function that produces a value to be returned
+     * @return the value, if present, otherwise the result produced by the
+     *         supplying function
+     * @throws NullPointerException if no value is present and the supplying
+     *         function is {@code null}
      */
-    public double orElseGet(DoubleSupplier other) {
-        return isPresent ? value : other.getAsDouble();
+    public double orElseGet(DoubleSupplier supplier) {
+        return isPresent ? value : supplier.getAsDouble();
     }
 
     /**
-     * Return the contained value, if present, otherwise throw an exception
-     * to be created by the provided supplier.
+     * 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 list can be used as the supplier. For example,
+     * @apiNote
+     * A method reference to the exception constructor with an empty argument
+     * list can be used as the supplier. For example,
      * {@code IllegalStateException::new}
      *
      * @param <X> Type of the exception to be thrown
-     * @param exceptionSupplier The supplier which will return the exception to
-     * be thrown
-     * @return the present value
-     * @throws X if there is no value present
-     * @throws NullPointerException if no value is present and
-     * {@code exceptionSupplier} is null
+     * @param exceptionSupplier the supplying function that produces an
+     *        exception to be thrown
+     * @return the value, if present
+     * @throws X if no value is present
+     * @throws NullPointerException if no value is present and the exception
+     *         supplying function is {@code null}
      */
     public<X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
         if (isPresent) {
@@ -235,17 +239,18 @@
     }
 
     /**
-     * Indicates whether some other object is "equal to" this OptionalDouble. The
-     * other object is considered equal if:
+     * Indicates whether some other object is "equal to" this
+     * {@code OptionalDouble}. The other object is considered equal if:
      * <ul>
      * <li>it is also an {@code OptionalDouble} and;
      * <li>both instances have no value present or;
-     * <li>the present values are "equal to" each other via {@code Double.compare() == 0}.
+     * <li>the present values are "equal to" each other via
+     * {@code Double.compare() == 0}.
      * </ul>
      *
      * @param obj an object to be tested for equality
      * @return {@code true} if the other object is "equal to" this object
-     * otherwise {@code false}
+     *         otherwise {@code false}
      */
     @Override
     public boolean equals(Object obj) {
@@ -264,10 +269,11 @@
     }
 
     /**
-     * Returns the hash code value of the present value, if any, or 0 (zero) if
-     * no value is present.
+     * Returns the hash code of the value, if present, otherwise {@code 0}
+     * (zero) if no value is present.
      *
-     * @return hash code value of the present value or 0 if no value is present
+     * @return hash code value of the present value or {@code 0} if no value is
+     *         present
      */
     @Override
     public int hashCode() {
@@ -275,14 +281,13 @@
     }
 
     /**
-     * {@inheritDoc}
+     * Returns a non-empty string representation of this {@code OptionalDouble}
+     * suitable for debugging.  The exact presentation format is unspecified and
+     * may vary between implementations and versions.
      *
-     * Returns a non-empty string representation of this object suitable for
-     * debugging. The exact presentation format is unspecified and may vary
-     * between implementations and versions.
-     *
-     * @implSpec If a value is present the result must include its string
-     * representation in the result. Empty and present instances must be
+     * @implSpec
+     * If a value is present the result must include its string representation
+     * in the result.  Empty and present {@code OptionalDouble}s must be
      * unambiguously differentiable.
      *
      * @return the string representation of this instance
--- a/jdk/src/java.base/share/classes/java/util/OptionalInt.java	Mon Oct 19 19:14:29 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/util/OptionalInt.java	Mon Oct 19 11:28:50 2015 -0700
@@ -30,15 +30,15 @@
 import java.util.stream.IntStream;
 
 /**
- * A container object which may or may not contain a {@code int} value.
- * If a value is present, {@code isPresent()} will return {@code true} and
- * {@code getAsInt()} will return 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} and
+ * {@code getAsInt()} returns the value.
  *
  * <p>Additional methods that depend on the presence or absence of a contained
  * value are provided, such as {@link #orElse(int) orElse()}
- * (return a default value if value not present) and
- * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (perform an
- * action if the value is present).
+ * (returns a default value if no value is present) and
+ * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()} (performs an
+ * action if a value is present).
  *
  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  * class; use of identity-sensitive operations (including reference equality
@@ -71,24 +71,25 @@
     }
 
     /**
-     * Returns an empty {@code OptionalInt} instance.  No value is present for this
-     * OptionalInt.
+     * Returns an empty {@code OptionalInt} instance.  No value is present for
+     * this {@code OptionalInt}.
      *
-     * @apiNote Though it may be tempting to do so, avoid testing if an object
-     * is empty by comparing with {@code ==} against instances returned by
-     * {@code Option.empty()}. There is no guarantee that it is a singleton.
+     * @apiNote
+     * Though it may be tempting to do so, avoid testing if an object is empty
+     * by comparing with {@code ==} against instances returned by
+     * {@code OptionalInt.empty()}.  There is no guarantee that it is a singleton.
      * Instead, use {@link #isPresent()}.
      *
-     *  @return an empty {@code OptionalInt}
+     * @return an empty {@code OptionalInt}
      */
     public static OptionalInt empty() {
         return EMPTY;
     }
 
     /**
-     * Construct an instance with the value present.
+     * Construct an instance with the described value.
      *
-     * @param value the int value to be present
+     * @param value the int value to describe
      */
     private OptionalInt(int value) {
         this.isPresent = true;
@@ -96,9 +97,9 @@
     }
 
     /**
-     * Return an {@code OptionalInt} with the specified value present.
+     * Returns an {@code OptionalInt} describing the given value.
      *
-     * @param value the value to be present
+     * @param value the value to describe
      * @return an {@code OptionalInt} with the value present
      */
     public static OptionalInt of(int value) {
@@ -106,12 +107,11 @@
     }
 
     /**
-     * If a value is present in this {@code OptionalInt}, returns the value,
-     * otherwise throws {@code NoSuchElementException}.
+     * If a value is present, returns the value, otherwise throws
+     * {@code NoSuchElementException}.
      *
-     * @return the value held by this {@code OptionalInt}
-     * @throws NoSuchElementException if there is no value present
-     *
+     * @return the value described by this {@code OptionalInt}
+     * @throws NoSuchElementException if no value is present
      * @see OptionalInt#isPresent()
      */
     public int getAsInt() {
@@ -122,21 +122,21 @@
     }
 
     /**
-     * Return {@code true} if there is a value present, otherwise {@code false}.
+     * If a value is present, returns {@code true}, otherwise {@code false}.
      *
-     * @return {@code true} if there is a value present, otherwise {@code false}
+     * @return {@code true} if a value is present, otherwise {@code false}
      */
     public boolean isPresent() {
         return isPresent;
     }
 
     /**
-     * If a value is present, perform the given action with the value,
-     * otherwise do nothing.
+     * If a value is present, performs the given action with the value,
+     * otherwise does nothing.
      *
-     * @param action the action to be performed if a value is present
-     * @throws NullPointerException if value is present and {@code action} is
-     * null
+     * @param action the action to be performed, if a value is present
+     * @throws NullPointerException if value is present and the given action is
+     *         {@code null}
      */
     public void ifPresent(IntConsumer action) {
         if (isPresent) {
@@ -145,15 +145,16 @@
     }
 
     /**
-     * If a value is present, perform the given action with the value,
-     * otherwise perform the given empty-based action.
+     * If a value is present, performs the given action with the value,
+     * otherwise performs the given empty-based action.
      *
-     * @param action the action to be performed if a value is present
-     * @param emptyAction the empty-based action to be performed if a value is
-     * not present
-     * @throws NullPointerException if a value is present and {@code action} is
-     * null, or a value is not present and {@code emptyAction} is null.
-     * @since 1.9
+     * @param action the action to be performed, if a value is present
+     * @param emptyAction the empty-based action to be performed, if no value is
+     *        present
+     * @throws NullPointerException if a value is present and the given action
+     *         is {@code null}, or no value is present and the given empty-based
+     *         action is {@code null}.
+     * @since 9
      */
     public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) {
         if (isPresent) {
@@ -164,19 +165,19 @@
     }
 
     /**
-     * If a value is present return a sequential {@link IntStream} containing
-     * only that value, otherwise return an empty {@code IntStream}.
+     * If a value is present, returns a sequential {@link IntStream} containing
+     * only that value, otherwise returns an empty {@code IntStream}.
      *
-     * @apiNote This method can be used to transform a {@code Stream} of
-     * optional integers to an {@code IntStream} of present integers:
-     *
+     * @apiNote
+     * This method can be used to transform a {@code Stream} of optional
+     * integers to an {@code IntStream} of present integers:
      * <pre>{@code
      *     Stream<OptionalInt> os = ..
      *     IntStream s = os.flatMapToInt(OptionalInt::stream)
      * }</pre>
      *
      * @return the optional value as an {@code IntStream}
-     * @since 1.9
+     * @since 9
      */
     public IntStream stream() {
         if (isPresent) {
@@ -187,9 +188,10 @@
     }
 
     /**
-     * Return the value if present, otherwise return {@code other}.
+     * If a value is present, returns the value, otherwise returns
+     * {@code other}.
      *
-     * @param other the value to be returned if there is no value present
+     * @param other the value to be returned, if no value is present
      * @return the value, if present, otherwise {@code other}
      */
     public int orElse(int other) {
@@ -197,34 +199,35 @@
     }
 
     /**
-     * Return the value if present, otherwise invoke {@code other} and return
-     * the result of that invocation.
+     * If a value is present, returns the value, otherwise returns the result
+     * produced by the supplying function.
      *
-     * @param other a {@code IntSupplier} whose result is returned if no value
-     * is present
-     * @return the value if present otherwise the result of {@code other.getAsInt()}
-     * @throws NullPointerException if value is not present and {@code other} is
-     * null
+     * @param supplier the supplying function that produces a value to be returned
+     * @return the value, if present, otherwise the result produced by the
+     *         supplying function
+     * @throws NullPointerException if no value is present and the supplying
+     *         function is {@code null}
      */
-    public int orElseGet(IntSupplier other) {
-        return isPresent ? value : other.getAsInt();
+    public int orElseGet(IntSupplier supplier) {
+        return isPresent ? value : supplier.getAsInt();
     }
 
     /**
-     * Return the contained value, if present, otherwise throw an exception
-     * to be created by the provided supplier.
+     * 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 list can be used as the supplier. For example,
+     * @apiNote
+     * A method reference to the exception constructor with an empty argument
+     * list can be used as the supplier. For example,
      * {@code IllegalStateException::new}
      *
      * @param <X> Type of the exception to be thrown
-     * @param exceptionSupplier The supplier which will return the exception to
-     * be thrown
-     * @return the present value
-     * @throws X if there is no value present
-     * @throws NullPointerException if no value is present and
-     * {@code exceptionSupplier} is null
+     * @param exceptionSupplier the supplying function that produces an
+     *        exception to be thrown
+     * @return the value, if present
+     * @throws X if no value is present
+     * @throws NullPointerException if no value is present and the exception
+     *         supplying function is {@code null}
      */
     public<X extends Throwable> int orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
         if (isPresent) {
@@ -235,8 +238,8 @@
     }
 
     /**
-     * Indicates whether some other object is "equal to" this OptionalInt. The
-     * other object is considered equal if:
+     * Indicates whether some other object is "equal to" this
+     * {@code OptionalInt}.  The other object is considered equal if:
      * <ul>
      * <li>it is also an {@code OptionalInt} and;
      * <li>both instances have no value present or;
@@ -245,7 +248,7 @@
      *
      * @param obj an object to be tested for equality
      * @return {@code true} if the other object is "equal to" this object
-     * otherwise {@code false}
+     *         otherwise {@code false}
      */
     @Override
     public boolean equals(Object obj) {
@@ -264,10 +267,11 @@
     }
 
     /**
-     * Returns the hash code value of the present value, if any, or 0 (zero) if
-     * no value is present.
+     * Returns the hash code of the value, if present, otherwise {@code 0}
+     * (zero) if no value is present.
      *
-     * @return hash code value of the present value or 0 if no value is present
+     * @return hash code value of the present value or {@code 0} if no value is
+     *         present
      */
     @Override
     public int hashCode() {
@@ -275,14 +279,13 @@
     }
 
     /**
-     * {@inheritDoc}
+     * Returns a non-empty string representation of this {@code OptionalInt}
+     * suitable for debugging.  The exact presentation format is unspecified and
+     * may vary between implementations and versions.
      *
-     * Returns a non-empty string representation of this object suitable for
-     * debugging. The exact presentation format is unspecified and may vary
-     * between implementations and versions.
-     *
-     * @implSpec If a value is present the result must include its string
-     * representation in the result. Empty and present instances must be
+     * @implSpec
+     * If a value is present the result must include its string representation
+     * in the result.  Empty and present {@code OptionalInt}s must be
      * unambiguously differentiable.
      *
      * @return the string representation of this instance
--- a/jdk/src/java.base/share/classes/java/util/OptionalLong.java	Mon Oct 19 19:14:29 2015 +0200
+++ b/jdk/src/java.base/share/classes/java/util/OptionalLong.java	Mon Oct 19 11:28:50 2015 -0700
@@ -30,15 +30,15 @@
 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()} will return {@code true} and
- * {@code getAsLong()} will return 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} and
+ * {@code getAsLong()} returns the value.
  *
  * <p>Additional methods that depend on the presence or absence of a contained
  * value are provided, such as {@link #orElse(long) orElse()}
- * (return a default value if value not present) and
- * {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (perform an
- * action if the value is present).
+ * (returns a default value if no value is present) and
+ * {@link #ifPresent(java.util.function.LongConsumer) ifPresent()} (performs an
+ * action if a value is present).
  *
  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  * class; use of identity-sensitive operations (including reference equality
@@ -71,24 +71,25 @@
     }
 
     /**
-     * Returns an empty {@code OptionalLong} instance.  No value is present for this
-     * OptionalLong.
+     * Returns an empty {@code OptionalLong} instance.  No value is present for
+     * this {@code OptionalLong}.
      *
-     * @apiNote Though it may be tempting to do so, avoid testing if an object
-     * is empty by comparing with {@code ==} against instances returned by
-     * {@code Option.empty()}. There is no guarantee that it is a singleton.
+     * @apiNote
+     * Though it may be tempting to do so, avoid testing if an object is empty
+     * by comparing with {@code ==} against instances returned by
+     * {@code OptionalLong.empty()}.  There is no guarantee that it is a singleton.
      * Instead, use {@link #isPresent()}.
      *
-     *  @return an empty {@code OptionalLong}.
+     * @return an empty {@code OptionalLong}.
      */
     public static OptionalLong empty() {
         return EMPTY;
     }
 
     /**
-     * Construct an instance with the value present.
+     * Construct an instance with the described value.
      *
-     * @param value the long value to be present
+     * @param value the long value to describe
      */
     private OptionalLong(long value) {
         this.isPresent = true;
@@ -96,9 +97,9 @@
     }
 
     /**
-     * Return an {@code OptionalLong} with the specified value present.
+     * Returns an {@code OptionalLong} describing the given value.
      *
-     * @param value the value to be present
+     * @param value the value to describe
      * @return an {@code OptionalLong} with the value present
      */
     public static OptionalLong of(long value) {
@@ -106,12 +107,11 @@
     }
 
     /**
-     * If a value is present in this {@code OptionalLong}, returns the value,
-     * otherwise throws {@code NoSuchElementException}.
+     * If a value is present, returns the value, otherwise throws
+     * {@code NoSuchElementException}.
      *
-     * @return the value held by this {@code OptionalLong}
-     * @throws NoSuchElementException if there is no value present
-     *
+     * @return the value described by this {@code OptionalLong}
+     * @throws NoSuchElementException if no value is present
      * @see OptionalLong#isPresent()
      */
     public long getAsLong() {
@@ -122,21 +122,21 @@
     }
 
     /**
-     * Return {@code true} if there is a value present, otherwise {@code false}.
+     * If a value is present, returns {@code true}, otherwise {@code false}.
      *
-     * @return {@code true} if there is a value present, otherwise {@code false}
+     * @return {@code true} if a value is present, otherwise {@code false}
      */
     public boolean isPresent() {
         return isPresent;
     }
 
     /**
-     * If a value is present, perform the given action with the value,
-     * otherwise do nothing.
+     * If a value is present, performs the given action with the value,
+     * otherwise does nothing.
      *
-     * @param action the action to be performed if a value is present
-     * @throws NullPointerException if a value is present and {@code action} is
-     * null
+     * @param action the action to be performed, if a value is present
+     * @throws NullPointerException if value is present and the given action is
+     *         {@code null}
      */
     public void ifPresent(LongConsumer action) {
         if (isPresent) {
@@ -145,15 +145,16 @@
     }
 
     /**
-     * If a value is present, perform the given action with the value,
-     * otherwise perform the given empty-based action.
+     * If a value is present, performs the given action with the value,
+     * otherwise performs the given empty-based action.
      *
-     * @param action the action to be performed if a value is present
-     * @param emptyAction the empty-based action to be performed if a value is
-     * not present
-     * @throws NullPointerException if a value is present and {@code action} is
-     * null, or a value is not present and {@code emptyAction} is null.
-     * @since 1.9
+     * @param action the action to be performed, if a value is present
+     * @param emptyAction the empty-based action to be performed, if no value is
+     *        present
+     * @throws NullPointerException if a value is present and the given action
+     *         is {@code null}, or no value is present and the given empty-based
+     *         action is {@code null}.
+     * @since 9
      */
     public void ifPresentOrElse(LongConsumer action, Runnable emptyAction) {
         if (isPresent) {
@@ -164,19 +165,19 @@
     }
 
     /**
-     * If a value is present return a sequential {@link LongStream} containing
-     * only that value, otherwise return an empty {@code LongStream}.
+     * If a value is present, returns a sequential {@link LongStream} containing
+     * only that value, otherwise returns an empty {@code LongStream}.
      *
-     * @apiNote This method can be used to transform a {@code Stream} of
-     * optional longs to a {@code LongStream} of present longs:
-     *
+     * @apiNote
+     * This method can be used to transform a {@code Stream} of optional longs
+     * to an {@code LongStream} of present longs:
      * <pre>{@code
      *     Stream<OptionalLong> os = ..
      *     LongStream s = os.flatMapToLong(OptionalLong::stream)
      * }</pre>
      *
-     * @return the optional value as a {@code LongStream}
-     * @since 1.9
+     * @return the optional value as an {@code LongStream}
+     * @since 9
      */
     public LongStream stream() {
         if (isPresent) {
@@ -187,9 +188,10 @@
     }
 
     /**
-     * Return the value if present, otherwise return {@code other}.
+     * If a value is present, returns the value, otherwise returns
+     * {@code other}.
      *
-     * @param other the value to be returned if there is no value present
+     * @param other the value to be returned, if no value is present
      * @return the value, if present, otherwise {@code other}
      */
     public long orElse(long other) {
@@ -197,34 +199,35 @@
     }
 
     /**
-     * Return the value if present, otherwise invoke {@code other} and return
-     * the result of that invocation.
+     * If a value is present, returns the value, otherwise returns the result
+     * produced by the supplying function.
      *
-     * @param other a {@code LongSupplier} whose result is returned if no value
-     * is present
-     * @return the value if present otherwise the result of {@code other.getAsLong()}
-     * @throws NullPointerException if value is not present and {@code other} is
-     * null
+     * @param supplier the supplying function that produces a value to be returned
+     * @return the value, if present, otherwise the result produced by the
+     *         supplying function
+     * @throws NullPointerException if no value is present and the supplying
+     *         function is {@code null}
      */
-    public long orElseGet(LongSupplier other) {
-        return isPresent ? value : other.getAsLong();
+    public long orElseGet(LongSupplier supplier) {
+        return isPresent ? value : supplier.getAsLong();
     }
 
     /**
-     * Return the contained value, if present, otherwise throw an exception
-     * to be created by the provided supplier.
+     * 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 list can be used as the supplier. For example,
+     * @apiNote
+     * A method reference to the exception constructor with an empty argument
+     * list can be used as the supplier. For example,
      * {@code IllegalStateException::new}
      *
      * @param <X> Type of the exception to be thrown
-     * @param exceptionSupplier The supplier which will return the exception to
-     * be thrown
-     * @return the present value
-     * @throws X if there is no value present
-     * @throws NullPointerException if no value is present and
-     * {@code exceptionSupplier} is null
+     * @param exceptionSupplier the supplying function that produces an
+     *        exception to be thrown
+     * @return the value, if present
+     * @throws X if no value is present
+     * @throws NullPointerException if no value is present and the exception
+     *         supplying function is {@code null}
      */
     public<X extends Throwable> long orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
         if (isPresent) {
@@ -235,8 +238,8 @@
     }
 
     /**
-     * Indicates whether some other object is "equal to" this OptionalLong. The
-     * other object is considered equal if:
+     * Indicates whether some other object is "equal to" this
+     * {@code OptionalLong}.  The other object is considered equal if:
      * <ul>
      * <li>it is also an {@code OptionalLong} and;
      * <li>both instances have no value present or;
@@ -245,7 +248,7 @@
      *
      * @param obj an object to be tested for equality
      * @return {@code true} if the other object is "equal to" this object
-     * otherwise {@code false}
+     *         otherwise {@code false}
      */
     @Override
     public boolean equals(Object obj) {
@@ -264,10 +267,11 @@
     }
 
     /**
-     * Returns the hash code value of the present value, if any, or 0 (zero) if
-     * no value is present.
+     * Returns the hash code of the value, if present, otherwise {@code 0}
+     * (zero) if no value is present.
      *
-     * @return hash code value of the present value or 0 if no value is present
+     * @return hash code value of the present value or {@code 0} if no value is
+     *         present
      */
     @Override
     public int hashCode() {
@@ -275,14 +279,13 @@
     }
 
     /**
-     * {@inheritDoc}
+     * Returns a non-empty string representation of this {@code OptionalLong}
+     * suitable for debugging.  The exact presentation format is unspecified and
+     * may vary between implementations and versions.
      *
-     * Returns a non-empty string representation of this object suitable for
-     * debugging. The exact presentation format is unspecified and may vary
-     * between implementations and versions.
-     *
-     * @implSpec If a value is present the result must include its string
-     * representation in the result. Empty and present instances must be
+     * @implSpec
+     * If a value is present the result must include its string representation
+     * in the result.  Empty and present {@code OptionalLong}s must be
      * unambiguously differentiable.
      *
      * @return the string representation of this instance
--- a/jdk/test/java/util/Optional/Basic.java	Mon Oct 19 19:14:29 2015 +0200
+++ b/jdk/test/java/util/Optional/Basic.java	Mon Oct 19 11:28:50 2015 -0700
@@ -294,6 +294,46 @@
     }
 
     @Test(groups = "unit")
+    public void testOr() {
+        Optional<String> empty = Optional.empty();
+        Optional<String> duke = Optional.of("Duke");
+
+        // Null supplier
+        try {
+            Optional<String> b = empty.or(null);
+            fail("Should throw NPE on null supplier");
+        } catch (NullPointerException npe) {
+            // expected
+        }
+
+        // Supply null
+        try {
+            Optional<String> b = empty.or(() -> null);
+            fail("Should throw NPE when supplier returns null");
+        } catch (NullPointerException npe) {
+            // expected
+        }
+
+        // Non-empty won't invoke supplier
+        try {
+            Optional<String> b = duke.or(() -> null);
+            assertTrue(b.isPresent());
+        } catch (NullPointerException npe) {
+            fail("Supplier should not be invoked");
+        }
+
+        // Supply for empty
+        Optional<String> suppliedDuke = empty.or(() -> duke);
+        assertTrue(suppliedDuke.isPresent());
+        assertSame(suppliedDuke, duke);
+
+        // Supply for non-empty
+        Optional<String> actualDuke = duke.or(() -> Optional.of("Other Duke"));
+        assertTrue(actualDuke.isPresent());
+        assertSame(actualDuke, duke);
+    }
+
+    @Test(groups = "unit")
     public void testStream() {
         {
             Stream<String> s = Optional.<String>empty().stream();