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.DoubleConsumer;
28 import java.util.function.DoubleSupplier;
29 import java.util.function.Supplier;
30 import java.util.stream.DoubleStream;
31
32 /**
33 * A container object which may or may not contain a {@code double} value.
34 * If a value is present, {@code isPresent()} will return {@code true} and
35 * {@code getAsDouble()} 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(double) orElse()}
39 * (return a default value if value not present) and
40 * {@link #ifPresent(java.util.function.DoubleConsumer) 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 OptionalDouble} may have unpredictable results and should be avoided.
47 *
48 * @since 1.8
49 */
50 public final class OptionalDouble {
51 /**
52 * Common instance for {@code empty()}.
53 */
54 private static final OptionalDouble EMPTY = new OptionalDouble();
55
56 /**
57 * If true then the value is present, otherwise indicates no value is present
58 */
59 private final boolean isPresent;
60 private final double value;
61
114 *
115 * @see OptionalDouble#isPresent()
116 */
117 public double getAsDouble() {
118 if (!isPresent) {
119 throw new NoSuchElementException("No value present");
120 }
121 return value;
122 }
123
124 /**
125 * Return {@code true} if there is a value present, otherwise {@code false}.
126 *
127 * @return {@code true} if there is a value present, otherwise {@code false}
128 */
129 public boolean isPresent() {
130 return isPresent;
131 }
132
133 /**
134 * Have the specified consumer accept the value if a value is present,
135 * otherwise do nothing.
136 *
137 * @param consumer block to be executed if a value is present
138 * @throws NullPointerException if value is present and {@code consumer} is
139 * null
140 */
141 public void ifPresent(DoubleConsumer consumer) {
142 if (isPresent) {
143 consumer.accept(value);
144 }
145 }
146
147 /**
148 * If a value is present return a sequential {@link DoubleStream} containing
149 * only that value, otherwise return an empty {@code DoubleStream}.
150 *
151 * @apiNote This method can be used to transform a {@code Stream} of
152 * optional doubles to a {@code DoubleStream} of present doubles:
153 *
154 * <pre>{@code
155 * Stream<OptionalDouble> os = ..
156 * DoubleStream s = os.flatMapToDouble(OptionalDouble::stream)
157 * }</pre>
158 *
159 * @return the optional value as a {@code DoubleStream}
160 * @since 1.9
161 */
162 public DoubleStream stream() {
163 if (isPresent) {
|
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.DoubleConsumer;
28 import java.util.function.DoubleSupplier;
29 import java.util.function.Supplier;
30 import java.util.stream.DoubleStream;
31
32 /**
33 * A container object which may or may not contain a {@code double} value.
34 * If a value is present, {@code isPresent()} will return {@code true} and
35 * {@code getAsDouble()} 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(double) orElse()}
39 * (return a default value if value not present) and
40 * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (perform an
41 * action 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 OptionalDouble} may have unpredictable results and should be avoided.
47 *
48 * @since 1.8
49 */
50 public final class OptionalDouble {
51 /**
52 * Common instance for {@code empty()}.
53 */
54 private static final OptionalDouble EMPTY = new OptionalDouble();
55
56 /**
57 * If true then the value is present, otherwise indicates no value is present
58 */
59 private final boolean isPresent;
60 private final double value;
61
114 *
115 * @see OptionalDouble#isPresent()
116 */
117 public double getAsDouble() {
118 if (!isPresent) {
119 throw new NoSuchElementException("No value present");
120 }
121 return value;
122 }
123
124 /**
125 * Return {@code true} if there is a value present, otherwise {@code false}.
126 *
127 * @return {@code true} if there is a value present, otherwise {@code false}
128 */
129 public boolean isPresent() {
130 return isPresent;
131 }
132
133 /**
134 * If a value is present, perform the given action with the value,
135 * otherwise do nothing.
136 *
137 * @param action the action to be performed if a value is present
138 * @throws NullPointerException if a value is present and {@code action} is
139 * null
140 */
141 public void ifPresent(DoubleConsumer action) {
142 if (isPresent) {
143 action.accept(value);
144 }
145 }
146
147 /**
148 * If a value is present, perform the given action with the value,
149 * otherwise perform the given empty-based action.
150 *
151 * @param action the action to be performed if a value is present
152 * @param emptyAction the empty-based action to be performed if a value is
153 * not present
154 * @throws NullPointerException if a value is present and {@code action} is
155 * null, or a value is not present and {@code emptyAction} is null.
156 * @since 1.9
157 */
158 public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
159 if (isPresent) {
160 action.accept(value);
161 } else {
162 emptyAction.run();
163 }
164 }
165
166 /**
167 * If a value is present return a sequential {@link DoubleStream} containing
168 * only that value, otherwise return an empty {@code DoubleStream}.
169 *
170 * @apiNote This method can be used to transform a {@code Stream} of
171 * optional doubles to a {@code DoubleStream} of present doubles:
172 *
173 * <pre>{@code
174 * Stream<OptionalDouble> os = ..
175 * DoubleStream s = os.flatMapToDouble(OptionalDouble::stream)
176 * }</pre>
177 *
178 * @return the optional value as a {@code DoubleStream}
179 * @since 1.9
180 */
181 public DoubleStream stream() {
182 if (isPresent) {
|