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
32 /**
33 * A container object which may or may not contain a non-null value.
34 * If a value is present, {@code isPresent()} will return {@code true} and
35 * {@code get()} will return the value.
36 *
37 * <p>Additional methods that depend on the presence or absence of a contained
38 * value are provided, such as {@link #orElse(java.lang.Object) orElse()}
39 * (return a default value if value not present) and
40 * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (execute a block
41 * of code if the value is present).
42 *
43 * @since 1.8
44 */
45 public final class Optional<T> {
46 /**
47 * Common instance for {@code empty()}.
48 */
49 private static final Optional<?> EMPTY = new Optional<>();
50
51 /**
52 * If non-null, the value; if null, indicates no value is present
53 */
54 private final T value;
55
56 /**
57 * Constructs an empty instance.
58 *
59 * @implNote Generally only one empty instance, {@link Optional#EMPTY},
60 * should exist per VM.
61 */
62 private Optional() {
|
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
32 /**
33 * A container object which may or may not contain a non-null value.
34 * If a value is present, {@code isPresent()} will return {@code true} and
35 * {@code get()} will return the value.
36 *
37 * <p>Additional methods that depend on the presence or absence of a contained
38 * value are provided, such as {@link #orElse(java.lang.Object) orElse()}
39 * (return a default value if value not present) and
40 * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (execute a block
41 * of code if the value is present).
42 *
43 * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
44 * class; use of identity-sensitive operations (including reference equality
45 * ({@code ==}), identity hash code, or synchronization) on instances of
46 * {@code Optional} may have unpredictable effects and should be avoided.
47 *
48 * @since 1.8
49 */
50 public final class Optional<T> {
51 /**
52 * Common instance for {@code empty()}.
53 */
54 private static final Optional<?> EMPTY = new Optional<>();
55
56 /**
57 * If non-null, the value; if null, indicates no value is present
58 */
59 private final T value;
60
61 /**
62 * Constructs an empty instance.
63 *
64 * @implNote Generally only one empty instance, {@link Optional#EMPTY},
65 * should exist per VM.
66 */
67 private Optional() {
|