author | psandoz |
Thu, 16 Jan 2014 18:20:31 +0100 | |
changeset 22289 | bb9c71b84919 |
parent 19220 | d3d40ccb544e |
permissions | -rw-r--r-- |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
1 |
/* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
4 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
10 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
15 |
* accompanied this code). |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
16 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
20 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
23 |
* questions. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
24 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
25 |
package java.util.stream; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
26 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
27 |
import java.util.Spliterator; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
28 |
import java.util.concurrent.atomic.AtomicReference; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
29 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
30 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
31 |
* Abstract class for fork-join tasks used to implement short-circuiting |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
32 |
* stream ops, which can produce a result without processing all elements of the |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
33 |
* stream. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
34 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
35 |
* @param <P_IN> type of input elements to the pipeline |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
36 |
* @param <P_OUT> type of output elements from the pipeline |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
37 |
* @param <R> type of intermediate result, may be different from operation |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
38 |
* result type |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
39 |
* @param <K> type of child and sibling tasks |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
40 |
* @since 1.8 |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
41 |
*/ |
19218
8e7212b90b81
8022446: Fix serial warnings in java.util.stream
henryjen
parents:
18795
diff
changeset
|
42 |
@SuppressWarnings("serial") |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
43 |
abstract class AbstractShortCircuitTask<P_IN, P_OUT, R, |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
44 |
K extends AbstractShortCircuitTask<P_IN, P_OUT, R, K>> |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
45 |
extends AbstractTask<P_IN, P_OUT, R, K> { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
46 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
47 |
* The result for this computation; this is shared among all tasks and set |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
48 |
* exactly once |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
49 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
50 |
protected final AtomicReference<R> sharedResult; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
51 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
52 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
53 |
* Indicates whether this task has been canceled. Tasks may cancel other |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
54 |
* tasks in the computation under various conditions, such as in a |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
55 |
* find-first operation, a task that finds a value will cancel all tasks |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
56 |
* that are later in the encounter order. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
57 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
58 |
protected volatile boolean canceled; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
59 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
60 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
61 |
* Constructor for root tasks. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
62 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
63 |
* @param helper the {@code PipelineHelper} describing the stream pipeline |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
64 |
* up to this operation |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
65 |
* @param spliterator the {@code Spliterator} describing the source for this |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
66 |
* pipeline |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
67 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
68 |
protected AbstractShortCircuitTask(PipelineHelper<P_OUT> helper, |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
69 |
Spliterator<P_IN> spliterator) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
70 |
super(helper, spliterator); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
71 |
sharedResult = new AtomicReference<>(null); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
72 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
73 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
74 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
75 |
* Constructor for non-root nodes. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
76 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
77 |
* @param parent parent task in the computation tree |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
78 |
* @param spliterator the {@code Spliterator} for the portion of the |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
79 |
* computation tree described by this task |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
80 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
81 |
protected AbstractShortCircuitTask(K parent, |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
82 |
Spliterator<P_IN> spliterator) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
83 |
super(parent, spliterator); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
84 |
sharedResult = parent.sharedResult; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
85 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
86 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
87 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
88 |
* Returns the value indicating the computation completed with no task |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
89 |
* finding a short-circuitable result. For example, for a "find" operation, |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
90 |
* this might be null or an empty {@code Optional}. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
91 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
92 |
* @return the result to return when no task finds a result |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
93 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
94 |
protected abstract R getEmptyResult(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
95 |
|
18795
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
96 |
/** |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
97 |
* Overrides AbstractTask version to include checks for early |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
98 |
* exits while splitting or computing. |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
99 |
*/ |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
100 |
@Override |
18795
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
101 |
public void compute() { |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
102 |
Spliterator<P_IN> rs = spliterator, ls; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
103 |
long sizeEstimate = rs.estimateSize(); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
104 |
long sizeThreshold = getTargetSize(sizeEstimate); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
105 |
boolean forkRight = false; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
106 |
@SuppressWarnings("unchecked") K task = (K) this; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
107 |
AtomicReference<R> sr = sharedResult; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
108 |
R result; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
109 |
while ((result = sr.get()) == null) { |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
110 |
if (task.taskCanceled()) { |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
111 |
result = task.getEmptyResult(); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
112 |
break; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
113 |
} |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
114 |
if (sizeEstimate <= sizeThreshold || (ls = rs.trySplit()) == null) { |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
115 |
result = task.doLeaf(); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
116 |
break; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
117 |
} |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
118 |
K leftChild, rightChild, taskToFork; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
119 |
task.leftChild = leftChild = task.makeChild(ls); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
120 |
task.rightChild = rightChild = task.makeChild(rs); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
121 |
task.setPendingCount(1); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
122 |
if (forkRight) { |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
123 |
forkRight = false; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
124 |
rs = ls; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
125 |
task = leftChild; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
126 |
taskToFork = rightChild; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
127 |
} |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
128 |
else { |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
129 |
forkRight = true; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
130 |
task = rightChild; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
131 |
taskToFork = leftChild; |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
132 |
} |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
133 |
taskToFork.fork(); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
134 |
sizeEstimate = rs.estimateSize(); |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
135 |
} |
18795
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
136 |
task.setLocalResult(result); |
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
137 |
task.tryComplete(); |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
138 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
139 |
|
18795
25d68f4a1b38
8020040: Improve and generalize the F/J tasks to handle right or left-balanced trees
psandoz
parents:
17163
diff
changeset
|
140 |
|
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
141 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
142 |
* Declares that a globally valid result has been found. If another task has |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
143 |
* not already found the answer, the result is installed in |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
144 |
* {@code sharedResult}. The {@code compute()} method will check |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
145 |
* {@code sharedResult} before proceeding with computation, so this causes |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
146 |
* the computation to terminate early. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
147 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
148 |
* @param result the result found |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
149 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
150 |
protected void shortCircuit(R result) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
151 |
if (result != null) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
152 |
sharedResult.compareAndSet(null, result); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
153 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
154 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
155 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
156 |
* Sets a local result for this task. If this task is the root, set the |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
157 |
* shared result instead (if not already set). |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
158 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
159 |
* @param localResult The result to set for this task |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
160 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
161 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
162 |
protected void setLocalResult(R localResult) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
163 |
if (isRoot()) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
164 |
if (localResult != null) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
165 |
sharedResult.compareAndSet(null, localResult); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
166 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
167 |
else |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
168 |
super.setLocalResult(localResult); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
169 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
170 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
171 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
172 |
* Retrieves the local result for this task |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
173 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
174 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
175 |
public R getRawResult() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
176 |
return getLocalResult(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
177 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
178 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
179 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
180 |
* Retrieves the local result for this task. If this task is the root, |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
181 |
* retrieves the shared result instead. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
182 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
183 |
@Override |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
184 |
public R getLocalResult() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
185 |
if (isRoot()) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
186 |
R answer = sharedResult.get(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
187 |
return (answer == null) ? getEmptyResult() : answer; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
188 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
189 |
else |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
190 |
return super.getLocalResult(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
191 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
192 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
193 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
194 |
* Mark this task as canceled |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
195 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
196 |
protected void cancel() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
197 |
canceled = true; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
198 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
199 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
200 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
201 |
* Queries whether this task is canceled. A task is considered canceled if |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
202 |
* it or any of its parents have been canceled. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
203 |
* |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
204 |
* @return {@code true} if this task or any parent is canceled. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
205 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
206 |
protected boolean taskCanceled() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
207 |
boolean cancel = canceled; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
208 |
if (!cancel) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
209 |
for (K parent = getParent(); !cancel && parent != null; parent = parent.getParent()) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
210 |
cancel = parent.canceled; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
211 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
212 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
213 |
return cancel; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
214 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
215 |
|
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
216 |
/** |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
217 |
* Cancels all tasks which succeed this one in the encounter order. This |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
218 |
* includes canceling all the current task's right sibling, as well as the |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
219 |
* later right siblings of all its parents. |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
220 |
*/ |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
221 |
protected void cancelLaterNodes() { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
222 |
// Go up the tree, cancel right siblings of this node and all parents |
19220
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
19218
diff
changeset
|
223 |
for (@SuppressWarnings("unchecked") K parent = getParent(), node = (K) this; |
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
19218
diff
changeset
|
224 |
parent != null; |
17163
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
225 |
node = parent, parent = parent.getParent()) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
226 |
// If node is a left child of parent, then has a right sibling |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
227 |
if (parent.leftChild == node) { |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
228 |
K rightSibling = parent.rightChild; |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
229 |
if (!rightSibling.canceled) |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
230 |
rightSibling.cancel(); |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
231 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
232 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
233 |
} |
6a5e9b4f27d2
8008670: Initial java.util.stream putback -- internal API classes
mduigou
parents:
diff
changeset
|
234 |
} |