< prev index next >

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

Print this page

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

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

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