@@ -5,6 +5,7 @@ import scala.language.higherKinds
55import java .util .stream ._
66import scala .compat .java8 .collectionImpl ._
77import scala .compat .java8 .converterImpl ._
8+ import scala .reflect .ClassTag
89
910/** Classes or objects implementing this trait create streams suitable for sequential use */
1011trait MakesSequentialStream [T , SS <: java.util.stream.BaseStream [_, SS ]] extends Any {
@@ -177,6 +178,13 @@ extends Priority1StreamConverters
177178with converterImpl.Priority1StepConverters
178179with converterImpl.Priority1AccumulatorConverters
179180{
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+
180188 implicit final class EnrichDoubleArrayWithStream (private val a : Array [Double ])
181189 extends AnyVal with MakesSequentialStream [Double , DoubleStream ] with MakesParallelStream [Double , DoubleStream ] {
182190 def seqStream : DoubleStream = java.util.Arrays .stream(a)
@@ -197,19 +205,19 @@ with converterImpl.Priority1AccumulatorConverters
197205
198206 implicit final class EnrichDoubleArraySeqWithStream (private val a : collection.mutable.ArraySeq [Double ])
199207 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) )
201209 def parStream : DoubleStream = seqStream.parallel
202210 }
203211
204212 implicit final class EnrichIntArraySeqWithStream (private val a : collection.mutable.ArraySeq [Int ])
205213 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) )
207215 def parStream : IntStream = seqStream.parallel
208216 }
209217
210218 implicit final class EnrichLongArraySeqWithStream (private val a : collection.mutable.ArraySeq [Long ])
211219 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) )
213221 def parStream : LongStream = seqStream.parallel
214222 }
215223
0 commit comments