author | psandoz |
Thu, 16 Jan 2014 18:20:31 +0100 | |
changeset 22289 | bb9c71b84919 |
parent 20866 | 36155ee613ef |
permissions | -rw-r--r-- |
17182 | 1 |
/* |
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.util.stream; |
|
26 |
||
27 |
import java.util.Comparator; |
|
28 |
import java.util.Iterator; |
|
29 |
import java.util.Objects; |
|
30 |
import java.util.Optional; |
|
31 |
import java.util.Spliterator; |
|
32 |
import java.util.Spliterators; |
|
33 |
import java.util.function.BiConsumer; |
|
34 |
import java.util.function.BiFunction; |
|
35 |
import java.util.function.BinaryOperator; |
|
36 |
import java.util.function.Consumer; |
|
37 |
import java.util.function.DoubleConsumer; |
|
38 |
import java.util.function.Function; |
|
39 |
import java.util.function.IntConsumer; |
|
40 |
import java.util.function.IntFunction; |
|
41 |
import java.util.function.LongConsumer; |
|
42 |
import java.util.function.Predicate; |
|
43 |
import java.util.function.Supplier; |
|
44 |
import java.util.function.ToDoubleFunction; |
|
45 |
import java.util.function.ToIntFunction; |
|
46 |
import java.util.function.ToLongFunction; |
|
47 |
||
48 |
/** |
|
49 |
* Abstract base class for an intermediate pipeline stage or pipeline source |
|
50 |
* stage implementing whose elements are of type {@code U}. |
|
51 |
* |
|
52 |
* @param <P_IN> type of elements in the upstream source |
|
53 |
* @param <P_OUT> type of elements in produced by this stage |
|
54 |
* |
|
55 |
* @since 1.8 |
|
56 |
*/ |
|
57 |
abstract class ReferencePipeline<P_IN, P_OUT> |
|
58 |
extends AbstractPipeline<P_IN, P_OUT, Stream<P_OUT>> |
|
59 |
implements Stream<P_OUT> { |
|
60 |
||
61 |
/** |
|
62 |
* Constructor for the head of a stream pipeline. |
|
63 |
* |
|
64 |
* @param source {@code Supplier<Spliterator>} describing the stream source |
|
65 |
* @param sourceFlags the source flags for the stream source, described in |
|
66 |
* {@link StreamOpFlag} |
|
67 |
* @param parallel {@code true} if the pipeline is parallel |
|
68 |
*/ |
|
69 |
ReferencePipeline(Supplier<? extends Spliterator<?>> source, |
|
70 |
int sourceFlags, boolean parallel) { |
|
71 |
super(source, sourceFlags, parallel); |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Constructor for the head of a stream pipeline. |
|
76 |
* |
|
77 |
* @param source {@code Spliterator} describing the stream source |
|
78 |
* @param sourceFlags The source flags for the stream source, described in |
|
79 |
* {@link StreamOpFlag} |
|
80 |
* @param parallel {@code true} if the pipeline is parallel |
|
81 |
*/ |
|
82 |
ReferencePipeline(Spliterator<?> source, |
|
83 |
int sourceFlags, boolean parallel) { |
|
84 |
super(source, sourceFlags, parallel); |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Constructor for appending an intermediate operation onto an existing |
|
89 |
* pipeline. |
|
90 |
* |
|
91 |
* @param upstream the upstream element source. |
|
92 |
*/ |
|
93 |
ReferencePipeline(AbstractPipeline<?, P_IN, ?> upstream, int opFlags) { |
|
94 |
super(upstream, opFlags); |
|
95 |
} |
|
96 |
||
97 |
// Shape-specific methods |
|
98 |
||
99 |
@Override |
|
100 |
final StreamShape getOutputShape() { |
|
101 |
return StreamShape.REFERENCE; |
|
102 |
} |
|
103 |
||
104 |
@Override |
|
105 |
final <P_IN> Node<P_OUT> evaluateToNode(PipelineHelper<P_OUT> helper, |
|
106 |
Spliterator<P_IN> spliterator, |
|
107 |
boolean flattenTree, |
|
108 |
IntFunction<P_OUT[]> generator) { |
|
109 |
return Nodes.collect(helper, spliterator, flattenTree, generator); |
|
110 |
} |
|
111 |
||
112 |
@Override |
|
113 |
final <P_IN> Spliterator<P_OUT> wrap(PipelineHelper<P_OUT> ph, |
|
114 |
Supplier<Spliterator<P_IN>> supplier, |
|
115 |
boolean isParallel) { |
|
116 |
return new StreamSpliterators.WrappingSpliterator<>(ph, supplier, isParallel); |
|
117 |
} |
|
118 |
||
119 |
@Override |
|
120 |
final Spliterator<P_OUT> lazySpliterator(Supplier<? extends Spliterator<P_OUT>> supplier) { |
|
121 |
return new StreamSpliterators.DelegatingSpliterator<>(supplier); |
|
122 |
} |
|
123 |
||
124 |
@Override |
|
125 |
final void forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) { |
|
126 |
do { } while (!sink.cancellationRequested() && spliterator.tryAdvance(sink)); |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
130 |
final Node.Builder<P_OUT> makeNodeBuilder(long exactSizeIfKnown, IntFunction<P_OUT[]> generator) { |
|
131 |
return Nodes.builder(exactSizeIfKnown, generator); |
|
132 |
} |
|
133 |
||
134 |
||
135 |
// BaseStream |
|
136 |
||
137 |
@Override |
|
138 |
public final Iterator<P_OUT> iterator() { |
|
18155
889970e5b728
8015792: Rename Spliterators.spliteratorFromIterator to Spliterators.iterator
psandoz
parents:
17182
diff
changeset
|
139 |
return Spliterators.iterator(spliterator()); |
17182 | 140 |
} |
141 |
||
142 |
||
143 |
// Stream |
|
144 |
||
145 |
// Stateless intermediate operations from Stream |
|
146 |
||
147 |
@Override |
|
148 |
public Stream<P_OUT> unordered() { |
|
149 |
if (!isOrdered()) |
|
150 |
return this; |
|
151 |
return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE, StreamOpFlag.NOT_ORDERED) { |
|
152 |
@Override |
|
153 |
Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) { |
|
154 |
return sink; |
|
155 |
} |
|
156 |
}; |
|
157 |
} |
|
158 |
||
159 |
@Override |
|
160 |
public final Stream<P_OUT> filter(Predicate<? super P_OUT> predicate) { |
|
161 |
Objects.requireNonNull(predicate); |
|
162 |
return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE, |
|
163 |
StreamOpFlag.NOT_SIZED) { |
|
164 |
@Override |
|
165 |
Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) { |
|
19593 | 166 |
return new Sink.ChainedReference<P_OUT, P_OUT>(sink) { |
17182 | 167 |
@Override |
18528
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
168 |
public void begin(long size) { |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
169 |
downstream.begin(-1); |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
170 |
} |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
171 |
|
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
172 |
@Override |
17182 | 173 |
public void accept(P_OUT u) { |
174 |
if (predicate.test(u)) |
|
19593 | 175 |
downstream.accept(u); |
17182 | 176 |
} |
177 |
}; |
|
178 |
} |
|
179 |
}; |
|
180 |
} |
|
181 |
||
182 |
@Override |
|
19220
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
19214
diff
changeset
|
183 |
@SuppressWarnings("unchecked") |
17182 | 184 |
public final <R> Stream<R> map(Function<? super P_OUT, ? extends R> mapper) { |
185 |
Objects.requireNonNull(mapper); |
|
186 |
return new StatelessOp<P_OUT, R>(this, StreamShape.REFERENCE, |
|
187 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
188 |
@Override |
|
189 |
Sink<P_OUT> opWrapSink(int flags, Sink<R> sink) { |
|
19593 | 190 |
return new Sink.ChainedReference<P_OUT, R>(sink) { |
17182 | 191 |
@Override |
192 |
public void accept(P_OUT u) { |
|
193 |
downstream.accept(mapper.apply(u)); |
|
194 |
} |
|
195 |
}; |
|
196 |
} |
|
197 |
}; |
|
198 |
} |
|
199 |
||
200 |
@Override |
|
201 |
public final IntStream mapToInt(ToIntFunction<? super P_OUT> mapper) { |
|
202 |
Objects.requireNonNull(mapper); |
|
203 |
return new IntPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE, |
|
204 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
205 |
@Override |
|
206 |
Sink<P_OUT> opWrapSink(int flags, Sink<Integer> sink) { |
|
19593 | 207 |
return new Sink.ChainedReference<P_OUT, Integer>(sink) { |
17182 | 208 |
@Override |
209 |
public void accept(P_OUT u) { |
|
210 |
downstream.accept(mapper.applyAsInt(u)); |
|
211 |
} |
|
212 |
}; |
|
213 |
} |
|
214 |
}; |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper) { |
|
219 |
Objects.requireNonNull(mapper); |
|
220 |
return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE, |
|
221 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
222 |
@Override |
|
223 |
Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) { |
|
19593 | 224 |
return new Sink.ChainedReference<P_OUT, Long>(sink) { |
17182 | 225 |
@Override |
226 |
public void accept(P_OUT u) { |
|
227 |
downstream.accept(mapper.applyAsLong(u)); |
|
228 |
} |
|
229 |
}; |
|
230 |
} |
|
231 |
}; |
|
232 |
} |
|
233 |
||
234 |
@Override |
|
235 |
public final DoubleStream mapToDouble(ToDoubleFunction<? super P_OUT> mapper) { |
|
236 |
Objects.requireNonNull(mapper); |
|
237 |
return new DoublePipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE, |
|
238 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { |
|
239 |
@Override |
|
240 |
Sink<P_OUT> opWrapSink(int flags, Sink<Double> sink) { |
|
19593 | 241 |
return new Sink.ChainedReference<P_OUT, Double>(sink) { |
17182 | 242 |
@Override |
243 |
public void accept(P_OUT u) { |
|
244 |
downstream.accept(mapper.applyAsDouble(u)); |
|
245 |
} |
|
246 |
}; |
|
247 |
} |
|
248 |
}; |
|
249 |
} |
|
250 |
||
251 |
@Override |
|
252 |
public final <R> Stream<R> flatMap(Function<? super P_OUT, ? extends Stream<? extends R>> mapper) { |
|
253 |
Objects.requireNonNull(mapper); |
|
254 |
// We can do better than this, by polling cancellationRequested when stream is infinite |
|
255 |
return new StatelessOp<P_OUT, R>(this, StreamShape.REFERENCE, |
|
256 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { |
|
257 |
@Override |
|
258 |
Sink<P_OUT> opWrapSink(int flags, Sink<R> sink) { |
|
19593 | 259 |
return new Sink.ChainedReference<P_OUT, R>(sink) { |
18528
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
260 |
@Override |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
261 |
public void begin(long size) { |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
262 |
downstream.begin(-1); |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
263 |
} |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
264 |
|
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
265 |
@Override |
17182 | 266 |
public void accept(P_OUT u) { |
19800 | 267 |
try (Stream<? extends R> result = mapper.apply(u)) { |
268 |
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it |
|
269 |
if (result != null) |
|
270 |
result.sequential().forEach(downstream); |
|
271 |
} |
|
17182 | 272 |
} |
273 |
}; |
|
274 |
} |
|
275 |
}; |
|
276 |
} |
|
277 |
||
278 |
@Override |
|
279 |
public final IntStream flatMapToInt(Function<? super P_OUT, ? extends IntStream> mapper) { |
|
280 |
Objects.requireNonNull(mapper); |
|
281 |
// We can do better than this, by polling cancellationRequested when stream is infinite |
|
282 |
return new IntPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE, |
|
283 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { |
|
284 |
@Override |
|
285 |
Sink<P_OUT> opWrapSink(int flags, Sink<Integer> sink) { |
|
19593 | 286 |
return new Sink.ChainedReference<P_OUT, Integer>(sink) { |
17182 | 287 |
IntConsumer downstreamAsInt = downstream::accept; |
18528
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
288 |
@Override |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
289 |
public void begin(long size) { |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
290 |
downstream.begin(-1); |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
291 |
} |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
292 |
|
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
293 |
@Override |
17182 | 294 |
public void accept(P_OUT u) { |
19800 | 295 |
try (IntStream result = mapper.apply(u)) { |
296 |
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it |
|
297 |
if (result != null) |
|
298 |
result.sequential().forEach(downstreamAsInt); |
|
299 |
} |
|
17182 | 300 |
} |
301 |
}; |
|
302 |
} |
|
303 |
}; |
|
304 |
} |
|
305 |
||
306 |
@Override |
|
307 |
public final DoubleStream flatMapToDouble(Function<? super P_OUT, ? extends DoubleStream> mapper) { |
|
308 |
Objects.requireNonNull(mapper); |
|
309 |
// We can do better than this, by polling cancellationRequested when stream is infinite |
|
310 |
return new DoublePipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE, |
|
311 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { |
|
312 |
@Override |
|
313 |
Sink<P_OUT> opWrapSink(int flags, Sink<Double> sink) { |
|
19593 | 314 |
return new Sink.ChainedReference<P_OUT, Double>(sink) { |
17182 | 315 |
DoubleConsumer downstreamAsDouble = downstream::accept; |
18528
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
316 |
@Override |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
317 |
public void begin(long size) { |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
318 |
downstream.begin(-1); |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
319 |
} |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
320 |
|
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
321 |
@Override |
17182 | 322 |
public void accept(P_OUT u) { |
19800 | 323 |
try (DoubleStream result = mapper.apply(u)) { |
324 |
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it |
|
325 |
if (result != null) |
|
326 |
result.sequential().forEach(downstreamAsDouble); |
|
327 |
} |
|
17182 | 328 |
} |
329 |
}; |
|
330 |
} |
|
331 |
}; |
|
332 |
} |
|
333 |
||
334 |
@Override |
|
335 |
public final LongStream flatMapToLong(Function<? super P_OUT, ? extends LongStream> mapper) { |
|
336 |
Objects.requireNonNull(mapper); |
|
337 |
// We can do better than this, by polling cancellationRequested when stream is infinite |
|
338 |
return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE, |
|
339 |
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { |
|
340 |
@Override |
|
341 |
Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) { |
|
19593 | 342 |
return new Sink.ChainedReference<P_OUT, Long>(sink) { |
17182 | 343 |
LongConsumer downstreamAsLong = downstream::accept; |
18528
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
344 |
@Override |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
345 |
public void begin(long size) { |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
346 |
downstream.begin(-1); |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
347 |
} |
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
348 |
|
9b02581cf079
8016324: filter/flatMap pipeline sinks should pass size information to downstream sink
psandoz
parents:
18155
diff
changeset
|
349 |
@Override |
17182 | 350 |
public void accept(P_OUT u) { |
19800 | 351 |
try (LongStream result = mapper.apply(u)) { |
352 |
// We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it |
|
353 |
if (result != null) |
|
354 |
result.sequential().forEach(downstreamAsLong); |
|
355 |
} |
|
17182 | 356 |
} |
357 |
}; |
|
358 |
} |
|
359 |
}; |
|
360 |
} |
|
361 |
||
362 |
@Override |
|
19850 | 363 |
public final Stream<P_OUT> peek(Consumer<? super P_OUT> action) { |
364 |
Objects.requireNonNull(action); |
|
17182 | 365 |
return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE, |
366 |
0) { |
|
367 |
@Override |
|
368 |
Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) { |
|
19593 | 369 |
return new Sink.ChainedReference<P_OUT, P_OUT>(sink) { |
17182 | 370 |
@Override |
371 |
public void accept(P_OUT u) { |
|
19850 | 372 |
action.accept(u); |
17182 | 373 |
downstream.accept(u); |
374 |
} |
|
375 |
}; |
|
376 |
} |
|
377 |
}; |
|
378 |
} |
|
379 |
||
380 |
// Stateful intermediate operations from Stream |
|
381 |
||
382 |
@Override |
|
383 |
public final Stream<P_OUT> distinct() { |
|
384 |
return DistinctOps.makeRef(this); |
|
385 |
} |
|
386 |
||
387 |
@Override |
|
388 |
public final Stream<P_OUT> sorted() { |
|
389 |
return SortedOps.makeRef(this); |
|
390 |
} |
|
391 |
||
392 |
@Override |
|
393 |
public final Stream<P_OUT> sorted(Comparator<? super P_OUT> comparator) { |
|
394 |
return SortedOps.makeRef(this, comparator); |
|
395 |
} |
|
396 |
||
397 |
@Override |
|
398 |
public final Stream<P_OUT> limit(long maxSize) { |
|
399 |
if (maxSize < 0) |
|
400 |
throw new IllegalArgumentException(Long.toString(maxSize)); |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
401 |
return SliceOps.makeRef(this, 0, maxSize); |
17182 | 402 |
} |
403 |
||
404 |
@Override |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
405 |
public final Stream<P_OUT> skip(long n) { |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
406 |
if (n < 0) |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
407 |
throw new IllegalArgumentException(Long.toString(n)); |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
408 |
if (n == 0) |
17182 | 409 |
return this; |
410 |
else |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19859
diff
changeset
|
411 |
return SliceOps.makeRef(this, n, -1); |
17182 | 412 |
} |
413 |
||
414 |
// Terminal operations from Stream |
|
415 |
||
416 |
@Override |
|
417 |
public void forEach(Consumer<? super P_OUT> action) { |
|
418 |
evaluate(ForEachOps.makeRef(action, false)); |
|
419 |
} |
|
420 |
||
421 |
@Override |
|
422 |
public void forEachOrdered(Consumer<? super P_OUT> action) { |
|
423 |
evaluate(ForEachOps.makeRef(action, true)); |
|
424 |
} |
|
425 |
||
426 |
@Override |
|
427 |
@SuppressWarnings("unchecked") |
|
428 |
public final <A> A[] toArray(IntFunction<A[]> generator) { |
|
429 |
// Since A has no relation to U (not possible to declare that A is an upper bound of U) |
|
430 |
// there will be no static type checking. |
|
431 |
// Therefore use a raw type and assume A == U rather than propagating the separation of A and U |
|
432 |
// throughout the code-base. |
|
433 |
// The runtime type of U is never checked for equality with the component type of the runtime type of A[]. |
|
434 |
// Runtime checking will be performed when an element is stored in A[], thus if A is not a |
|
435 |
// super type of U an ArrayStoreException will be thrown. |
|
19220
d3d40ccb544e
8022476: cleanup some raw types and unchecked warnings in java.util.stream
mduigou
parents:
19214
diff
changeset
|
436 |
@SuppressWarnings("rawtypes") |
17182 | 437 |
IntFunction rawGenerator = (IntFunction) generator; |
438 |
return (A[]) Nodes.flatten(evaluateToArrayNode(rawGenerator), rawGenerator) |
|
439 |
.asArray(rawGenerator); |
|
440 |
} |
|
441 |
||
442 |
@Override |
|
443 |
public final Object[] toArray() { |
|
444 |
return toArray(Object[]::new); |
|
445 |
} |
|
446 |
||
447 |
@Override |
|
448 |
public final boolean anyMatch(Predicate<? super P_OUT> predicate) { |
|
449 |
return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.ANY)); |
|
450 |
} |
|
451 |
||
452 |
@Override |
|
453 |
public final boolean allMatch(Predicate<? super P_OUT> predicate) { |
|
454 |
return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.ALL)); |
|
455 |
} |
|
456 |
||
457 |
@Override |
|
458 |
public final boolean noneMatch(Predicate<? super P_OUT> predicate) { |
|
459 |
return evaluate(MatchOps.makeRef(predicate, MatchOps.MatchKind.NONE)); |
|
460 |
} |
|
461 |
||
462 |
@Override |
|
463 |
public final Optional<P_OUT> findFirst() { |
|
464 |
return evaluate(FindOps.makeRef(true)); |
|
465 |
} |
|
466 |
||
467 |
@Override |
|
468 |
public final Optional<P_OUT> findAny() { |
|
469 |
return evaluate(FindOps.makeRef(false)); |
|
470 |
} |
|
471 |
||
472 |
@Override |
|
473 |
public final P_OUT reduce(final P_OUT identity, final BinaryOperator<P_OUT> accumulator) { |
|
474 |
return evaluate(ReduceOps.makeRef(identity, accumulator, accumulator)); |
|
475 |
} |
|
476 |
||
477 |
@Override |
|
478 |
public final Optional<P_OUT> reduce(BinaryOperator<P_OUT> accumulator) { |
|
479 |
return evaluate(ReduceOps.makeRef(accumulator)); |
|
480 |
} |
|
481 |
||
482 |
@Override |
|
483 |
public final <R> R reduce(R identity, BiFunction<R, ? super P_OUT, R> accumulator, BinaryOperator<R> combiner) { |
|
484 |
return evaluate(ReduceOps.makeRef(identity, accumulator, combiner)); |
|
485 |
} |
|
486 |
||
487 |
@Override |
|
19593 | 488 |
@SuppressWarnings("unchecked") |
19799
efa9ff09b024
8024178: Difference in Stream.collect(Collector) methods located in jdk8 and jdk8-lambda repos
henryjen
parents:
19593
diff
changeset
|
489 |
public final <R, A> R collect(Collector<? super P_OUT, A, R> collector) { |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
490 |
A container; |
17182 | 491 |
if (isParallel() |
492 |
&& (collector.characteristics().contains(Collector.Characteristics.CONCURRENT)) |
|
493 |
&& (!isOrdered() || collector.characteristics().contains(Collector.Characteristics.UNORDERED))) { |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
494 |
container = collector.supplier().get(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
495 |
BiConsumer<A, ? super P_OUT> accumulator = collector.accumulator(); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
496 |
forEach(u -> accumulator.accept(container, u)); |
17182 | 497 |
} |
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
498 |
else { |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
499 |
container = evaluate(ReduceOps.makeRef(collector)); |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
500 |
} |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
501 |
return collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH) |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
502 |
? (R) container |
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18571
diff
changeset
|
503 |
: collector.finisher().apply(container); |
17182 | 504 |
} |
505 |
||
506 |
@Override |
|
19850 | 507 |
public final <R> R collect(Supplier<R> supplier, |
17182 | 508 |
BiConsumer<R, ? super P_OUT> accumulator, |
509 |
BiConsumer<R, R> combiner) { |
|
19850 | 510 |
return evaluate(ReduceOps.makeRef(supplier, accumulator, combiner)); |
17182 | 511 |
} |
512 |
||
513 |
@Override |
|
514 |
public final Optional<P_OUT> max(Comparator<? super P_OUT> comparator) { |
|
18571 | 515 |
return reduce(BinaryOperator.maxBy(comparator)); |
17182 | 516 |
} |
517 |
||
518 |
@Override |
|
519 |
public final Optional<P_OUT> min(Comparator<? super P_OUT> comparator) { |
|
18571 | 520 |
return reduce(BinaryOperator.minBy(comparator)); |
17182 | 521 |
|
522 |
} |
|
523 |
||
524 |
@Override |
|
525 |
public final long count() { |
|
526 |
return mapToLong(e -> 1L).sum(); |
|
527 |
} |
|
528 |
||
529 |
||
530 |
// |
|
531 |
||
532 |
/** |
|
533 |
* Source stage of a ReferencePipeline. |
|
534 |
* |
|
535 |
* @param <E_IN> type of elements in the upstream source |
|
536 |
* @param <E_OUT> type of elements in produced by this stage |
|
537 |
* @since 1.8 |
|
538 |
*/ |
|
539 |
static class Head<E_IN, E_OUT> extends ReferencePipeline<E_IN, E_OUT> { |
|
540 |
/** |
|
541 |
* Constructor for the source stage of a Stream. |
|
542 |
* |
|
543 |
* @param source {@code Supplier<Spliterator>} describing the stream |
|
544 |
* source |
|
545 |
* @param sourceFlags the source flags for the stream source, described |
|
546 |
* in {@link StreamOpFlag} |
|
547 |
*/ |
|
548 |
Head(Supplier<? extends Spliterator<?>> source, |
|
549 |
int sourceFlags, boolean parallel) { |
|
550 |
super(source, sourceFlags, parallel); |
|
551 |
} |
|
552 |
||
553 |
/** |
|
554 |
* Constructor for the source stage of a Stream. |
|
555 |
* |
|
556 |
* @param source {@code Spliterator} describing the stream source |
|
557 |
* @param sourceFlags the source flags for the stream source, described |
|
558 |
* in {@link StreamOpFlag} |
|
559 |
*/ |
|
560 |
Head(Spliterator<?> source, |
|
561 |
int sourceFlags, boolean parallel) { |
|
562 |
super(source, sourceFlags, parallel); |
|
563 |
} |
|
564 |
||
565 |
@Override |
|
566 |
final boolean opIsStateful() { |
|
567 |
throw new UnsupportedOperationException(); |
|
568 |
} |
|
569 |
||
570 |
@Override |
|
571 |
final Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink) { |
|
572 |
throw new UnsupportedOperationException(); |
|
573 |
} |
|
574 |
||
575 |
// Optimized sequential terminal operations for the head of the pipeline |
|
576 |
||
577 |
@Override |
|
578 |
public void forEach(Consumer<? super E_OUT> action) { |
|
579 |
if (!isParallel()) { |
|
580 |
sourceStageSpliterator().forEachRemaining(action); |
|
581 |
} |
|
582 |
else { |
|
583 |
super.forEach(action); |
|
584 |
} |
|
585 |
} |
|
586 |
||
587 |
@Override |
|
588 |
public void forEachOrdered(Consumer<? super E_OUT> action) { |
|
589 |
if (!isParallel()) { |
|
590 |
sourceStageSpliterator().forEachRemaining(action); |
|
591 |
} |
|
592 |
else { |
|
593 |
super.forEachOrdered(action); |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
/** |
|
599 |
* Base class for a stateless intermediate stage of a Stream. |
|
600 |
* |
|
601 |
* @param <E_IN> type of elements in the upstream source |
|
602 |
* @param <E_OUT> type of elements in produced by this stage |
|
603 |
* @since 1.8 |
|
604 |
*/ |
|
605 |
abstract static class StatelessOp<E_IN, E_OUT> |
|
606 |
extends ReferencePipeline<E_IN, E_OUT> { |
|
607 |
/** |
|
608 |
* Construct a new Stream by appending a stateless intermediate |
|
609 |
* operation to an existing stream. |
|
610 |
* |
|
611 |
* @param upstream The upstream pipeline stage |
|
612 |
* @param inputShape The stream shape for the upstream pipeline stage |
|
613 |
* @param opFlags Operation flags for the new stage |
|
614 |
*/ |
|
615 |
StatelessOp(AbstractPipeline<?, E_IN, ?> upstream, |
|
616 |
StreamShape inputShape, |
|
617 |
int opFlags) { |
|
618 |
super(upstream, opFlags); |
|
619 |
assert upstream.getOutputShape() == inputShape; |
|
620 |
} |
|
621 |
||
622 |
@Override |
|
623 |
final boolean opIsStateful() { |
|
624 |
return false; |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* Base class for a stateful intermediate stage of a Stream. |
|
630 |
* |
|
631 |
* @param <E_IN> type of elements in the upstream source |
|
632 |
* @param <E_OUT> type of elements in produced by this stage |
|
633 |
* @since 1.8 |
|
634 |
*/ |
|
635 |
abstract static class StatefulOp<E_IN, E_OUT> |
|
636 |
extends ReferencePipeline<E_IN, E_OUT> { |
|
637 |
/** |
|
638 |
* Construct a new Stream by appending a stateful intermediate operation |
|
639 |
* to an existing stream. |
|
640 |
* @param upstream The upstream pipeline stage |
|
641 |
* @param inputShape The stream shape for the upstream pipeline stage |
|
642 |
* @param opFlags Operation flags for the new stage |
|
643 |
*/ |
|
644 |
StatefulOp(AbstractPipeline<?, E_IN, ?> upstream, |
|
645 |
StreamShape inputShape, |
|
646 |
int opFlags) { |
|
647 |
super(upstream, opFlags); |
|
648 |
assert upstream.getOutputShape() == inputShape; |
|
649 |
} |
|
650 |
||
651 |
@Override |
|
652 |
final boolean opIsStateful() { |
|
653 |
return true; |
|
654 |
} |
|
655 |
||
656 |
@Override |
|
657 |
abstract <P_IN> Node<E_OUT> opEvaluateParallel(PipelineHelper<E_OUT> helper, |
|
658 |
Spliterator<P_IN> spliterator, |
|
659 |
IntFunction<E_OUT[]> generator); |
|
660 |
} |
|
661 |
} |