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 23 * questions. 24 */ 25 package java.util; 26 27 import java.util.function.IntConsumer; 28 import java.util.function.IntSupplier; 29 import java.util.function.Supplier; 30 import java.util.stream.IntStream; 31 32 // Android-changed: removed ValueBased paragraph. 33 /** 34 * A container object which may or may not contain an {@code int} value. 35 * If a value is present, {@code isPresent()} returns {@code true}. If no 36 * value is present, the object is considered <i>empty</i> and 37 * {@code isPresent()} returns {@code false}. 38 * 39 * <p>Additional methods that depend on the presence or absence of a contained 40 * value are provided, such as {@link #orElse(int) orElse()} 41 * (returns a default value if no value is present) and 42 * {@link #ifPresent(IntConsumer) ifPresent()} (performs an 43 * action if a value is present). 44 * 45 * 46 * @apiNote 47 * {@code OptionalInt} is primarily intended for use as a method return type where 48 * there is a clear need to represent "no result." A variable whose type is 49 * {@code OptionalInt} should never itself be {@code null}; it should always point 50 * to an {@code OptionalInt} instance. 51 * 52 * @since 1.8 53 */ 54 public final class OptionalInt { 55 /** 56 * Common instance for {@code empty()}. 57 */ 58 private static final OptionalInt EMPTY = new OptionalInt(); 59 60 /** 61 * If true then the value is present, otherwise indicates no value is present 62 */ 63 private final boolean isPresent; 64 private final int value; 65 66 /** 67 * Construct an empty instance. 68 * 69 * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY}, 70 * should exist per VM. 71 */ OptionalInt()72 private OptionalInt() { 73 this.isPresent = false; 74 this.value = 0; 75 } 76 77 /** 78 * Returns an empty {@code OptionalInt} instance. No value is present for 79 * this {@code OptionalInt}. 80 * 81 * @apiNote 82 * Though it may be tempting to do so, avoid testing if an object is empty 83 * by comparing with {@code ==} against instances returned by 84 * {@code OptionalInt.empty()}. There is no guarantee that it is a singleton. 85 * Instead, use {@link #isPresent()}. 86 * 87 * @return an empty {@code OptionalInt} 88 */ empty()89 public static OptionalInt empty() { 90 return EMPTY; 91 } 92 93 /** 94 * Construct an instance with the described value. 95 * 96 * @param value the int value to describe 97 */ OptionalInt(int value)98 private OptionalInt(int value) { 99 this.isPresent = true; 100 this.value = value; 101 } 102 103 /** 104 * Returns an {@code OptionalInt} describing the given value. 105 * 106 * @param value the value to describe 107 * @return an {@code OptionalInt} with the value present 108 */ of(int value)109 public static OptionalInt of(int value) { 110 return new OptionalInt(value); 111 } 112 113 /** 114 * If a value is present, returns the value, otherwise throws 115 * {@code NoSuchElementException}. 116 * 117 * @apiNote 118 * The preferred alternative to this method is {@link #orElseThrow()}. 119 * 120 * @return the value described by this {@code OptionalInt} 121 * @throws NoSuchElementException if no value is present 122 */ getAsInt()123 public int getAsInt() { 124 if (!isPresent) { 125 throw new NoSuchElementException("No value present"); 126 } 127 return value; 128 } 129 130 /** 131 * If a value is present, returns {@code true}, otherwise {@code false}. 132 * 133 * @return {@code true} if a value is present, otherwise {@code false} 134 */ isPresent()135 public boolean isPresent() { 136 return isPresent; 137 } 138 139 /** 140 * If a value is not present, returns {@code true}, otherwise 141 * {@code false}. 142 * 143 * @return {@code true} if a value is not present, otherwise {@code false} 144 * @since 11 145 */ isEmpty()146 public boolean isEmpty() { 147 return !isPresent; 148 } 149 150 /** 151 * If a value is present, performs the given action with the value, 152 * otherwise does nothing. 153 * 154 * @param action the action to be performed, if a value is present 155 * @throws NullPointerException if value is present and the given action is 156 * {@code null} 157 */ ifPresent(IntConsumer action)158 public void ifPresent(IntConsumer action) { 159 if (isPresent) { 160 action.accept(value); 161 } 162 } 163 164 /** 165 * If a value is present, performs the given action with the value, 166 * otherwise performs the given empty-based action. 167 * 168 * @param action the action to be performed, if a value is present 169 * @param emptyAction the empty-based action to be performed, if no value is 170 * present 171 * @throws NullPointerException if a value is present and the given action 172 * is {@code null}, or no value is present and the given empty-based 173 * action is {@code null}. 174 * @since 9 175 */ ifPresentOrElse(IntConsumer action, Runnable emptyAction)176 public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) { 177 if (isPresent) { 178 action.accept(value); 179 } else { 180 emptyAction.run(); 181 } 182 } 183 184 /** 185 * If a value is present, returns a sequential {@link IntStream} containing 186 * only that value, otherwise returns an empty {@code IntStream}. 187 * 188 * @apiNote 189 * This method can be used to transform a {@code Stream} of optional 190 * integers to an {@code IntStream} of present integers: 191 * <pre>{@code 192 * Stream<OptionalInt> os = .. 193 * IntStream s = os.flatMapToInt(OptionalInt::stream) 194 * }</pre> 195 * 196 * @return the optional value as an {@code IntStream} 197 * @since 9 198 */ stream()199 public IntStream stream() { 200 if (isPresent) { 201 return IntStream.of(value); 202 } else { 203 return IntStream.empty(); 204 } 205 } 206 207 /** 208 * If a value is present, returns the value, otherwise returns 209 * {@code other}. 210 * 211 * @param other the value to be returned, if no value is present 212 * @return the value, if present, otherwise {@code other} 213 */ orElse(int other)214 public int orElse(int other) { 215 return isPresent ? value : other; 216 } 217 218 /** 219 * If a value is present, returns the value, otherwise returns the result 220 * produced by the supplying function. 221 * 222 * @param supplier the supplying function that produces a value to be returned 223 * @return the value, if present, otherwise the result produced by the 224 * supplying function 225 * @throws NullPointerException if no value is present and the supplying 226 * function is {@code null} 227 */ orElseGet(IntSupplier supplier)228 public int orElseGet(IntSupplier supplier) { 229 return isPresent ? value : supplier.getAsInt(); 230 } 231 232 /** 233 * If a value is present, returns the value, otherwise throws 234 * {@code NoSuchElementException}. 235 * 236 * @return the value described by this {@code OptionalInt} 237 * @throws NoSuchElementException if no value is present 238 * @since 10 239 */ orElseThrow()240 public int orElseThrow() { 241 if (!isPresent) { 242 throw new NoSuchElementException("No value present"); 243 } 244 return value; 245 } 246 247 /** 248 * If a value is present, returns the value, otherwise throws an exception 249 * produced by the exception supplying function. 250 * 251 * @apiNote 252 * A method reference to the exception constructor with an empty argument 253 * list can be used as the supplier. For example, 254 * {@code IllegalStateException::new} 255 * 256 * @param <X> Type of the exception to be thrown 257 * @param exceptionSupplier the supplying function that produces an 258 * exception to be thrown 259 * @return the value, if present 260 * @throws X if no value is present 261 * @throws NullPointerException if no value is present and the exception 262 * supplying function is {@code null} 263 */ orElseThrow(Supplier<? extends X> exceptionSupplier)264 public<X extends Throwable> int orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { 265 if (isPresent) { 266 return value; 267 } else { 268 throw exceptionSupplier.get(); 269 } 270 } 271 272 /** 273 * Indicates whether some other object is "equal to" this 274 * {@code OptionalInt}. The other object is considered equal if: 275 * <ul> 276 * <li>it is also an {@code OptionalInt} and; 277 * <li>both instances have no value present or; 278 * <li>the present values are "equal to" each other via {@code ==}. 279 * </ul> 280 * 281 * @param obj an object to be tested for equality 282 * @return {@code true} if the other object is "equal to" this object 283 * otherwise {@code false} 284 */ 285 @Override equals(Object obj)286 public boolean equals(Object obj) { 287 if (this == obj) { 288 return true; 289 } 290 291 if (!(obj instanceof OptionalInt)) { 292 return false; 293 } 294 295 OptionalInt other = (OptionalInt) obj; 296 return (isPresent && other.isPresent) 297 ? value == other.value 298 : isPresent == other.isPresent; 299 } 300 301 /** 302 * Returns the hash code of the value, if present, otherwise {@code 0} 303 * (zero) if no value is present. 304 * 305 * @return hash code value of the present value or {@code 0} if no value is 306 * present 307 */ 308 @Override hashCode()309 public int hashCode() { 310 return isPresent ? Integer.hashCode(value) : 0; 311 } 312 313 /** 314 * Returns a non-empty string representation of this {@code OptionalInt} 315 * suitable for debugging. The exact presentation format is unspecified and 316 * may vary between implementations and versions. 317 * 318 * @implSpec 319 * If a value is present the result must include its string representation 320 * in the result. Empty and present {@code OptionalInt}s must be 321 * unambiguously differentiable. 322 * 323 * @return the string representation of this instance 324 */ 325 @Override toString()326 public String toString() { 327 return isPresent 328 ? String.format("OptionalInt[%s]", value) 329 : "OptionalInt.empty"; 330 } 331 } 332