< prev index next >

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

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


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

 117     public int getAsInt() {















 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




  90      * Construct an instance with the described value.
  91      *
  92      * @param value the int value to describe
  93      */
  94     private OptionalInt(int value) {
  95         this.isPresent = true;
  96         this.value = value;
  97     }
  98 
  99     /**
 100      * Returns an {@code OptionalInt} describing the given value.
 101      *
 102      * @param value the value to describe
 103      * @return an {@code OptionalInt} with the value present
 104      */
 105     public static OptionalInt of(int value) {
 106         return new OptionalInt(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 getAsInt} makes it the obvious method to
 116      * call to retrieve the value from this {@code OptionalInt}. However, it has
 117      * no mechanism to avoid an exception if this {@code OptionalInt} is empty.
 118      * This tends to lead to code that mishandles empty {@code OptionalInt}
 119      * values. Consider using other methods that handle the case where
 120      * the {@code OptionalInt} might be empty, such as
 121      * {@link #ifPresent(java.util.function.IntConsumer) ifPresent()}
 122      * and related methods, and
 123      * {@link #orElse(int) 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 OptionalInt}
 128      * @throws NoSuchElementException if no value is present
 129      * @see OptionalInt#isPresent()
 130      */
 131     @Deprecated(since="9")
 132     public int getAsInt() {
 133         return getWhenPresent();
 134     }
 135 
 136     /**
 137      * If a value is present, returns the value, otherwise throws
 138      * {@code NoSuchElementException}.
 139      *
 140      * @apiNote
 141      * Use this method only when it is known that a value is always present.
 142      *
 143      * @return the value described by this {@code OptionalInt}
 144      * @throws NoSuchElementException if no value is present
 145      * @see OptionalInt#isPresent()
 146      */
 147     public int getWhenPresent() {
 148         if (!isPresent) {
 149             throw new NoSuchElementException("No value present");
 150         }
 151         return value;
 152     }
 153 
 154     /**
 155      * If a value is present, returns {@code true}, otherwise {@code false}.
 156      *
 157      * @return {@code true} if a value is present, otherwise {@code false}
 158      */
 159     public boolean isPresent() {
 160         return isPresent;
 161     }
 162 
 163     /**
 164      * If a value is present, performs the given action with the value,
 165      * otherwise does nothing.
 166      *
 167      * @param action the action to be performed, if a value is present


< prev index next >