< prev index next >

src/java.base/share/classes/java/util/OptionalInt.java

Print this page

 39  * value are provided, such as {@link #orElse(int) orElse()}
 40  * (returns a default value if no value is present) and
 41  * {@link #ifPresent(IntConsumer) 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 OptionalInt} 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 OptionalInt} should never itself be {@code null}; it should always point
 54  * to an {@code OptionalInt} instance.
 55  *
 56  * @since 1.8
 57  */
 58 @jdk.internal.ValueBased

 59 public final class OptionalInt {
 60     /**
 61      * Common instance for {@code empty()}.
 62      */
 63     private static final OptionalInt EMPTY = new OptionalInt();
 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 int value;
 70 
 71     /**
 72      * Construct an empty instance.
 73      *
 74      * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
 75      * should exist per VM.
 76      */
 77     private OptionalInt() {
 78         this.isPresent = false;

 39  * value are provided, such as {@link #orElse(int) orElse()}
 40  * (returns a default value if no value is present) and
 41  * {@link #ifPresent(IntConsumer) 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 OptionalInt} 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 OptionalInt} should never itself be {@code null}; it should always point
 54  * to an {@code OptionalInt} instance.
 55  *
 56  * @since 1.8
 57  */
 58 @jdk.internal.ValueBased
 59 @jdk.internal.MigratedValueClass
 60 public final class OptionalInt {
 61     /**
 62      * Common instance for {@code empty()}.
 63      */
 64     private static final OptionalInt EMPTY = new OptionalInt();
 65 
 66     /**
 67      * If true then the value is present, otherwise indicates no value is present
 68      */
 69     private final boolean isPresent;
 70     private final int value;
 71 
 72     /**
 73      * Construct an empty instance.
 74      *
 75      * @implNote Generally only one empty instance, {@link OptionalInt#EMPTY},
 76      * should exist per VM.
 77      */
 78     private OptionalInt() {
 79         this.isPresent = false;
< prev index next >