< prev index next >

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

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


  90      * Construct an instance with the described value.
  91      *
  92      * @param value the long value to describe
  93      */
  94     private OptionalLong(long value) {
  95         this.isPresent = true;
  96         this.value = value;
  97     }
  98 
  99     /**
 100      * Returns an {@code OptionalLong} describing the given value.
 101      *
 102      * @param value the value to describe
 103      * @return an {@code OptionalLong} with the value present
 104      */
 105     public static OptionalLong of(long value) {
 106         return new OptionalLong(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 OptionalLong}
 114      * @throws NoSuchElementException if no value is present
 115      * @see OptionalLong#isPresent()
 116      */

 117     public long getAsLong() {
 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(LongConsumer action) {
 142         if (isPresent) {
 143             action.accept(value);
 144         }




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


< prev index next >