changeset 6649:e7bed11092a5

Merge AbstractShortCircuitTask and AbstractCancelableTask; migrate away from using CC.setRawResult
author briangoetz
date Fri, 30 Nov 2012 10:08:46 -0500
parents 5b10db63e38d
children b23bf7b019ba
files src/share/classes/java/util/stream/op/AbstractTask.java src/share/classes/java/util/stream/op/FindFirstOp.java src/share/classes/java/util/stream/op/OpUtils.java src/share/classes/java/util/stream/op/SliceOp.java src/share/classes/java/util/stream/op/TreeUtils.java src/share/classes/java/util/stream/primitive/IntTreeUtils.java
diffstat 6 files changed, 108 insertions(+), 112 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/op/AbstractTask.java	Fri Nov 30 09:53:01 2012 -0500
+++ b/src/share/classes/java/util/stream/op/AbstractTask.java	Fri Nov 30 10:08:46 2012 -0500
@@ -67,7 +67,7 @@
     /** Next sibling of this task */
     protected T nextSibling;
 
-    private R rawResult;
+    private R localResult;
 
     protected AbstractTask(ParallelPipelineHelper<P_IN, P_OUT> helper) {
         super(null);
@@ -88,20 +88,29 @@
     /** Compute the result associated with a leaf node */
     protected abstract R doLeaf();
 
-    /**
-     * Retrieve a result previously stored with <code>setRawResult</code>
-     */
     @Override
     public R getRawResult() {
-        return rawResult;
+        return localResult;
+    }
+
+    @Override
+    protected void setRawResult(R r) {
+        if (r != null)
+            throw new IllegalStateException();
     }
 
     /**
-     * Associate the result with the task, can be retrieved with <code>getRawResult</code>
+     * Retrieve a result previously stored with <code>setLocalResult</code>
      */
-    @Override
-    protected void setRawResult(R r) {
-        rawResult = r;
+    protected R getLocalResult() {
+        return localResult;
+    }
+
+    /**
+     * Associate the result with the task, can be retrieved with <code>getLocalResult</code>
+     */
+    protected void setLocalResult(R localResult) {
+        this.localResult = localResult;
     }
 
     /** Is this task a leaf node?  (Only valid after <code>compute()</code> has been called on this node).
@@ -132,14 +141,14 @@
     @Override
     public void compute() {
         if (!helper.suggestSplit(spliterator)) {
-            setRawResult(doLeaf());
+            setLocalResult(doLeaf());
             tryComplete();
         }
         else {
             int naturalSplits = spliterator.getNaturalSplits();
             if (naturalSplits == 0) {
                 // Shouldn't get here, but if we do, act like a leaf
-                setRawResult(doLeaf());
+                setLocalResult(doLeaf());
                 tryComplete();
             }
             else if (naturalSplits == 1) {
@@ -171,6 +180,89 @@
             }
         }
     }
+}
+
+abstract class AbstractShortCircuitTask<P_IN, P_OUT, R, T extends AbstractShortCircuitTask<P_IN, P_OUT, R, T>>
+        extends AbstractTask<P_IN, P_OUT, R, T> {
+    protected final AtomicReference<R> sharedResult;
+    protected volatile boolean canceled;
+
+    protected AbstractShortCircuitTask(ParallelPipelineHelper<P_IN, P_OUT> helper) {
+        super(helper);
+        sharedResult = new AtomicReference<>(null);
+    }
+
+    protected AbstractShortCircuitTask(T parent, Spliterator<P_IN> spliterator) {
+        super(parent, spliterator);
+        sharedResult = parent.sharedResult;
+    }
+
+    protected abstract R getEmptyResult();
+
+    @Override
+    public void compute() {
+        // Have we already found an answer?
+        if (sharedResult.get() != null)
+            tryComplete();
+        else if (taskCancelled()) {
+            setLocalResult(getEmptyResult());
+            tryComplete();
+        }
+        else
+            super.compute();
+    }
+
+    protected void shortCircuit(R r) {
+        if (r != null)
+            sharedResult.compareAndSet(null, r);
+    }
+
+    @Override
+    protected void setLocalResult(R r) {
+        if (isRoot()) {
+            if (r != null)
+                sharedResult.compareAndSet(null, r);
+        }
+        else
+            super.setLocalResult(r);
+    }
+
+    @Override
+    public R getRawResult() {
+        return getLocalResult();
+    }
+
+    @Override
+    public R getLocalResult() {
+        if (isRoot()) {
+            R answer = sharedResult.get();
+            return (answer == null) ? getEmptyResult() : answer;
+        }
+        else
+            return super.getLocalResult();
+    }
+
+    protected void cancel() {
+        canceled = true;
+    }
+
+    protected boolean taskCancelled() {
+        boolean cancel = canceled;
+        if (!cancel)
+            for (T parent = getParent(); !cancel && parent != null; parent = parent.getParent())
+                cancel = parent.canceled;
+        return cancel;
+    }
+
+    protected void cancelLaterNodes() {
+        T parent = getParent();
+        for (T sibling = this.nextSibling; sibling != null; sibling = sibling.nextSibling)
+            if (!sibling.canceled)
+                sibling.canceled = true;
+        // Go up the tree, cancel later siblings of all parents
+        if (parent != null)
+            parent.cancelLaterNodes();
+    }
 
     protected boolean isLeftSpine() {
         T node = (T) this;
@@ -183,99 +275,3 @@
         return true;
     }
 }
-
-abstract class AbstractCancelableTask<P_IN, P_OUT, R, T extends AbstractCancelableTask<P_IN, P_OUT, R, T>>
-        extends AbstractTask<P_IN, P_OUT, R, T> {
-    protected volatile boolean canceled;
-
-    protected AbstractCancelableTask(ParallelPipelineHelper<P_IN, P_OUT> helper) {
-        super(helper);
-    }
-
-    protected AbstractCancelableTask(T parent, Spliterator<P_IN> spliterator) {
-        super(parent, spliterator);
-    }
-
-    protected boolean taskCancelled() {
-        boolean cancel = canceled;
-        if (!cancel)
-            for (T parent = getParent(); !cancel && parent != null; parent = parent.getParent())
-                cancel = parent.canceled;
-        return cancel;
-    }
-
-    protected abstract R getEmptyResult();
-
-    @Override
-    public void compute() {
-        if (taskCancelled()) {
-            setRawResult(getEmptyResult());
-            tryComplete();
-        }
-        else
-            super.compute();
-    }
-
-    protected void cancel() {
-        canceled = true;
-    }
-
-    protected void cancelLaterNodes() {
-        T parent = getParent();
-        for (T sibling = this.nextSibling; sibling != null; sibling = sibling.nextSibling)
-            if (!sibling.canceled)
-                sibling.canceled = true;
-        // Go up the tree, cancel later siblings of all parents
-        if (parent != null)
-            parent.cancelLaterNodes();
-    }
-}
-
-abstract class AbstractShortCircuitTask<P_IN, P_OUT, R, T extends AbstractShortCircuitTask<P_IN, P_OUT, R, T>>
-        extends AbstractCancelableTask<P_IN, P_OUT, R, T> {
-    protected final AtomicReference<R> atomicAnswer;
-
-    protected AbstractShortCircuitTask(ParallelPipelineHelper<P_IN, P_OUT> helper) {
-        super(helper);
-        atomicAnswer = new AtomicReference<>(null);
-    }
-
-    protected AbstractShortCircuitTask(T parent, Spliterator<P_IN> spliterator) {
-        super(parent, spliterator);
-        atomicAnswer = parent.atomicAnswer;
-    }
-
-    @Override
-    public void compute() {
-        // Have we already found an answer?
-        if (atomicAnswer.get() != null)
-            tryComplete();
-        else
-            super.compute();
-    }
-
-    protected void shortCircuit(R r) {
-        if (r != null)
-            atomicAnswer.compareAndSet(null, r);
-    }
-
-    @Override
-    protected void setRawResult(R r) {
-        if (isRoot()) {
-            if (r != null)
-                atomicAnswer.compareAndSet(null, r);
-        }
-        else
-            super.setRawResult(r);
-    }
-
-    @Override
-    public R getRawResult() {
-        if (isRoot()) {
-            R answer = atomicAnswer.get();
-            return (answer == null) ? getEmptyResult() : answer;
-        }
-        else
-            return super.getRawResult();
-    }
-}
--- a/src/share/classes/java/util/stream/op/FindFirstOp.java	Fri Nov 30 09:53:01 2012 -0500
+++ b/src/share/classes/java/util/stream/op/FindFirstOp.java	Fri Nov 30 10:08:46 2012 -0500
@@ -113,7 +113,7 @@
             for (FindFirstTask<S, T> child = children; child != null; child = child.nextSibling) {
                 Optional<T> result = child.getRawResult();
                 if (result != null && result.isPresent()) {
-                    setRawResult(result);
+                    setLocalResult(result);
                     foundResult(result);
                     break;
                 }
--- a/src/share/classes/java/util/stream/op/OpUtils.java	Fri Nov 30 09:53:01 2012 -0500
+++ b/src/share/classes/java/util/stream/op/OpUtils.java	Fri Nov 30 10:08:46 2012 -0500
@@ -156,7 +156,7 @@
                     result.combine(otherResult);
                     otherResult.clearState();
                 }
-                setRawResult(result);
+                setLocalResult(result);
             }
         }
     }
--- a/src/share/classes/java/util/stream/op/SliceOp.java	Fri Nov 30 09:53:01 2012 -0500
+++ b/src/share/classes/java/util/stream/op/SliceOp.java	Fri Nov 30 10:08:46 2012 -0500
@@ -227,7 +227,7 @@
                         result = nodes.get(0);
                     else
                         result = Nodes.node(nodes.toArray(new Node[nodes.size()]));
-                    setRawResult(result);
+                    setLocalResult(result);
                     shortCircuit(result);
                 }
             }
--- a/src/share/classes/java/util/stream/op/TreeUtils.java	Fri Nov 30 09:53:01 2012 -0500
+++ b/src/share/classes/java/util/stream/op/TreeUtils.java	Fri Nov 30 10:08:46 2012 -0500
@@ -120,7 +120,7 @@
                 int idx = 0;
                 for (CollectorTask<T, U> cur = children; cur != null; cur = cur.nextSibling)
                     nodes[idx++] = cur.getRawResult();
-                setRawResult(Nodes.node(nodes));
+                setLocalResult(Nodes.node(nodes));
             }
         }
     }
--- a/src/share/classes/java/util/stream/primitive/IntTreeUtils.java	Fri Nov 30 09:53:01 2012 -0500
+++ b/src/share/classes/java/util/stream/primitive/IntTreeUtils.java	Fri Nov 30 10:08:46 2012 -0500
@@ -133,7 +133,7 @@
                     nodes[idx++] = cur.getRawResult();
 
                 // @@@ conc nodes
-                setRawResult(IntNodes.node(nodes));
+                setLocalResult(IntNodes.node(nodes));
             }
         }
     }