1 /*
2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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
|
1 /*
2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
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
|