changeset 6358:b0f979b37bff

Simplify Optional implementations; add ifPresent(Block) method
author briangoetz
date Mon, 29 Oct 2012 11:04:27 -0400
parents b93e380a7834
children 90896528e6ab
files src/share/classes/java/util/Optional.java
diffstat 1 files changed, 21 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Optional.java	Mon Oct 29 10:39:46 2012 -0400
+++ b/src/share/classes/java/util/Optional.java	Mon Oct 29 11:04:27 2012 -0400
@@ -24,6 +24,7 @@
  */
 package java.util;
 
+import java.util.functions.Block;
 import java.util.functions.Factory;
 
 /**
@@ -40,27 +41,20 @@
     private final static Optional<?> EMPTY = new Optional<>();
 
     /**
-     * Value, if present.
+     * If non-null, the value; if null, incdicates no value is present
      */
     private final T value;
 
     /**
-     * Indicates if a value is present.
-     */
-    private final boolean present;
-
-    /**
      * Create a new Optional with a present value
      * @param value The value, must be non-null
      */
     public Optional(T value) {
         this.value = Objects.requireNonNull(value);
-        this.present = true;
     }
 
     private Optional() {
         this.value = null;
-        this.present = false;
     }
 
     /**
@@ -85,7 +79,7 @@
      * @throws NoSuchElementException if there is no value present.
      */
     public T get() {
-        if (!present) {
+        if (value == null) {
             throw new NoSuchElementException("No value present");
         }
         return value;
@@ -96,7 +90,15 @@
      * @return {@code true} if there is a value present otherwise {@code false}.
      */
     public boolean isPresent() {
-        return present;
+        return value != null;
+    }
+
+    /**
+     * Execute the specified block with the value if a value is present
+     */
+    public void ifPresent(Block<? super T> block) {
+        if (value != null)
+            block.apply(value);
     }
 
     /**
@@ -106,7 +108,7 @@
      * @return the value if present otherwise return {@code other}.
      */
     public T orElse(T other) {
-        return present ? value : other;
+        return value != null ? value : other;
     }
 
     /**
@@ -116,7 +118,7 @@
      * @return the value if present otherwise return result of {@code other}.
      */
     public T orElse(Factory<T> other) {
-        return present ? value : other.make();
+        return value != null ? value : other.make();
     }
 
     /**
@@ -130,7 +132,7 @@
      * @throws V if there is no value present.
      */
     public<V extends Throwable> T orElseThrow(Factory<V> exceptionFactory) throws V {
-        if (present) {
+        if (value != null) {
             return value;
         } else {
             throw exceptionFactory.make();
@@ -148,7 +150,7 @@
      * @throws V if there is no value present.
      */
     public<V extends Throwable> T orElseThrow(Class<V> exceptionClass) throws V {
-        if (present) {
+        if (value != null) {
             return value;
         } else {
             try {
@@ -161,35 +163,23 @@
 
     @Override
     public boolean equals(Object o) {
-        if (this == o) {
-            return true;
-        }
-        if (o == null || getClass() != o.getClass()) {
-            return false;
-        }
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
 
         Optional optional = (Optional) o;
 
-        if (present != optional.present) {
-            return false;
-        }
-
-        if (value != null ? !value.equals(optional.value) : optional.value != null) {
-            return false;
-        }
+        if (value != null ? !value.equals(optional.value) : optional.value != null) return false;
 
         return true;
     }
 
     @Override
     public int hashCode() {
-        int result = Objects.hashCode(value);
-        result = 31 * result + (present ? 1 : 0);
-        return result;
+        return value != null ? value.hashCode() : 0;
     }
 
     @Override
     public String toString() {
-        return present ? String.format("Optional[%s]", value) : "Optional.empty";
+        return value != null ? String.format("Optional[%s]", value) : "Optional.empty";
     }
 }