< prev index next >

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

Print this page
rev 14279 : [mq]: 8140281-deprecation-optional.get


  93      */
  94     private OptionalDouble(double value) {
  95         this.isPresent = true;
  96         this.value = value;
  97     }
  98 
  99     /**
 100      * Returns an {@code OptionalDouble} describing the given value.
 101      *
 102      * @param value the value to describe
 103      * @return an {@code OptionalDouble} with the value present
 104      */
 105     public static OptionalDouble of(double value) {
 106         return new OptionalDouble(value);
 107     }
 108 
 109     /**
 110      * If a value is present, returns the value, otherwise throws
 111      * {@code NoSuchElementException}.
 112      *













 113      * @return the value described by this {@code OptionalDouble}
 114      * @throws NoSuchElementException if no value is present
 115      * @see OptionalDouble#isPresent()
 116      */

 117     public double getAsDouble() {
 118         if (!isPresent) {
 119             throw new NoSuchElementException("No value present");
 120         }
 121         return value;
 122     }
 123 
 124     /**


















 125      * If a value is present, returns {@code true}, otherwise {@code false}.
 126      *
 127      * @return {@code true} if a value is present, otherwise {@code false}
 128      */
 129     public boolean isPresent() {
 130         return isPresent;
 131     }
 132 
 133     /**
 134      * If a value is present, performs the given action with the value,
 135      * otherwise does nothing.
 136      *
 137      * @param action the action to be performed, if a value is present
 138      * @throws NullPointerException if value is present and the given action is
 139      *         {@code null}
 140      */
 141     public void ifPresent(DoubleConsumer action) {
 142         if (isPresent) {
 143             action.accept(value);
 144         }




  93      */
  94     private OptionalDouble(double value) {
  95         this.isPresent = true;
  96         this.value = value;
  97     }
  98 
  99     /**
 100      * Returns an {@code OptionalDouble} describing the given value.
 101      *
 102      * @param value the value to describe
 103      * @return an {@code OptionalDouble} with the value present
 104      */
 105     public static OptionalDouble of(double value) {
 106         return new OptionalDouble(value);
 107     }
 108 
 109     /**
 110      * If a value is present, returns the value, otherwise throws
 111      * {@code NoSuchElementException}.
 112      *
 113      * @deprecated
 114      * This method's name {@code getAsDouble} makes it the obvious method to
 115      * call to retrieve the value from this {@code OptionalDouble}. However, it has
 116      * no mechanism to avoid an exception if this {@code OptionalDouble} is empty.
 117      * This tends to lead to code that mishandles empty {@code OptionalDouble}
 118      * values. Consider using other methods that handle the case where
 119      * the {@code OptionalDouble} might be empty, such as
 120      * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()}
 121      * and related methods, and
 122      * {@link #orElse(double) orElse()} and related methods.
 123      * Use {@link getWhenPresent()} when it is known that a value is
 124      * always present.
 125      *
 126      * @return the value described by this {@code OptionalDouble}
 127      * @throws NoSuchElementException if no value is present
 128      * @see OptionalDouble#isPresent()
 129      */
 130     @Deprecated(since="9")
 131     public double getAsDouble() {
 132         if (!isPresent) {
 133             throw new NoSuchElementException("No value present");
 134         }
 135         return value;
 136     }
 137 
 138     /**
 139      * If a value is present, returns the value, otherwise throws
 140      * {@code NoSuchElementException}.
 141      *
 142      * @apiNote
 143      * Use this method only when it is known that a value is always present.
 144      *
 145      * @return the value described by this {@code OptionalDouble}
 146      * @throws NoSuchElementException if no value is present
 147      * @see OptionalDouble#isPresent()
 148      */
 149     public double getWhenPresent() {
 150         if (!isPresent) {
 151             throw new NoSuchElementException("No value present");
 152         }
 153         return value;
 154     }
 155 
 156     /**
 157      * If a value is present, returns {@code true}, otherwise {@code false}.
 158      *
 159      * @return {@code true} if a value is present, otherwise {@code false}
 160      */
 161     public boolean isPresent() {
 162         return isPresent;
 163     }
 164 
 165     /**
 166      * If a value is present, performs the given action with the value,
 167      * otherwise does nothing.
 168      *
 169      * @param action the action to be performed, if a value is present
 170      * @throws NullPointerException if value is present and the given action is
 171      *         {@code null}
 172      */
 173     public void ifPresent(DoubleConsumer action) {
 174         if (isPresent) {
 175             action.accept(value);
 176         }


< prev index next >