changeset 6415:ab258565c0c9

Cleanup in Optional
author briangoetz
date Thu, 15 Nov 2012 17:19:42 -0500
parents 16ebde8b1e17
children 1257aeafc7f5
files src/share/classes/java/util/Optional.java src/share/classes/java/util/streams/ops/FindAnyOp.java src/share/classes/java/util/streams/ops/FindFirstOp.java src/share/classes/java/util/streams/ops/SeedlessFoldOp.java
diffstat 4 files changed, 27 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Optional.java	Thu Nov 15 16:41:55 2012 -0500
+++ b/src/share/classes/java/util/Optional.java	Thu Nov 15 17:19:42 2012 -0500
@@ -28,9 +28,11 @@
 import java.util.function.Factory;
 
 /**
- * A return object which may or may not contain a value. If a value is present
- * then {@code isPresent()} will return {@code true}. The value can be retrieved
- * with {@code get()}.
+ * A return object which may or may not contain a non-null value. If a value is present
+ * then {@code isPresent()} will return {@code true} and {@code get()} will return successfully.
+ * Additional methods that depend on presence or absence are provided, such as {@code orElse()}
+ * (return a default value if not present) or {@code ifPresent()} (execute a block of code if
+ * the value is present.)
  *
  * @author Brian Goetz
  */
@@ -45,11 +47,7 @@
      */
     private final T value;
 
-    /**
-     * Create a new Optional with a present value
-     * @param value The value, must be non-null
-     */
-    public Optional(T value) {
+    private Optional(T value) {
         this.value = Objects.requireNonNull(value);
     }
 
@@ -63,6 +61,7 @@
      * Note: Though it may be tempting to do so, avoid testing if an object
      * is empty by comparing with {@code ==} against instances returned
      * {@code Option.empty()}. There is no guarantee that it is a singleton.
+     * Instead, use {@code isPresent()}.
      *
      * @param <T> Type of the non-existent value.
      * @return an empty object.
@@ -73,7 +72,15 @@
     }
 
     /**
-     * Returns the value of this object.
+     * Create a new Optional with a present value
+     * @param value The value, must be non-null
+     */
+    public static <T> Optional<T> of(T value) {
+        return new Optional<>(value);
+    }
+
+    /**
+     * Returns the value held by this object.
      *
      * @return the value of this object.
      * @throws NoSuchElementException if there is no value present.
@@ -163,14 +170,13 @@
 
     @Override
     public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
+        if (this == o)
+            return true;
+        else if (o == null || getClass() != o.getClass())
+            return false;
 
-        Optional optional = (Optional) o;
-
-        if (value != null ? !value.equals(optional.value) : optional.value != null) return false;
-
-        return true;
+        Optional other = (Optional) o;
+        return (value == null) ? (other.value == null) : value.equals(other.value);
     }
 
     @Override
--- a/src/share/classes/java/util/streams/ops/FindAnyOp.java	Thu Nov 15 16:41:55 2012 -0500
+++ b/src/share/classes/java/util/streams/ops/FindAnyOp.java	Thu Nov 15 17:19:42 2012 -0500
@@ -26,8 +26,6 @@
 
 import java.util.Iterator;
 import java.util.Optional;
-import java.util.concurrent.CountedCompleter;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.streams.ParallelPipelineHelper;
 import java.util.streams.PipelineHelper;
 import java.util.streams.Spliterator;
@@ -67,7 +65,7 @@
 
     // For testing purposes
     public Optional<T> evaluate(Iterator<T> iterator) {
-        return iterator.hasNext() ? new Optional<>(iterator.next()) : Optional.<T>empty();
+        return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.<T>empty();
     }
 
     private static class FindAnyTask<S, T> extends AbstractShortCircuitTask<S, T, Optional<T>, FindAnyTask<S, T>> {
@@ -93,7 +91,7 @@
         protected Optional<T> doLeaf() {
             Iterator<T> iterator = helper.wrapIterator(spliterator.iterator());
             if (iterator.hasNext())
-                shortCircuit(new Optional<>(iterator.next()));
+                shortCircuit(Optional.of(iterator.next()));
             return null;
         }
     }
--- a/src/share/classes/java/util/streams/ops/FindFirstOp.java	Thu Nov 15 16:41:55 2012 -0500
+++ b/src/share/classes/java/util/streams/ops/FindFirstOp.java	Thu Nov 15 17:19:42 2012 -0500
@@ -56,7 +56,7 @@
 
     // For testing purposes
     public Optional<T> evaluate(Iterator<T> iterator) {
-        return iterator.hasNext() ? new Optional<>(iterator.next()) : Optional.<T>empty();
+        return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.<T>empty();
     }
 
     @Override
@@ -100,7 +100,7 @@
         protected Optional<T> doLeaf() {
             Iterator<T> iterator = helper.wrapIterator(spliterator.iterator());
             if (iterator.hasNext()) {
-                Optional<T> answer = new Optional<>(iterator.next());
+                Optional<T> answer = Optional.of(iterator.next());
                 foundResult(answer);
                 return answer;
             }
--- a/src/share/classes/java/util/streams/ops/SeedlessFoldOp.java	Thu Nov 15 16:41:55 2012 -0500
+++ b/src/share/classes/java/util/streams/ops/SeedlessFoldOp.java	Thu Nov 15 17:19:42 2012 -0500
@@ -69,7 +69,7 @@
         @Override
         public Optional<T> getAndClearState() {
             try {
-                return empty ? Optional.<T>empty() : new Optional<>(state);
+                return empty ? Optional.<T>empty() : Optional.of(state);
             }
             finally {
                 state = null;