< prev index next >

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

Print this page
rev 48216 : 8140281: add no-arg Optional.orElseThrow() as preferred alternative to get()
Reviewed-by: XXX


 100     private OptionalDouble(double value) {
 101         this.isPresent = true;
 102         this.value = value;
 103     }
 104 
 105     /**
 106      * Returns an {@code OptionalDouble} describing the given value.
 107      *
 108      * @param value the value to describe
 109      * @return an {@code OptionalDouble} with the value present
 110      */
 111     public static OptionalDouble of(double value) {
 112         return new OptionalDouble(value);
 113     }
 114 
 115     /**
 116      * If a value is present, returns the value, otherwise throws
 117      * {@code NoSuchElementException}.
 118      *
 119      * @apiNote
 120      * The methods {@link #orElse(double) orElse} and
 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      *
 125      * @return the value described by this {@code OptionalDouble}
 126      * @throws NoSuchElementException if no value is present
 127      * @see OptionalDouble#isPresent()
 128      */
 129     public double getAsDouble() {
 130         if (!isPresent) {
 131             throw new NoSuchElementException("No value present");
 132         }
 133         return value;
 134     }
 135 
 136     /**
 137      * If a value is present, returns {@code true}, otherwise {@code false}.
 138      *
 139      * @return {@code true} if a value is present, otherwise {@code false}
 140      */
 141     public boolean isPresent() {
 142         return isPresent;
 143     }
 144 
 145     /**
 146      * If a value is present, performs the given action with the value,
 147      * otherwise does nothing.


 209      */
 210     public double orElse(double other) {
 211         return isPresent ? value : other;
 212     }
 213 
 214     /**
 215      * If a value is present, returns the value, otherwise returns the result
 216      * produced by the supplying function.
 217      *
 218      * @param supplier the supplying function that produces a value to be returned
 219      * @return the value, if present, otherwise the result produced by the
 220      *         supplying function
 221      * @throws NullPointerException if no value is present and the supplying
 222      *         function is {@code null}
 223      */
 224     public double orElseGet(DoubleSupplier supplier) {
 225         return isPresent ? value : supplier.getAsDouble();
 226     }
 227 
 228     /**















 229      * If a value is present, returns the value, otherwise throws an exception
 230      * produced by the exception supplying function.
 231      *
 232      * @apiNote
 233      * A method reference to the exception constructor with an empty argument
 234      * list can be used as the supplier. For example,
 235      * {@code IllegalStateException::new}
 236      *
 237      * @param <X> Type of the exception to be thrown
 238      * @param exceptionSupplier the supplying function that produces an
 239      *        exception to be thrown
 240      * @return the value, if present
 241      * @throws X if no value is present
 242      * @throws NullPointerException if no value is present and the exception
 243      *         supplying function is {@code null}
 244      */
 245     public<X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
 246         if (isPresent) {
 247             return value;
 248         } else {




 100     private OptionalDouble(double value) {
 101         this.isPresent = true;
 102         this.value = value;
 103     }
 104 
 105     /**
 106      * Returns an {@code OptionalDouble} describing the given value.
 107      *
 108      * @param value the value to describe
 109      * @return an {@code OptionalDouble} with the value present
 110      */
 111     public static OptionalDouble of(double value) {
 112         return new OptionalDouble(value);
 113     }
 114 
 115     /**
 116      * If a value is present, returns the value, otherwise throws
 117      * {@code NoSuchElementException}.
 118      *
 119      * @apiNote
 120      * The preferred alternative to this method is {@link #orElseThrow()}.



 121      *
 122      * @return the value described by this {@code OptionalDouble}
 123      * @throws NoSuchElementException if no value is present

 124      */
 125     public double getAsDouble() {
 126         if (!isPresent) {
 127             throw new NoSuchElementException("No value present");
 128         }
 129         return value;
 130     }
 131 
 132     /**
 133      * If a value is present, returns {@code true}, otherwise {@code false}.
 134      *
 135      * @return {@code true} if a value is present, otherwise {@code false}
 136      */
 137     public boolean isPresent() {
 138         return isPresent;
 139     }
 140 
 141     /**
 142      * If a value is present, performs the given action with the value,
 143      * otherwise does nothing.


 205      */
 206     public double orElse(double other) {
 207         return isPresent ? value : other;
 208     }
 209 
 210     /**
 211      * If a value is present, returns the value, otherwise returns the result
 212      * produced by the supplying function.
 213      *
 214      * @param supplier the supplying function that produces a value to be returned
 215      * @return the value, if present, otherwise the result produced by the
 216      *         supplying function
 217      * @throws NullPointerException if no value is present and the supplying
 218      *         function is {@code null}
 219      */
 220     public double orElseGet(DoubleSupplier supplier) {
 221         return isPresent ? value : supplier.getAsDouble();
 222     }
 223 
 224     /**
 225      * If a value is present, returns the value, otherwise throws
 226      * {@code NoSuchElementException}.
 227      *
 228      * @return the value described by this {@code OptionalDouble}
 229      * @throws NoSuchElementException if no value is present
 230      * @since 10
 231      */
 232     public double orElseThrow() {
 233         if (!isPresent) {
 234             throw new NoSuchElementException("No value present");
 235         }
 236         return value;
 237     }
 238 
 239     /**
 240      * If a value is present, returns the value, otherwise throws an exception
 241      * produced by the exception supplying function.
 242      *
 243      * @apiNote
 244      * A method reference to the exception constructor with an empty argument
 245      * list can be used as the supplier. For example,
 246      * {@code IllegalStateException::new}
 247      *
 248      * @param <X> Type of the exception to be thrown
 249      * @param exceptionSupplier the supplying function that produces an
 250      *        exception to be thrown
 251      * @return the value, if present
 252      * @throws X if no value is present
 253      * @throws NullPointerException if no value is present and the exception
 254      *         supplying function is {@code null}
 255      */
 256     public<X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
 257         if (isPresent) {
 258             return value;
 259         } else {


< prev index next >