< prev index next >

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

Print this page




 123      * @return the value described by this {@code OptionalInt}
 124      * @throws NoSuchElementException if no value is present
 125      */
 126     public int getAsInt() {
 127         if (!isPresent) {
 128             throw new NoSuchElementException("No value present");
 129         }
 130         return value;
 131     }
 132 
 133     /**
 134      * If a value is present, returns {@code true}, otherwise {@code false}.
 135      *
 136      * @return {@code true} if a value is present, otherwise {@code false}
 137      */
 138     public boolean isPresent() {
 139         return isPresent;
 140     }
 141 
 142     /**










 143      * If a value is present, performs the given action with the value,
 144      * otherwise does nothing.
 145      *
 146      * @param action the action to be performed, if a value is present
 147      * @throws NullPointerException if value is present and the given action is
 148      *         {@code null}
 149      */
 150     public void ifPresent(IntConsumer action) {
 151         if (isPresent) {
 152             action.accept(value);
 153         }
 154     }
 155 
 156     /**
 157      * If a value is present, performs the given action with the value,
 158      * otherwise performs the given empty-based action.
 159      *
 160      * @param action the action to be performed, if a value is present
 161      * @param emptyAction the empty-based action to be performed, if no value is
 162      *        present




 123      * @return the value described by this {@code OptionalInt}
 124      * @throws NoSuchElementException if no value is present
 125      */
 126     public int getAsInt() {
 127         if (!isPresent) {
 128             throw new NoSuchElementException("No value present");
 129         }
 130         return value;
 131     }
 132 
 133     /**
 134      * If a value is present, returns {@code true}, otherwise {@code false}.
 135      *
 136      * @return {@code true} if a value is present, otherwise {@code false}
 137      */
 138     public boolean isPresent() {
 139         return isPresent;
 140     }
 141 
 142     /**
 143      * If a value is not present, returns {@code true}, otherwise
 144      * {@code false}.
 145      *
 146      * @return {@code true} if a value is not present, otherwise {@code false}
 147      */
 148     public boolean isEmpty() {
 149         return !isPresent;
 150     }
 151 
 152     /**
 153      * If a value is present, performs the given action with the value,
 154      * otherwise does nothing.
 155      *
 156      * @param action the action to be performed, if a value is present
 157      * @throws NullPointerException if value is present and the given action is
 158      *         {@code null}
 159      */
 160     public void ifPresent(IntConsumer action) {
 161         if (isPresent) {
 162             action.accept(value);
 163         }
 164     }
 165 
 166     /**
 167      * If a value is present, performs the given action with the value,
 168      * otherwise performs the given empty-based action.
 169      *
 170      * @param action the action to be performed, if a value is present
 171      * @param emptyAction the empty-based action to be performed, if no value is
 172      *        present


< prev index next >