< prev index next >
src/java.base/share/classes/java/util/OptionalDouble.java
Print this page
rev 11381 : 8071670: java.util.Optional: please add a way to specify if-else behavior
Reviewed-by: dfuchs
*** 35,46 ****
* {@code getAsDouble()} will return the value.
*
* <p>Additional methods that depend on the presence or absence of a contained
* value are provided, such as {@link #orElse(double) orElse()}
* (return a default value if value not present) and
! * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (execute a block
! * of code if the value is present).
*
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
* class; use of identity-sensitive operations (including reference equality
* ({@code ==}), identity hash code, or synchronization) on instances of
* {@code OptionalDouble} may have unpredictable results and should be avoided.
--- 35,46 ----
* {@code getAsDouble()} will return the value.
*
* <p>Additional methods that depend on the presence or absence of a contained
* value are provided, such as {@link #orElse(double) orElse()}
* (return a default value if value not present) and
! * {@link #ifPresent(java.util.function.DoubleConsumer) ifPresent()} (perform an
! * action if the value is present).
*
* <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
* class; use of identity-sensitive operations (including reference equality
* ({@code ==}), identity hash code, or synchronization) on instances of
* {@code OptionalDouble} may have unpredictable results and should be avoided.
*** 129,148 ****
public boolean isPresent() {
return isPresent;
}
/**
! * Have the specified consumer accept the value if a value is present,
* otherwise do nothing.
*
! * @param consumer block to be executed if a value is present
! * @throws NullPointerException if value is present and {@code consumer} is
* null
*/
! public void ifPresent(DoubleConsumer consumer) {
if (isPresent) {
! consumer.accept(value);
}
}
/**
* If a value is present return a sequential {@link DoubleStream} containing
--- 129,167 ----
public boolean isPresent() {
return isPresent;
}
/**
! * If a value is present, perform the given action with the value,
* otherwise do nothing.
*
! * @param action the action to be performed if a value is present
! * @throws NullPointerException if a value is present and {@code action} is
* null
*/
! public void ifPresent(DoubleConsumer action) {
if (isPresent) {
! action.accept(value);
! }
! }
!
! /**
! * If a value is present, perform the given action with the value,
! * otherwise perform the given empty-based action.
! *
! * @param action the action to be performed if a value is present
! * @param emptyAction the empty-based action to be performed if a value is
! * not present
! * @throws NullPointerException if a value is present and {@code action} is
! * null, or a value is not present and {@code emptyAction} is null.
! * @since 1.9
! */
! public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
! if (isPresent) {
! action.accept(value);
! } else {
! emptyAction.run();
}
}
/**
* If a value is present return a sequential {@link DoubleStream} containing
< prev index next >