< prev index next >

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

Print this page
rev 11381 : 8071670: java.util.Optional: please add a way to specify if-else behavior
Reviewed-by: dfuchs


  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.Consumer;
  28 import java.util.function.Function;
  29 import java.util.function.Predicate;
  30 import java.util.function.Supplier;
  31 import java.util.stream.Stream;
  32 
  33 /**
  34  * A container object which may or may not contain a non-null value.
  35  * If a value is present, {@code isPresent()} will return {@code true} and
  36  * {@code get()} will return the value.
  37  *
  38  * <p>Additional methods that depend on the presence or absence of a contained
  39  * value are provided, such as {@link #orElse(java.lang.Object) orElse()}
  40  * (return a default value if value not present) and
  41  * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (execute a block
  42  * of code if the value is present).
  43  *
  44  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  45  * class; use of identity-sensitive operations (including reference equality
  46  * ({@code ==}), identity hash code, or synchronization) on instances of
  47  * {@code Optional} may have unpredictable results and should be avoided.
  48  *
  49  * @since 1.8
  50  */
  51 public final class Optional<T> {
  52     /**
  53      * Common instance for {@code empty()}.
  54      */
  55     private static final Optional<?> EMPTY = new Optional<>();
  56 
  57     /**
  58      * If non-null, the value; if null, indicates no value is present
  59      */
  60     private final T value;
  61 
  62     /**


 131      *
 132      * @see Optional#isPresent()
 133      */
 134     public T get() {
 135         if (value == null) {
 136             throw new NoSuchElementException("No value present");
 137         }
 138         return value;
 139     }
 140 
 141     /**
 142      * Return {@code true} if there is a value present, otherwise {@code false}.
 143      *
 144      * @return {@code true} if there is a value present, otherwise {@code false}
 145      */
 146     public boolean isPresent() {
 147         return value != null;
 148     }
 149 
 150     /**
 151      * If a value is present, invoke the specified consumer with the value,
 152      * otherwise do nothing.
 153      *
 154      * @param consumer block to be executed if a value is present
 155      * @throws NullPointerException if value is present and {@code consumer} is
 156      * null
 157      */
 158     public void ifPresent(Consumer<? super T> consumer) {
 159         if (value != null) {
 160             consumer.accept(value);



















 161         }
 162     }
 163 
 164     /**
 165      * If a value is present, and the value matches the given predicate,
 166      * return an {@code Optional} describing the value, otherwise return an
 167      * empty {@code Optional}.
 168      *
 169      * @param predicate a predicate to apply to the value, if present
 170      * @return an {@code Optional} describing the value of this {@code Optional}
 171      * if a value is present and the value matches the given predicate,
 172      * otherwise an empty {@code Optional}
 173      * @throws NullPointerException if the predicate is null
 174      */
 175     public Optional<T> filter(Predicate<? super T> predicate) {
 176         Objects.requireNonNull(predicate);
 177         if (!isPresent()) {
 178             return this;
 179         } else {
 180             return predicate.test(value) ? this : empty();




  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.Consumer;
  28 import java.util.function.Function;
  29 import java.util.function.Predicate;
  30 import java.util.function.Supplier;
  31 import java.util.stream.Stream;
  32 
  33 /**
  34  * A container object which may or may not contain a non-null value.
  35  * If a value is present, {@code isPresent()} will return {@code true} and
  36  * {@code get()} will return the value.
  37  *
  38  * <p>Additional methods that depend on the presence or absence of a contained
  39  * value are provided, such as {@link #orElse(java.lang.Object) orElse()}
  40  * (return a default value if value not present) and
  41  * {@link #ifPresent(java.util.function.Consumer) ifPresent()} (perform an
  42  * action if the value is present).
  43  *
  44  * <p>This is a <a href="../lang/doc-files/ValueBased.html">value-based</a>
  45  * class; use of identity-sensitive operations (including reference equality
  46  * ({@code ==}), identity hash code, or synchronization) on instances of
  47  * {@code Optional} may have unpredictable results and should be avoided.
  48  *
  49  * @since 1.8
  50  */
  51 public final class Optional<T> {
  52     /**
  53      * Common instance for {@code empty()}.
  54      */
  55     private static final Optional<?> EMPTY = new Optional<>();
  56 
  57     /**
  58      * If non-null, the value; if null, indicates no value is present
  59      */
  60     private final T value;
  61 
  62     /**


 131      *
 132      * @see Optional#isPresent()
 133      */
 134     public T get() {
 135         if (value == null) {
 136             throw new NoSuchElementException("No value present");
 137         }
 138         return value;
 139     }
 140 
 141     /**
 142      * Return {@code true} if there is a value present, otherwise {@code false}.
 143      *
 144      * @return {@code true} if there is a value present, otherwise {@code false}
 145      */
 146     public boolean isPresent() {
 147         return value != null;
 148     }
 149 
 150     /**
 151      * If a value is present, perform the given action with the value,
 152      * otherwise do nothing.
 153      *
 154      * @param action the action to be performed if a value is present
 155      * @throws NullPointerException if a value is present and {@code action} is
 156      * null
 157      */
 158     public void ifPresent(Consumer<? super T> action) {
 159         if (value != null) {
 160             action.accept(value);
 161         }
 162     }
 163 
 164     /**
 165      * If a value is present, perform the given action with the value,
 166      * otherwise perform the given empty-based action.
 167      *
 168      * @param action the action to be performed if a value is present
 169      * @param emptyAction the empty-based action to be performed if a value is
 170      * not present
 171      * @throws NullPointerException if a value is present and {@code action} is
 172      * null, or a value is not present and {@code emptyAction} is null.
 173      * @since 1.9
 174      */
 175     public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
 176         if (value != null) {
 177             action.accept(value);
 178         } else {
 179             emptyAction.run();
 180         }
 181     }
 182 
 183     /**
 184      * If a value is present, and the value matches the given predicate,
 185      * return an {@code Optional} describing the value, otherwise return an
 186      * empty {@code Optional}.
 187      *
 188      * @param predicate a predicate to apply to the value, if present
 189      * @return an {@code Optional} describing the value of this {@code Optional}
 190      * if a value is present and the value matches the given predicate,
 191      * otherwise an empty {@code Optional}
 192      * @throws NullPointerException if the predicate is null
 193      */
 194     public Optional<T> filter(Predicate<? super T> predicate) {
 195         Objects.requireNonNull(predicate);
 196         if (!isPresent()) {
 197             return this;
 198         } else {
 199             return predicate.test(value) ? this : empty();


< prev index next >