< prev index next >

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

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


 100     private OptionalLong(long value) {
 101         this.isPresent = true;
 102         this.value = value;
 103     }
 104 
 105     /**
 106      * Returns an {@code OptionalLong} describing the given value.
 107      *
 108      * @param value the value to describe
 109      * @return an {@code OptionalLong} with the value present
 110      */
 111     public static OptionalLong of(long value) {
 112         return new OptionalLong(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(long) orElse} and
 121      * {@link #orElseGet(LongSupplier) 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 OptionalLong}
 126      * @throws NoSuchElementException if no value is present
 127      * @see OptionalLong#isPresent()
 128      */
 129     public long getAsLong() {
 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.


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















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




 100     private OptionalLong(long value) {
 101         this.isPresent = true;
 102         this.value = value;
 103     }
 104 
 105     /**
 106      * Returns an {@code OptionalLong} describing the given value.
 107      *
 108      * @param value the value to describe
 109      * @return an {@code OptionalLong} with the value present
 110      */
 111     public static OptionalLong of(long value) {
 112         return new OptionalLong(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 OptionalLong}
 123      * @throws NoSuchElementException if no value is present

 124      */
 125     public long getAsLong() {
 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.


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


< prev index next >