1 /* 2 * Copyright (c) 2012, 2015, 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.Consumer; 28 import java.util.function.Function; 29 import java.util.function.Predicate; 30 import java.util.function.Supplier; 31 import java.util.stream.Stream; 32 33 /** 34 * A container object which may or may not contain a non-{@code null} value. 35 * If a value is present, {@code isPresent()} returns {@code true} and 36 * {@code get()} returns the value. 37 * 38 * <p>Additional methods that depend on the presence or absence of a contained 39 * value are provided, such as {@link #orElse(java.lang.Object) orElse()} 40 * (returns a default value if no value is present) and 41 * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (performs an 42 * action if a value is present). 43 * 44 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a> 45 * class; use of identity-sensitive operations (including reference equality 46 * ({@code ==}), identity hash code, or synchronization) on instances of 47 * {@code Optional} may have unpredictable results and should be avoided. 48 * 49 * @param <T> the type of value 50 * @since 1.8 51 */ 52 public final class Optional<T> { 53 /** 54 * Common instance for {@code empty()}. 55 */ 56 private static final Optional<?> EMPTY = new Optional<>(); 57 58 /** 59 * If non-null, the value; if null, indicates no value is present 60 */ 61 private final T value; 62 63 /** 64 * Constructs an empty instance. 65 * 66 * @implNote Generally only one empty instance, {@link Optional#EMPTY}, 67 * should exist per VM. 68 */ 69 private Optional() { 70 this.value = null; 71 } 72 73 /** 74 * Returns an empty {@code Optional} instance. No value is present for this 75 * {@code Optional}. 76 * 77 * @apiNote 78 * Though it may be tempting to do so, avoid testing if an object is empty 79 * by comparing with {@code ==} against instances returned by 80 * {@code Optional.empty()}. There is no guarantee that it is a singleton. 81 * Instead, use {@link #isPresent()}. 82 * 83 * @param <T> The type of the non-existent value 84 * @return an empty {@code Optional} 85 */ 86 public static<T> Optional<T> empty() { 87 @SuppressWarnings("unchecked") 88 Optional<T> t = (Optional<T>) EMPTY; 89 return t; 90 } 91 92 /** 93 * Constructs an instance with the described value. 94 * 95 * @param value the non-{@code null} value to describe 96 * @throws NullPointerException if value is {@code null} 97 */ 98 private Optional(T value) { 99 this.value = Objects.requireNonNull(value); 100 } 101 102 /** 103 * Returns an {@code Optional} describing the given non-{@code null} 104 * value. 105 * 106 * @param value the value to describe, which must be non-{@code null} 107 * @param <T> the type of the value 108 * @return an {@code Optional} with the value present 109 * @throws NullPointerException if value is {@code null} 110 */ 111 public static <T> Optional<T> of(T value) { 112 return new Optional<>(value); 113 } 114 115 /** 116 * Returns an {@code Optional} describing the given value, if 117 * non-{@code null}, otherwise returns an empty {@code Optional}. 118 * 119 * @param value the possibly-{@code null} value to describe 120 * @param <T> the type of the value 121 * @return an {@code Optional} with a present value if the specified value 122 * is non-{@code null}, otherwise an empty {@code Optional} 123 */ 124 public static <T> Optional<T> ofNullable(T value) { 125 return value == null ? empty() : of(value); 126 } 127 128 /** 129 * Equivalent to {@link #getWhenPresent()}. 130 * If a value is present, returns the value, otherwise throws 131 * {@code NoSuchElementException}. 132 * 133 * @deprecated 134 * This method's simple name {@code get} makes it the obvious method to 135 * call to retrieve the value from this {@code Optional}. However, it has 136 * no mechanism to avoid an exception if this {@code Optional} is empty. 137 * This tends to lead to code that mishandles empty {@code Optional} 138 * values. Consider using other methods that handle the case where 139 * the {@code Optional} might be empty, such as 140 * {@link #filter(java.util.function.Predicate) filter()}, 141 * {@link #map(java.util.function.Function) map()}, 142 * {@link #ifPresent(java.util.function.Consumer) ifPresent()} 143 * and related methods, and 144 * {@link #orElse(java.lang.Object) orElse()} and related methods. 145 * Use {@link getWhenPresent()} when it is known that a value is 146 * always present. 147 * 148 * @return the non-{@code null} value described by this {@code Optional} 149 * @throws NoSuchElementException if no value is present 150 * @see Optional#isPresent() 151 */ 152 @Deprecated(since="9") 153 public T get() { 154 return getWhenPresent(); 155 } 156 157 /** 158 * If a value is present, returns the value, otherwise throws 159 * {@code NoSuchElementException}. 160 * 161 * @apiNote 162 * Use this method only when it is known that a value is always present. 163 * 164 * @return the non-{@code null} value described by this {@code Optional} 165 * @throws NoSuchElementException if no value is present 166 * @see Optional#isPresent() 167 */ 168 public T getWhenPresent() { 169 if (value == null) { 170 throw new NoSuchElementException("No value present"); 171 } 172 return value; 173 } 174 175 /** 176 * If a value is present, returns {@code true}, otherwise {@code false}. 177 * 178 * @return {@code true} if a value is present, otherwise {@code false} 179 */ 180 public boolean isPresent() { 181 return value != null; 182 } 183 184 /** 185 * If a value is present, performs the given action with the value, 186 * otherwise does nothing. 187 * 188 * @param action the action to be performed, if a value is present 189 * @throws NullPointerException if value is present and the given action is 190 * {@code null} 191 */ 192 public void ifPresent(Consumer<? super T> action) { 193 if (value != null) { 194 action.accept(value); 195 } 196 } 197 198 /** 199 * If a value is present, performs the given action with the value, 200 * otherwise performs the given empty-based action. 201 * 202 * @param action the action to be performed, if a value is present 203 * @param emptyAction the empty-based action to be performed, if no value is 204 * present 205 * @throws NullPointerException if a value is present and the given action 206 * is {@code null}, or no value is present and the given empty-based 207 * action is {@code null}. 208 * @since 9 209 */ 210 public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) { 211 if (value != null) { 212 action.accept(value); 213 } else { 214 emptyAction.run(); 215 } 216 } 217 218 /** 219 * If a value is present, and the value matches the given predicate, 220 * returns an {@code Optional} describing the value, otherwise returns an 221 * empty {@code Optional}. 222 * 223 * @param predicate the predicate to apply to a value, if present 224 * @return an {@code Optional} describing the value of this 225 * {@code Optional}, if a value is present and the value matches the 226 * given predicate, otherwise an empty {@code Optional} 227 * @throws NullPointerException if the predicate is {@code null} 228 */ 229 public Optional<T> filter(Predicate<? super T> predicate) { 230 Objects.requireNonNull(predicate); 231 if (!isPresent()) { 232 return this; 233 } else { 234 return predicate.test(value) ? this : empty(); 235 } 236 } 237 238 /** 239 * If a value is present, returns an {@code Optional} describing (as if by 240 * {@link #ofNullable}) the result of applying the given mapping function to 241 * the value, otherwise returns an empty {@code Optional}. 242 * 243 * <p>If the mapping function returns a {@code null} result then this method 244 * returns an empty {@code Optional}. 245 * 246 * @apiNote 247 * This method supports post-processing on {@code Optional} values, without 248 * the need to explicitly check for a return status. For example, the 249 * following code traverses a stream of file names, selects one that has not 250 * yet been processed, and then opens that file, returning an 251 * {@code Optional<FileInputStream>}: 252 * 253 * <pre>{@code 254 * Optional<FileInputStream> fis = 255 * names.stream().filter(name -> !isProcessedYet(name)) 256 * .findFirst() 257 * .map(name -> new FileInputStream(name)); 258 * }</pre> 259 * 260 * Here, {@code findFirst} returns an {@code Optional<String>}, and then 261 * {@code map} returns an {@code Optional<FileInputStream>} for the desired 262 * file if one exists. 263 * 264 * @param mapper the mapping function to apply to a value, if present 265 * @param <U> The type of the value returned from the mapping function 266 * @return an {@code Optional} describing the result of applying a mapping 267 * function to the value of this {@code Optional}, if a value is 268 * present, otherwise an empty {@code Optional} 269 * @throws NullPointerException if the mapping function is {@code null} 270 */ 271 public<U> Optional<U> map(Function<? super T, ? extends U> mapper) { 272 Objects.requireNonNull(mapper); 273 if (!isPresent()) { 274 return empty(); 275 } else { 276 return Optional.ofNullable(mapper.apply(value)); 277 } 278 } 279 280 /** 281 * If a value is present, returns the result of applying the given 282 * {@code Optional}-bearing mapping function to the value, otherwise returns 283 * an empty {@code Optional}. 284 * 285 * <p>This method is similar to {@link #map(Function)}, but the mapping 286 * function is one whose result is already an {@code Optional}, and if 287 * invoked, {@code flatMap} does not wrap it within an additional 288 * {@code Optional}. 289 * 290 * @param <U> The type of value of the {@code Optional} returned by the 291 * mapping function 292 * @param mapper the mapping function to apply to a value, if present 293 * @return the result of applying an {@code Optional}-bearing mapping 294 * function to the value of this {@code Optional}, if a value is 295 * present, otherwise an empty {@code Optional} 296 * @throws NullPointerException if the mapping function is {@code null} or 297 * returns a {@code null} result 298 */ 299 public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) { 300 Objects.requireNonNull(mapper); 301 if (!isPresent()) { 302 return empty(); 303 } else { 304 return Objects.requireNonNull(mapper.apply(value)); 305 } 306 } 307 308 /** 309 * If a value is present, returns an {@code Optional} describing the value, 310 * otherwise returns an {@code Optional} produced by the supplying function. 311 * 312 * @param supplier the supplying function that produces an {@code Optional} 313 * to be returned 314 * @return returns an {@code Optional} describing the value of this 315 * {@code Optional}, if a value is present, otherwise an 316 * {@code Optional} produced by the supplying function. 317 * @throws NullPointerException if the supplying function is {@code null} or 318 * produces a {@code null} result 319 * @since 9 320 */ 321 public Optional<T> or(Supplier<Optional<T>> supplier) { 322 Objects.requireNonNull(supplier); 323 if (isPresent()) { 324 return this; 325 } else { 326 return Objects.requireNonNull(supplier.get()); 327 } 328 } 329 330 /** 331 * If a value is present, returns a sequential {@link Stream} containing 332 * only that value, otherwise returns an empty {@code Stream}. 333 * 334 * @apiNote 335 * This method can be used to transform a {@code Stream} of optional 336 * elements to a {@code Stream} of present value elements: 337 * <pre>{@code 338 * Stream<Optional<T>> os = .. 339 * Stream<T> s = os.flatMap(Optional::stream) 340 * }</pre> 341 * 342 * @return the optional value as a {@code Stream} 343 * @since 9 344 */ 345 public Stream<T> stream() { 346 if (!isPresent()) { 347 return Stream.empty(); 348 } else { 349 return Stream.of(value); 350 } 351 } 352 353 /** 354 * If a value is present, returns the value, otherwise returns 355 * {@code other}. 356 * 357 * @param other the value to be returned, if no value is present. 358 * May be {@code null}. 359 * @return the value, if present, otherwise {@code other} 360 */ 361 public T orElse(T other) { 362 return value != null ? value : other; 363 } 364 365 /** 366 * If a value is present, returns the value, otherwise returns the result 367 * produced by the supplying function. 368 * 369 * @param supplier the supplying function that produces a value to be returned 370 * @return the value, if present, otherwise the result produced by the 371 * supplying function 372 * @throws NullPointerException if no value is present and the supplying 373 * function is {@code null} 374 */ 375 public T orElseGet(Supplier<? extends T> supplier) { 376 return value != null ? value : supplier.get(); 377 } 378 379 /** 380 * If a value is present, returns the value, otherwise throws an exception 381 * produced by the exception supplying function. 382 * 383 * @apiNote 384 * A method reference to the exception constructor with an empty argument 385 * list can be used as the supplier. For example, 386 * {@code IllegalStateException::new} 387 * 388 * @param <X> Type of the exception to be thrown 389 * @param exceptionSupplier the supplying function that produces an 390 * exception to be thrown 391 * @return the value, if present 392 * @throws X if no value is present 393 * @throws NullPointerException if no value is present and the exception 394 * supplying function is {@code null} 395 */ 396 public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X { 397 if (value != null) { 398 return value; 399 } else { 400 throw exceptionSupplier.get(); 401 } 402 } 403 404 /** 405 * Indicates whether some other object is "equal to" this {@code Optional}. 406 * The other object is considered equal if: 407 * <ul> 408 * <li>it is also an {@code Optional} and; 409 * <li>both instances have no value present or; 410 * <li>the present values are "equal to" each other via {@code equals()}. 411 * </ul> 412 * 413 * @param obj an object to be tested for equality 414 * @return {@code true} if the other object is "equal to" this object 415 * otherwise {@code false} 416 */ 417 @Override 418 public boolean equals(Object obj) { 419 if (this == obj) { 420 return true; 421 } 422 423 if (!(obj instanceof Optional)) { 424 return false; 425 } 426 427 Optional<?> other = (Optional<?>) obj; 428 return Objects.equals(value, other.value); 429 } 430 431 /** 432 * Returns the hash code of the value, if present, otherwise {@code 0} 433 * (zero) if no value is present. 434 * 435 * @return hash code value of the present value or {@code 0} if no value is 436 * present 437 */ 438 @Override 439 public int hashCode() { 440 return Objects.hashCode(value); 441 } 442 443 /** 444 * Returns a non-empty string representation of this {@code Optional} 445 * suitable for debugging. The exact presentation format is unspecified and 446 * may vary between implementations and versions. 447 * 448 * @implSpec 449 * If a value is present the result must include its string representation 450 * in the result. Empty and present {@code Optional}s must be unambiguously 451 * differentiable. 452 * 453 * @return the string representation of this instance 454 */ 455 @Override 456 public String toString() { 457 return value != null 458 ? String.format("Optional[%s]", value) 459 : "Optional.empty"; 460 } 461 }