forked from scala/scala-java8-compat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScalaStreamSupport.java
384 lines (352 loc) · 17.5 KB
/
ScalaStreamSupport.java
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc. dba Akka
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.compat.java8;
import java.util.stream.*;
import scala.collection.*;
import scala.jdk.javaapi.StreamConverters;
/**
* This class contains static utility methods for creating Java Streams from Scala Collections, similar
* to the methods in {@code java.util.stream.StreamSupport} for other Java types. It is intended for
* use from Java code. In Scala code, you can use the extension methods provided by
* {@code scala.compat.java8.StreamConverters} instead.
*
* Streams created from immutable Scala collections are also immutable. Mutable collections should
* not be modified concurrently. There are no guarantees for success or failure modes of existing
* streams in case of concurrent modifications.
*/
public class ScalaStreamSupport {
/////////////////////
// Generic Streams //
/////////////////////
/**
* Generates a Stream that traverses a Scala collection.
* <p>
* Parallel processing is only efficient for collections that have a Stepper implementation
* which supports efficient splitting. For collections where this is the case, the stepper
* method has a return type marked with EfficientSplit.
*
* @param coll The IterableOnce to traverse
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <T> Stream<T> stream(IterableOnce<T> coll) {
return StreamConverters.asJavaSeqStream(coll);
}
/**
* Generates a Stream that traverses the keys of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for Maps that have a keyStepper implementation
* which supports efficient splitting. For collections where this is the case, the keyStepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <K> Stream<K> streamKeys(Map<K, ?> coll) {
return StreamSupport.stream(coll.keyStepper(StepperShape.anyStepperShape()).spliterator(), false);
}
/**
* Generates a Stream that traverses the values of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for Maps that have a valueStepper implementation
* which supports efficient splitting. For collections where this is the case, the valueStepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <V> Stream<V> streamValues(Map<?, V> coll) {
return StreamSupport.stream(coll.<AnyStepper<V>>valueStepper(StepperShape.anyStepperShape()).spliterator(), false);
}
/**
* Generates a Stream that traverses the key-value pairs of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for collections that have a Stepper implementation
* which supports efficient splitting. For collections where this is the case, the stepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <K,V> Stream< scala.Tuple2<K, V> > stream(Map<K, V> coll) {
return StreamConverters.asJavaSeqStream(coll);
}
/**
* Generates a Stream that traverses any Scala collection by accumulating its entries
* into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The collection to traverse
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <T> Stream<T> streamAccumulated(IterableOnce<T> coll) {
return StreamConverters.asJavaSeqStream(scala.jdk.AnyAccumulator.from(coll));
}
/**
* Generates a Stream that traverses the keys of any Scala map by
* accumulating those keys into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The map containing keys to traverse
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <K> Stream<K> streamAccumulatedKeys(Map<K, ?> coll) {
return StreamConverters.asJavaSeqStream(scala.jdk.AnyAccumulator.from(coll.keysIterator()));
}
/**
* Generates a Stream that traverses the values of any Scala map by
* accumulating those values into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The map containing values to traverse
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <V> Stream<V> streamAccumulatedValues(Map<?, V> coll) {
return StreamConverters.asJavaSeqStream(scala.jdk.AnyAccumulator.from(coll.valuesIterator()));
}
////////////////////
// Double Streams //
////////////////////
/**
* Generates a DoubleStream that traverses a Scala collection.
* <p>
* Parallel processing is only efficient for collections that have a Stepper implementation
* which supports efficient splitting. For collections where this is the case, the stepper
* method has a return type marked with EfficientSplit.
*
* @param coll The IterableOnce to traverse
* @return A DoubleStream view of the collection which, by default, executes sequentially.
*/
public static DoubleStream doubleStream(IterableOnce<Double> coll) {
return StreamConverters.asJavaSeqDoubleStream(coll);
}
/**
* Generates a DoubleStream that traverses the keys of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for Maps that have a keyStepper implementation
* which supports efficient splitting. For collections where this is the case, the keyStepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A DoubleStream view of the collection which, by default, executes sequentially.
*/
public static DoubleStream doubleStreamKeys(Map<Double, ?> coll) {
return StreamSupport.doubleStream(coll.keyStepper((StepperShape<Double, DoubleStepper>)(Object)StepperShape.doubleStepperShape()).spliterator(), false);
}
/**
* Generates a DoubleStream that traverses the values of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for Maps that have a valueStepper implementation
* which supports efficient splitting. For collections where this is the case, the valueStepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A DoubleStream view of the collection which, by default, executes sequentially.
*/
public static DoubleStream doubleStreamValues(Map<?, Double> coll) {
return StreamSupport.doubleStream(coll.valueStepper((StepperShape<Double, DoubleStepper>)(Object)StepperShape.doubleStepperShape()).spliterator(), false);
}
/**
* Generates a DoubleStream that traverses any Scala collection by accumulating its entries
* into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The collection to traverse
* @return A DoubleStream view of the collection which, by default, executes sequentially.
*/
public static DoubleStream doubleStreamAccumulated(IterableOnce<Double> coll) {
return StreamConverters.asJavaSeqDoubleStream((IterableOnce<Double>)(Object)scala.jdk.DoubleAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll));
}
/**
* Generates a DoubleStream that traverses the keys of any Scala map by
* accumulating those keys into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The map containing keys to traverse
* @return A DoubleStream view of the collection which, by default, executes sequentially.
*/
public static DoubleStream doubleStreamAccumulatedKeys(Map<Double, ?> coll) {
return StreamConverters.asJavaSeqDoubleStream((IterableOnce<Double>)(Object)scala.jdk.DoubleAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll.keysIterator()));
}
/**
* Generates a DoubleStream that traverses the values of any Scala map by
* accumulating those values into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The map containing values to traverse
* @return A DoubleStream view of the collection which, by default, executes sequentially.
*/
public static DoubleStream doubleStreamAccumulatedValues(Map<?, Double> coll) {
return StreamConverters.asJavaSeqDoubleStream((IterableOnce<Double>)(Object)scala.jdk.DoubleAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll.valuesIterator()));
}
/////////////////
// Int Streams //
/////////////////
/**
* Generates a IntStream that traverses a Scala collection.
* <p>
* Parallel processing is only efficient for collections that have a Stepper implementation
* which supports efficient splitting. For collections where this is the case, the stepper
* method has a return type marked with EfficientSplit.
*
* @param coll The IterableOnce to traverse
* @return A IntStream view of the collection which, by default, executes sequentially.
*/
public static IntStream intStream(IterableOnce<Integer> coll) {
return StreamConverters.asJavaSeqIntStream(coll);
}
/**
* Generates a IntStream that traverses the keys of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for Maps that have a keyStepper implementation
* which supports efficient splitting. For collections where this is the case, the keyStepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A IntStream view of the collection which, by default, executes sequentially.
*/
public static IntStream intStreamKeys(Map<Integer, ?> coll) {
return StreamSupport.intStream(coll.keyStepper((StepperShape<Integer, IntStepper>)(Object)StepperShape.intStepperShape()).spliterator(), false);
}
/**
* Generates a IntStream that traverses the values of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for Maps that have a valueStepper implementation
* which supports efficient splitting. For collections where this is the case, the valueStepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A IntStream view of the collection which, by default, executes sequentially.
*/
public static IntStream intStreamValues(Map<?, Integer> coll) {
return StreamSupport.intStream(coll.valueStepper((StepperShape<Integer, IntStepper>)(Object)StepperShape.intStepperShape()).spliterator(), false);
}
/**
* Generates a IntStream that traverses any Scala collection by accumulating its entries
* into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The collection to traverse
* @return A IntStream view of the collection which, by default, executes sequentially.
*/
public static IntStream intStreamAccumulated(IterableOnce<Integer> coll) {
return StreamConverters.asJavaSeqIntStream((IterableOnce<Integer>)(Object)scala.jdk.IntAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll));
}
/**
* Generates a IntStream that traverses the keys of any Scala map by
* accumulating those keys into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The map containing keys to traverse
* @return A IntStream view of the collection which, by default, executes sequentially.
*/
public static IntStream intStreamAccumulatedKeys(Map<Integer, ?> coll) {
return StreamConverters.asJavaSeqIntStream((IterableOnce<Integer>)(Object)scala.jdk.IntAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll.keysIterator()));
}
/**
* Generates a IntStream that traverses the values of any Scala map by
* accumulating those values into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The map containing values to traverse
* @return A IntStream view of the collection which, by default, executes sequentially.
*/
public static IntStream intStreamAccumulatedValues(Map<?, Integer> coll) {
return StreamConverters.asJavaSeqIntStream((IterableOnce<Integer>)(Object)scala.jdk.IntAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll.valuesIterator()));
}
//////////////////
// Long Streams //
//////////////////
/**
* Generates a LongStream that traverses a Scala collection.
* <p>
* Parallel processing is only efficient for collections that have a Stepper implementation
* which supports efficient splitting. For collections where this is the case, the stepper
* method has a return type marked with EfficientSplit.
*
* @param coll The IterableOnce to traverse
* @return A LongStream view of the collection which, by default, executes sequentially.
*/
public static LongStream longStream(IterableOnce<Long> coll) {
return StreamConverters.asJavaSeqLongStream(coll);
}
/**
* Generates a LongStream that traverses the keys of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for Maps that have a keyStepper implementation
* which supports efficient splitting. For collections where this is the case, the keyStepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A LongStream view of the collection which, by default, executes sequentially.
*/
public static LongStream longStreamKeys(Map<Long, ?> coll) {
return StreamSupport.longStream(coll.keyStepper((StepperShape<Long, LongStepper>)(Object)StepperShape.doubleStepperShape()).spliterator(), false);
}
/**
* Generates a LongStream that traverses the values of a scala.collection.Map.
* <p>
* Parallel processing is only efficient for Maps that have a valueStepper implementation
* which supports efficient splitting. For collections where this is the case, the valueStepper
* method has a return type marked with EfficientSplit.
*
* @param coll The Map to traverse
* @return A LongStream view of the collection which, by default, executes sequentially.
*/
public static LongStream longStreamValues(Map<?, Long> coll) {
return StreamSupport.longStream(coll.valueStepper((StepperShape<Long, LongStepper>)(Object)StepperShape.doubleStepperShape()).spliterator(), false);
}
/**
* Generates a LongStream that traverses any Scala collection by accumulating its entries
* into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The collection to traverse
* @return A LongStream view of the collection which, by default, executes sequentially.
*/
public static LongStream longStreamAccumulated(IterableOnce<Long> coll) {
return StreamConverters.asJavaSeqLongStream((IterableOnce<Long>)(Object)scala.jdk.LongAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll));
}
/**
* Generates a LongStream that traverses the keys of any Scala map by
* accumulating those keys into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The map containing keys to traverse
* @return A LongStream view of the collection which, by default, executes sequentially.
*/
public static LongStream longStreamAccumulatedKeys(Map<Long, ?> coll) {
return StreamConverters.asJavaSeqLongStream((IterableOnce<Long>)(Object)scala.jdk.LongAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll.keysIterator()));
}
/**
* Generates a LongStream that traverses the values of any Scala map by
* accumulating those values into a buffer class (Accumulator).
* <p>
* Both sequential and parallel operations will be efficient.
*
* @param coll The map containing values to traverse
* @return A LongStream view of the collection which, by default, executes sequentially.
*/
public static LongStream longStreamAccumulatedValues(Map<?, Long> coll) {
return StreamConverters.asJavaSeqLongStream((IterableOnce<Long>)(Object)scala.jdk.LongAccumulator$.MODULE$.fromSpecific((IterableOnce<Object>)(Object)coll.valuesIterator()));
}
}