src/java.base/share/classes/java/util/OptionalDouble.java
changeset 48328 7acf5700d542
parent 47216 71c04702a3d5
child 49823 af4b57a556be
equal deleted inserted replaced
48327:d2a837cf9ff1 48328:7acf5700d542
    28 import java.util.function.DoubleSupplier;
    28 import java.util.function.DoubleSupplier;
    29 import java.util.function.Supplier;
    29 import java.util.function.Supplier;
    30 import java.util.stream.DoubleStream;
    30 import java.util.stream.DoubleStream;
    31 
    31 
    32 /**
    32 /**
    33  * A container object which may or may not contain a {@code double} value.  If a
    33  * A container object which may or may not contain a {@code double} value.
    34  * value is present, {@code isPresent()} returns {@code true} and
    34  * If a value is present, {@code isPresent()} returns {@code true}. If no
    35  * {@code getAsDouble()} returns the value.
    35  * value is present, the object is considered <i>empty</i> and
       
    36  * {@code isPresent()} returns {@code false}.
    36  *
    37  *
    37  * <p>Additional methods that depend on the presence or absence of a contained
    38  * <p>Additional methods that depend on the presence or absence of a contained
    38  * value are provided, such as {@link #orElse(double) orElse()}
    39  * value are provided, such as {@link #orElse(double) orElse()}
    39  * (returns a default value if no value is present) and
    40  * (returns a default value if no value is present) and
    40  * {@link #ifPresent(DoubleConsumer) ifPresent()} (performs
    41  * {@link #ifPresent(DoubleConsumer) ifPresent()} (performs
   115     /**
   116     /**
   116      * If a value is present, returns the value, otherwise throws
   117      * If a value is present, returns the value, otherwise throws
   117      * {@code NoSuchElementException}.
   118      * {@code NoSuchElementException}.
   118      *
   119      *
   119      * @apiNote
   120      * @apiNote
   120      * The methods {@link #orElse(double) orElse} and
   121      * The preferred alternative to this method is {@link #orElseThrow()}.
   121      * {@link #orElseGet(DoubleSupplier) orElseGet}
       
   122      * are generally preferable to this method, as they return a substitute
       
   123      * value if the value is absent, instead of throwing an exception.
       
   124      *
   122      *
   125      * @return the value described by this {@code OptionalDouble}
   123      * @return the value described by this {@code OptionalDouble}
   126      * @throws NoSuchElementException if no value is present
   124      * @throws NoSuchElementException if no value is present
   127      * @see OptionalDouble#isPresent()
       
   128      */
   125      */
   129     public double getAsDouble() {
   126     public double getAsDouble() {
   130         if (!isPresent) {
   127         if (!isPresent) {
   131             throw new NoSuchElementException("No value present");
   128             throw new NoSuchElementException("No value present");
   132         }
   129         }
   221      * @throws NullPointerException if no value is present and the supplying
   218      * @throws NullPointerException if no value is present and the supplying
   222      *         function is {@code null}
   219      *         function is {@code null}
   223      */
   220      */
   224     public double orElseGet(DoubleSupplier supplier) {
   221     public double orElseGet(DoubleSupplier supplier) {
   225         return isPresent ? value : supplier.getAsDouble();
   222         return isPresent ? value : supplier.getAsDouble();
       
   223     }
       
   224 
       
   225     /**
       
   226      * If a value is present, returns the value, otherwise throws
       
   227      * {@code NoSuchElementException}.
       
   228      *
       
   229      * @return the value described by this {@code OptionalDouble}
       
   230      * @throws NoSuchElementException if no value is present
       
   231      * @since 10
       
   232      */
       
   233     public double orElseThrow() {
       
   234         if (!isPresent) {
       
   235             throw new NoSuchElementException("No value present");
       
   236         }
       
   237         return value;
   226     }
   238     }
   227 
   239 
   228     /**
   240     /**
   229      * If a value is present, returns the value, otherwise throws an exception
   241      * If a value is present, returns the value, otherwise throws an exception
   230      * produced by the exception supplying function.
   242      * produced by the exception supplying function.