25 package java.util;
26
27 import java.util.function.LongConsumer;
28 import java.util.function.LongSupplier;
29 import java.util.function.Supplier;
30 import java.util.stream.LongStream;
31
32 /**
33 * A container object which may or may not contain a {@code long} value.
34 * If a value is present, {@code isPresent()} returns {@code true}. If no
35 * value is present, the object is considered <i>empty</i> and
36 * {@code isPresent()} returns {@code false}.
37 *
38 * <p>Additional methods that depend on the presence or absence of a contained
39 * value are provided, such as {@link #orElse(long) orElse()}
40 * (returns a default value if no value is present) and
41 * {@link #ifPresent(LongConsumer) ifPresent()} (performs an
42 * action if a value is present).
43 *
44 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
45 * class; programmers should treat instances that are
46 * {@linkplain #equals(Object) equal} as interchangeable and should not
47 * use instances for synchronization, or unpredictable behavior may
48 * occur. For example, in a future release, synchronization may fail.
49 *
50 * @apiNote
51 * {@code OptionalLong} is primarily intended for use as a method return type where
52 * there is a clear need to represent "no result." A variable whose type is
53 * {@code OptionalLong} should never itself be {@code null}; it should always point
54 * to an {@code OptionalLong} instance.
55 *
56 * @since 1.8
57 */
58 @jdk.internal.ValueBased
59 public final class OptionalLong {
60 /**
61 * Common instance for {@code empty()}.
62 */
63 private static final OptionalLong EMPTY = new OptionalLong();
64
65 /**
66 * If true then the value is present, otherwise indicates no value is present
67 */
68 private final boolean isPresent;
69 private final long value;
70
71 /**
72 * Construct an empty instance.
73 *
74 * @implNote generally only one empty instance, {@link OptionalLong#EMPTY},
75 * should exist per VM.
76 */
77 private OptionalLong() {
78 this.isPresent = false;
|
25 package java.util;
26
27 import java.util.function.LongConsumer;
28 import java.util.function.LongSupplier;
29 import java.util.function.Supplier;
30 import java.util.stream.LongStream;
31
32 /**
33 * A container object which may or may not contain a {@code long} value.
34 * If a value is present, {@code isPresent()} returns {@code true}. If no
35 * value is present, the object is considered <i>empty</i> and
36 * {@code isPresent()} returns {@code false}.
37 *
38 * <p>Additional methods that depend on the presence or absence of a contained
39 * value are provided, such as {@link #orElse(long) orElse()}
40 * (returns a default value if no value is present) and
41 * {@link #ifPresent(LongConsumer) ifPresent()} (performs an
42 * action if a value is present).
43 *
44 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
45 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
46 * as interchangeable and should not use instances for synchronization, mutexes, or
47 * with {@linkplain java.lang.ref.Reference object references}.
48 *
49 * <div class="preview-block">
50 * <div class="preview-comment">
51 * When preview features are enabled, {@code OptionalLong} is a {@linkplain Class#isValue value class}.
52 * Use of value class instances for synchronization, mutexes, or with
53 * {@linkplain java.lang.ref.Reference object references} result in
54 * {@link IdentityException}.
55 * </div>
56 * </div>
57 *
58 * @apiNote
59 * {@code OptionalLong} is primarily intended for use as a method return type where
60 * there is a clear need to represent "no result." A variable whose type is
61 * {@code OptionalLong} should never itself be {@code null}; it should always point
62 * to an {@code OptionalLong} instance.
63 *
64 * @since 1.8
65 */
66 @jdk.internal.ValueBased
67 @jdk.internal.MigratedValueClass
68 public final class OptionalLong {
69 /**
70 * Common instance for {@code empty()}.
71 */
72 private static final OptionalLong EMPTY = new OptionalLong();
73
74 /**
75 * If true then the value is present, otherwise indicates no value is present
76 */
77 private final boolean isPresent;
78 private final long value;
79
80 /**
81 * Construct an empty instance.
82 *
83 * @implNote generally only one empty instance, {@link OptionalLong#EMPTY},
84 * should exist per VM.
85 */
86 private OptionalLong() {
87 this.isPresent = false;
|