Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
318 lines (285 loc) · 10.7 KB

File metadata and controls

318 lines (285 loc) · 10.7 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.util.stream;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Spliterator;
import java.util.concurrent.CountedCompleter;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* Factory for instances of a short-circuiting {@code TerminalOp} that searches
* for an element in a stream pipeline, and terminates when it finds one.
* Supported variants include find-first (find the first element in the
* encounter order) and find-any (find any element, may not be the first in
* encounter order.)
*
* @since 1.8
*/
final class FindOps {
private FindOps() { }
/**
* Constructs a {@code TerminalOp} for streams of objects.
*
* @param <T> the type of elements of the stream
* @param mustFindFirst whether the {@code TerminalOp} must produce the
* first element in the encounter order
* @return a {@code TerminalOp} implementing the find operation
*/
public static <T> TerminalOp<T, Optional<T>> makeRef(boolean mustFindFirst) {
return new FindOp<>(mustFindFirst, StreamShape.REFERENCE, Optional.empty(),
Optional::isPresent, FindSink.OfRef::new);
}
/**
* Constructs a {@code TerminalOp} for streams of ints.
*
* @param mustFindFirst whether the {@code TerminalOp} must produce the
* first element in the encounter order
* @return a {@code TerminalOp} implementing the find operation
*/
public static TerminalOp<Integer, OptionalInt> makeInt(boolean mustFindFirst) {
return new FindOp<>(mustFindFirst, StreamShape.INT_VALUE, OptionalInt.empty(),
OptionalInt::isPresent, FindSink.OfInt::new);
}
/**
* Constructs a {@code TerminalOp} for streams of longs.
*
* @param mustFindFirst whether the {@code TerminalOp} must produce the
* first element in the encounter order
* @return a {@code TerminalOp} implementing the find operation
*/
public static TerminalOp<Long, OptionalLong> makeLong(boolean mustFindFirst) {
return new FindOp<>(mustFindFirst, StreamShape.LONG_VALUE, OptionalLong.empty(),
OptionalLong::isPresent, FindSink.OfLong::new);
}
/**
* Constructs a {@code FindOp} for streams of doubles.
*
* @param mustFindFirst whether the {@code TerminalOp} must produce the
* first element in the encounter order
* @return a {@code TerminalOp} implementing the find operation
*/
public static TerminalOp<Double, OptionalDouble> makeDouble(boolean mustFindFirst) {
return new FindOp<>(mustFindFirst, StreamShape.DOUBLE_VALUE, OptionalDouble.empty(),
OptionalDouble::isPresent, FindSink.OfDouble::new);
}
/**
* A short-circuiting {@code TerminalOp} that searches for an element in a
* stream pipeline, and terminates when it finds one. Implements both
* find-first (find the first element in the encounter order) and find-any
* (find any element, may not be the first in encounter order.)
*
* @param <T> the output type of the stream pipeline
* @param <O> the result type of the find operation, typically an optional
* type
*/
private static final class FindOp<T, O> implements TerminalOp<T, O> {
private final StreamShape shape;
final boolean mustFindFirst;
final O emptyValue;
final Predicate<O> presentPredicate;
final Supplier<TerminalSink<T, O>> sinkSupplier;
/**
* Constructs a {@code FindOp}.
*
* @param mustFindFirst if true, must find the first element in
* encounter order, otherwise can find any element
* @param shape stream shape of elements to search
* @param emptyValue result value corresponding to "found nothing"
* @param presentPredicate {@code Predicate} on result value
* corresponding to "found something"
* @param sinkSupplier supplier for a {@code TerminalSink} implementing
* the matching functionality
*/
FindOp(boolean mustFindFirst,
StreamShape shape,
O emptyValue,
Predicate<O> presentPredicate,
Supplier<TerminalSink<T, O>> sinkSupplier) {
this.mustFindFirst = mustFindFirst;
this.shape = shape;
this.emptyValue = emptyValue;
this.presentPredicate = presentPredicate;
this.sinkSupplier = sinkSupplier;
}
@Override
public int getOpFlags() {
return StreamOpFlag.IS_SHORT_CIRCUIT | (mustFindFirst ? 0 : StreamOpFlag.NOT_ORDERED);
}
@Override
public StreamShape inputShape() {
return shape;
}
@Override
public <S> O evaluateSequential(PipelineHelper<T> helper,
Spliterator<S> spliterator) {
O result = helper.wrapAndCopyInto(sinkSupplier.get(), spliterator).get();
return result != null ? result : emptyValue;
}
@Override
public <P_IN> O evaluateParallel(PipelineHelper<T> helper,
Spliterator<P_IN> spliterator) {
return new FindTask<>(this, helper, spliterator).invoke();
}
}
/**
* Implementation of @{code TerminalSink} that implements the find
* functionality, requesting cancellation when something has been found
*
* @param <T> The type of input element
* @param <O> The result type, typically an optional type
*/
private static abstract class FindSink<T, O> implements TerminalSink<T, O> {
boolean hasValue;
T value;
FindSink() {} // Avoid creation of special accessor
@Override
public void accept(T value) {
if (!hasValue) {
hasValue = true;
this.value = value;
}
}
@Override
public boolean cancellationRequested() {
return hasValue;
}
/** Specialization of {@code FindSink} for reference streams */
static final class OfRef<T> extends FindSink<T, Optional<T>> {
@Override
public Optional<T> get() {
return hasValue ? Optional.of(value) : null;
}
}
/** Specialization of {@code FindSink} for int streams */
static final class OfInt extends FindSink<Integer, OptionalInt>
implements Sink.OfInt {
@Override
public void accept(int value) {
// Boxing is OK here, since few values will actually flow into the sink
accept((Integer) value);
}
@Override
public OptionalInt get() {
return hasValue ? OptionalInt.of(value) : null;
}
}
/** Specialization of {@code FindSink} for long streams */
static final class OfLong extends FindSink<Long, OptionalLong>
implements Sink.OfLong {
@Override
public void accept(long value) {
// Boxing is OK here, since few values will actually flow into the sink
accept((Long) value);
}
@Override
public OptionalLong get() {
return hasValue ? OptionalLong.of(value) : null;
}
}
/** Specialization of {@code FindSink} for double streams */
static final class OfDouble extends FindSink<Double, OptionalDouble>
implements Sink.OfDouble {
@Override
public void accept(double value) {
// Boxing is OK here, since few values will actually flow into the sink
accept((Double) value);
}
@Override
public OptionalDouble get() {
return hasValue ? OptionalDouble.of(value) : null;
}
}
}
/**
* {@code ForkJoinTask} implementing parallel short-circuiting search
* @param <P_IN> Input element type to the stream pipeline
* @param <P_OUT> Output element type from the stream pipeline
* @param <O> Result type from the find operation
*/
@SuppressWarnings("serial")
private static final class FindTask<P_IN, P_OUT, O>
extends AbstractShortCircuitTask<P_IN, P_OUT, O, FindTask<P_IN, P_OUT, O>> {
private final FindOp<P_OUT, O> op;
FindTask(FindOp<P_OUT, O> op,
PipelineHelper<P_OUT> helper,
Spliterator<P_IN> spliterator) {
super(helper, spliterator);
this.op = op;
}
FindTask(FindTask<P_IN, P_OUT, O> parent, Spliterator<P_IN> spliterator) {
super(parent, spliterator);
this.op = parent.op;
}
@Override
protected FindTask<P_IN, P_OUT, O> makeChild(Spliterator<P_IN> spliterator) {
return new FindTask<>(this, spliterator);
}
@Override
protected O getEmptyResult() {
return op.emptyValue;
}
private void foundResult(O answer) {
if (isLeftmostNode())
shortCircuit(answer);
else
cancelLaterNodes();
}
@Override
protected O doLeaf() {
O result = helper.wrapAndCopyInto(op.sinkSupplier.get(), spliterator).get();
if (!op.mustFindFirst) {
if (result != null)
shortCircuit(result);
return null;
}
else {
if (result != null) {
foundResult(result);
return result;
}
else
return null;
}
}
@Override
public void onCompletion(CountedCompleter<?> caller) {
if (op.mustFindFirst) {
for (FindTask<P_IN, P_OUT, O> child = leftChild, p = null; child != p;
p = child, child = rightChild) {
O result = child.getLocalResult();
if (result != null && op.presentPredicate.test(result)) {
setLocalResult(result);
foundResult(result);
break;
}
}
}
super.onCompletion(caller);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.