@@ -5,6 +5,7 @@ import scala.language.higherKinds
5
5
import java .util .stream ._
6
6
import scala .compat .java8 .collectionImpl ._
7
7
import scala .compat .java8 .converterImpl ._
8
+ import scala .reflect .ClassTag
8
9
9
10
/** Classes or objects implementing this trait create streams suitable for sequential use */
10
11
trait MakesSequentialStream [T , SS <: java.util.stream.BaseStream [_, SS ]] extends Any {
@@ -177,6 +178,13 @@ extends Priority1StreamConverters
177
178
with converterImpl.Priority1StepConverters
178
179
with converterImpl.Priority1AccumulatorConverters
179
180
{
181
+ private [java8] def unsafeArrayIfPossible [A ](a : collection.mutable.ArraySeq [A ])(implicit c : ClassTag [A ]): Array [A ] = {
182
+ if (a.elemTag == c)
183
+ a.array.asInstanceOf [Array [A ]]
184
+ else
185
+ a.toArray
186
+ }
187
+
180
188
implicit final class EnrichDoubleArrayWithStream (private val a : Array [Double ])
181
189
extends AnyVal with MakesSequentialStream [Double , DoubleStream ] with MakesParallelStream [Double , DoubleStream ] {
182
190
def seqStream : DoubleStream = java.util.Arrays .stream(a)
@@ -197,19 +205,19 @@ with converterImpl.Priority1AccumulatorConverters
197
205
198
206
implicit final class EnrichDoubleArraySeqWithStream (private val a : collection.mutable.ArraySeq [Double ])
199
207
extends AnyVal with MakesSequentialStream [Double , DoubleStream ] with MakesParallelStream [Double , DoubleStream ] {
200
- def seqStream : DoubleStream = java.util.Arrays .stream(a.array )
208
+ def seqStream : DoubleStream = java.util.Arrays .stream(unsafeArrayIfPossible(a) )
201
209
def parStream : DoubleStream = seqStream.parallel
202
210
}
203
211
204
212
implicit final class EnrichIntArraySeqWithStream (private val a : collection.mutable.ArraySeq [Int ])
205
213
extends AnyVal with MakesSequentialStream [Int , IntStream ] with MakesParallelStream [Int , IntStream ] {
206
- def seqStream : IntStream = java.util.Arrays .stream(a.array )
214
+ def seqStream : IntStream = java.util.Arrays .stream(unsafeArrayIfPossible(a) )
207
215
def parStream : IntStream = seqStream.parallel
208
216
}
209
217
210
218
implicit final class EnrichLongArraySeqWithStream (private val a : collection.mutable.ArraySeq [Long ])
211
219
extends AnyVal with MakesSequentialStream [Long , LongStream ] with MakesParallelStream [Long , LongStream ] {
212
- def seqStream : LongStream = java.util.Arrays .stream(a.array )
220
+ def seqStream : LongStream = java.util.Arrays .stream(unsafeArrayIfPossible(a) )
213
221
def parStream : LongStream = seqStream.parallel
214
222
}
215
223
0 commit comments