changeset 6870:77266b843351

Merge FindFirstTask and FindAnyTask
author briangoetz
date Thu, 27 Dec 2012 17:59:05 -0500
parents 96b2c9471b09
children 51b919152407
files src/share/classes/java/util/stream/op/FindOp.java
diffstat 1 files changed, 31 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/op/FindOp.java	Wed Dec 26 17:30:29 2012 -0500
+++ b/src/share/classes/java/util/stream/op/FindOp.java	Thu Dec 27 17:59:05 2012 -0500
@@ -34,16 +34,13 @@
 public abstract class FindOp<T, O> implements TerminalOp<T, O> {
 
     public static final class OfReference<T> extends FindOp<T, Optional<T>> {
-        private static final OfReference<?> ANY = new OfReference<>(FindKind.ANY);
-
-        private static final OfReference<?> FIRST = new OfReference<>(FindKind.FIRST);
 
         public static <T> FindOp<T, Optional<T>> any() {
-            return (FindOp<T, Optional<T>>) ANY;
+            return new OfReference<>(FindKind.ANY);
         }
 
         public static <T> FindOp<T, Optional<T>> first() {
-            return (FindOp<T, Optional<T>>) FIRST;
+            return new OfReference<>(FindKind.FIRST);
         }
 
         private OfReference(FindKind findKind) {
@@ -67,16 +64,13 @@
     }
 
     public static final class OfInt extends FindOp<Integer, OptionalInt> {
-        private static final OfInt ANY = new OfInt(FindKind.ANY);
-
-        private static final OfInt FIRST = new OfInt(FindKind.FIRST);
 
         public static FindOp<Integer, OptionalInt> any() {
-            return ANY;
+            return new OfInt(FindKind.ANY);
         }
 
         public static FindOp<Integer, OptionalInt> first() {
-            return FIRST;
+            return new OfInt(FindKind.FIRST);
         }
 
         private OfInt(FindKind findKind) {
@@ -132,63 +126,25 @@
 
     @Override
     public <P_IN> O evaluateParallel(ParallelPipelineHelper<P_IN, T> helper) {
-        return helper.invoke(findKind == FindKind.ANY
-                             ? new FindAnyTask<>(helper, this)
-                             : new FindFirstTask<>(helper, this));
+        return helper.invoke(new FindTask<>(helper, this));
     }
 
-
-    // @@@ FindFirstTask could probably extend from FindAnyTask with some
-    //     slight modifications to returning a result from a leaf node
-
-    private static class FindAnyTask<S, T, O> extends AbstractShortCircuitTask<S, T, O, FindAnyTask<S, T, O>> {
+    private static class FindTask<S, T, O> extends AbstractShortCircuitTask<S, T, O, FindTask<S, T, O>> {
         private final FindOp<T, O> op;
 
-        private FindAnyTask(ParallelPipelineHelper<S, T> helper, FindOp<T, O> op) {
+        private FindTask(ParallelPipelineHelper<S, T> helper, FindOp<T, O> op) {
             super(helper);
             this.op = op;
         }
 
-        private FindAnyTask(FindAnyTask<S, T, O> parent, Spliterator<S> spliterator) {
+        private FindTask(FindTask<S, T, O> parent, Spliterator<S> spliterator) {
             super(parent, spliterator);
             this.op = parent.op;
         }
 
         @Override
-        protected FindAnyTask<S, T, O> makeChild(Spliterator<S> spliterator) {
-            return new FindAnyTask<>(this, spliterator);
-        }
-
-        @Override
-        protected O getEmptyResult() {
-            return op.emptyResult();
-        }
-
-        @Override
-        protected O doLeaf() {
-            Iterator<T> iterator = helper.wrapAsIterator(spliterator);
-            if (iterator.hasNext())
-                shortCircuit(op.findFirst(iterator));
-            return null;
-        }
-    }
-
-    private static class FindFirstTask<S, T, O> extends AbstractShortCircuitTask<S, T, O, FindFirstTask<S, T, O>> {
-        private final FindOp<T, O> op;
-
-        private FindFirstTask(ParallelPipelineHelper<S, T> helper, FindOp<T, O> op) {
-            super(helper);
-            this.op = op;
-        }
-
-        private FindFirstTask(FindFirstTask<S, T, O> parent, Spliterator<S> spliterator) {
-            super(parent, spliterator);
-            this.op = parent.op;
-        }
-
-        @Override
-        protected FindFirstTask<S, T, O> makeChild(Spliterator<S> spliterator) {
-            return new FindFirstTask<>(this, spliterator);
+        protected FindTask<S, T, O> makeChild(Spliterator<S> spliterator) {
+            return new FindTask<>(this, spliterator);
         }
 
         @Override
@@ -206,23 +162,32 @@
         @Override
         protected O doLeaf() {
             Iterator<T> iterator = helper.wrapAsIterator(spliterator);
-            if (iterator.hasNext()) {
-                O answer = op.findFirst(iterator);
-                foundResult(answer);
-                return answer;
+            if (op.findKind == FindKind.ANY) {
+                if (iterator.hasNext())
+                    shortCircuit(op.findFirst(iterator));
+                return null;
             }
-            else
-                return null;
+            else {
+                if (iterator.hasNext()) {
+                    O answer = op.findFirst(iterator);
+                    foundResult(answer);
+                    return answer;
+                }
+                else
+                    return null;
+            }
         }
 
         @Override
         public void onCompletion(CountedCompleter<?> caller) {
-            for (FindFirstTask<S, T, O> child = children; child != null; child = child.nextSibling) {
-                O result = child.getLocalResult();
-                if (result != null && op.isPresent(result)) {
-                    setLocalResult(result);
-                    foundResult(result);
-                    break;
+            if (op.findKind == FindKind.FIRST) {
+                for (FindTask<S, T, O> child = children; child != null; child = child.nextSibling) {
+                    O result = child.getLocalResult();
+                    if (result != null && op.isPresent(result)) {
+                        setLocalResult(result);
+                        foundResult(result);
+                        break;
+                    }
                 }
             }
         }