Skip to content

Commit da376f7

Browse files
committed
Compatibility with 2.13 collections
1 parent 6d64fa0 commit da376f7

File tree

18 files changed

+213
-223
lines changed

18 files changed

+213
-223
lines changed

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ jdk:
66
- oraclejdk8
77

88
scala:
9-
# no 2.13 for now in cross-build because of
10-
# https://github.com/scala/scala-java8-compat/issues/97
11-
- 2.11.12
12-
- 2.12.6
9+
- 2.13.0-M4
1310

1411
env:
1512
global:

build.sbt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import ScalaModulePlugin._
22

3-
// no 2.13 for now in cross-build because of
4-
// https://github.com/scala/scala-java8-compat/issues/97
5-
crossScalaVersions in ThisBuild := List("2.12.6", "2.11.12")
3+
crossScalaVersions in ThisBuild := List("2.13.0-M4")
64

75
val disableDocs = sys.props("nodocs") == "true"
86

src/main/java/scala/compat/java8/ScalaStreamSupport.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ScalaStreamSupport {
2929
* @return A Stream view of the collection which, by default, executes sequentially.
3030
*/
3131
public static <T> Stream<T> stream(scala.collection.IndexedSeq<T> coll) {
32-
return StreamSupport.stream(new StepsAnyIndexedSeq<T>(coll, 0, coll.length()), false);
32+
return StreamSupport.stream(new StepsAnyIndexedSeq<T>((scala.collection.IndexedSeq)coll, 0, coll.length()), false);
3333
}
3434

3535
/**
@@ -90,7 +90,7 @@ public static <T> Stream<T> stream(scala.collection.immutable.HashSet<T> coll) {
9090
* @return A Stream view of the collection which, by default, executes sequentially.
9191
*/
9292
public static <K> Stream<K> streamKeys(scala.collection.mutable.HashMap<K, ?> coll) {
93-
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
93+
Object[] tbl = CollectionInternals.getTable(coll);
9494
return StreamSupport.stream(new StepsAnyHashTableKey(tbl, 0, tbl.length), false);
9595
}
9696

@@ -103,7 +103,7 @@ public static <K> Stream<K> streamKeys(scala.collection.mutable.HashMap<K, ?> co
103103
* @return A Stream view of the collection which, by default, executes sequentially.
104104
*/
105105
public static <V> Stream<V> streamValues(scala.collection.mutable.HashMap<? super Object, V> coll) {
106-
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
106+
Object[] tbl = CollectionInternals.getTable(coll);
107107
return StreamSupport.stream(new StepsAnyDefaultHashTableValue(tbl, 0, tbl.length), false);
108108
}
109109

@@ -117,7 +117,7 @@ public static <V> Stream<V> streamValues(scala.collection.mutable.HashMap<? supe
117117
* @return A Stream view of the collection which, by default, executes sequentially.
118118
*/
119119
public static <K, V> Stream< scala.Tuple2<K, V> > stream(scala.collection.mutable.HashMap<K, V> coll) {
120-
scala.collection.mutable.HashEntry< K, scala.collection.mutable.DefaultEntry<K, V> >[] tbl =
120+
Object[] tbl =
121121
CollectionInternals.getTable(coll);
122122
return StreamSupport.stream(new StepsAnyDefaultHashTable<K, V>(tbl, 0, tbl.length), false);
123123
}
@@ -226,7 +226,7 @@ public static <T> Stream<T> stream(scala.collection.Iterable<T> coll) {
226226
* @param coll The collection to traverse
227227
* @return A Stream view of the collection which, by default, executes sequentially.
228228
*/
229-
public static <T> Stream<T> streamAccumulated(scala.collection.TraversableOnce<T> coll) {
229+
public static <T> Stream<T> streamAccumulated(scala.collection.IterableOnce<T> coll) {
230230
scala.compat.java8.collectionImpl.Accumulator<T> acc = scala.compat.java8.collectionImpl.Accumulator.from(coll);
231231
return StreamSupport.stream(acc.spliterator(), false);
232232
}
@@ -321,7 +321,7 @@ public static DoubleStream doubleStream(scala.collection.immutable.HashSet<Doubl
321321
* @return A DoubleStream view of the collection which, by default, executes sequentially.
322322
*/
323323
public static DoubleStream doubleStreamKeys(scala.collection.mutable.HashMap<Double, ?> coll) {
324-
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
324+
Object[] tbl = CollectionInternals.getTable(coll);
325325
return StreamSupport.doubleStream(new StepsDoubleHashTableKey(tbl, 0, tbl.length), false);
326326
}
327327

@@ -334,7 +334,7 @@ public static DoubleStream doubleStreamKeys(scala.collection.mutable.HashMap<Dou
334334
* @return A DoubleStream view of the collection which, by default, executes sequentially.
335335
*/
336336
public static DoubleStream doubleStreamValues(scala.collection.mutable.HashMap<? super Object, Double> coll) {
337-
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
337+
Object[] tbl = CollectionInternals.getTable(coll);
338338
return StreamSupport.doubleStream(new StepsDoubleDefaultHashTableValue(tbl, 0, tbl.length), false);
339339
}
340340

@@ -432,9 +432,9 @@ public static DoubleStream doubleStream(scala.collection.Iterable<Double> coll)
432432
* @param coll The collection to traverse
433433
* @return A Stream view of the collection which, by default, executes sequentially.
434434
*/
435-
public static DoubleStream doubleStreamAccumulated(scala.collection.TraversableOnce<Double> coll) {
435+
public static DoubleStream doubleStreamAccumulated(scala.collection.IterableOnce<Double> coll) {
436436
scala.compat.java8.collectionImpl.DoubleAccumulator acc =
437-
scala.compat.java8.collectionImpl.DoubleAccumulator.from((scala.collection.TraversableOnce)coll);
437+
scala.compat.java8.collectionImpl.DoubleAccumulator.from((scala.collection.IterableOnce)coll);
438438
return StreamSupport.doubleStream(acc.spliterator(), false);
439439
}
440440

@@ -557,7 +557,7 @@ public static IntStream intStream(scala.collection.immutable.HashSet<Integer> co
557557
* @return A IntStream view of the collection which, by default, executes sequentially.
558558
*/
559559
public static IntStream intStreamKeys(scala.collection.mutable.HashMap<Integer, ?> coll) {
560-
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
560+
Object[] tbl = CollectionInternals.getTable(coll);
561561
return StreamSupport.intStream(new StepsIntHashTableKey(tbl, 0, tbl.length), false);
562562
}
563563

@@ -570,7 +570,7 @@ public static IntStream intStreamKeys(scala.collection.mutable.HashMap<Integer,
570570
* @return A IntStream view of the collection which, by default, executes sequentially.
571571
*/
572572
public static IntStream intStreamValues(scala.collection.mutable.HashMap<? super Object, Integer> coll) {
573-
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
573+
Object[] tbl = CollectionInternals.getTable(coll);
574574
return StreamSupport.intStream(new StepsIntDefaultHashTableValue(tbl, 0, tbl.length), false);
575575
}
576576

@@ -668,9 +668,9 @@ public static IntStream intStream(scala.collection.Iterable<Integer> coll) {
668668
* @param coll The collection to traverse
669669
* @return A Stream view of the collection which, by default, executes sequentially.
670670
*/
671-
public static IntStream intStreamAccumulated(scala.collection.TraversableOnce<Integer> coll) {
671+
public static IntStream intStreamAccumulated(scala.collection.IterableOnce<Integer> coll) {
672672
scala.compat.java8.collectionImpl.IntAccumulator acc =
673-
scala.compat.java8.collectionImpl.IntAccumulator.from((scala.collection.TraversableOnce)coll);
673+
scala.compat.java8.collectionImpl.IntAccumulator.from((scala.collection.IterableOnce)coll);
674674
return StreamSupport.intStream(acc.spliterator(), false);
675675
}
676676

@@ -766,7 +766,7 @@ public static LongStream longStream(scala.collection.immutable.HashSet<Long> col
766766
* @return A LongStream view of the collection which, by default, executes sequentially.
767767
*/
768768
public static LongStream longStreamKeys(scala.collection.mutable.HashMap<Long, ?> coll) {
769-
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
769+
Object[] tbl = CollectionInternals.getTable(coll);
770770
return StreamSupport.longStream(new StepsLongHashTableKey(tbl, 0, tbl.length), false);
771771
}
772772

@@ -779,7 +779,7 @@ public static LongStream longStreamKeys(scala.collection.mutable.HashMap<Long, ?
779779
* @return A LongStream view of the collection which, by default, executes sequentially.
780780
*/
781781
public static LongStream longStreamValues(scala.collection.mutable.HashMap<? super Object, Long> coll) {
782-
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
782+
Object[] tbl = CollectionInternals.getTable(coll);
783783
return StreamSupport.longStream(new StepsLongDefaultHashTableValue(tbl, 0, tbl.length), false);
784784
}
785785

@@ -877,9 +877,9 @@ public static LongStream longStream(scala.collection.Iterable<Long> coll) {
877877
* @param coll The collection to traverse
878878
* @return A Stream view of the collection which, by default, executes sequentially.
879879
*/
880-
public static LongStream longStreamAccumulated(scala.collection.TraversableOnce<Long> coll) {
880+
public static LongStream longStreamAccumulated(scala.collection.IterableOnce<Long> coll) {
881881
scala.compat.java8.collectionImpl.LongAccumulator acc =
882-
scala.compat.java8.collectionImpl.LongAccumulator.from((scala.collection.TraversableOnce)coll);
882+
scala.compat.java8.collectionImpl.LongAccumulator.from((scala.collection.IterableOnce)coll);
883883
return StreamSupport.longStream(acc.spliterator(), false);
884884
}
885885

src/main/java/scala/compat/java8/runtime/CollectionInternals.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,32 @@
33
// No imports! All type names are fully qualified to avoid confusion!
44

55
public class CollectionInternals {
6-
public static <A> Object[] getTable(scala.collection.mutable.FlatHashTable<A> fht) { return fht.hashTableContents().table(); }
7-
public static <A, E extends scala.collection.mutable.HashEntry<A,E>> scala.collection.mutable.HashEntry<A, E>[] getTable(scala.collection.mutable.HashTable<A,E> ht) { return ht.hashTableContents().table(); }
6+
public static <A> Object[] getTable(scala.collection.mutable.HashSet<A> hs) { return hs.getTable().table(); }
7+
public static <A> Object[] getTable(scala.collection.mutable.LinkedHashSet<A> hm) { return hm.getTable().table(); }
8+
9+
public static <A, B> Object[] getTable(scala.collection.mutable.HashMap<A, B> hm) { return hm.getTable().table(); }
10+
public static <A, B> Object[] getTable(scala.collection.mutable.LinkedHashMap<A, B> hm) { return hm.getTable().table(); }
11+
12+
public static <K> K hashEntryKey(Object hashEntry) { return ((scala.collection.mutable.HashEntry<K, ?>)hashEntry).key(); }
13+
public static Object hashEntryNext(Object hashEntry) { return ((scala.collection.mutable.HashEntry<?, ?>)hashEntry).next(); }
14+
public static <V> V linkedEntryValue(Object hashEntry) { return ((scala.collection.mutable.LinkedHashMap.LinkedEntry<?, V>)hashEntry).value(); }
15+
public static <V> V defaultEntryValue(Object hashEntry) { return ((scala.collection.mutable.DefaultEntry<?, V>)hashEntry).value(); }
16+
817
public static <A> Object[] getDisplay0(scala.collection.immutable.Vector<A> v) { return v.display0(); }
918
public static <A> Object[] getDisplay1(scala.collection.immutable.Vector<A> v) { return v.display1(); }
1019
public static <A> Object[] getDisplay2(scala.collection.immutable.Vector<A> v) { return v.display2(); }
1120
public static <A> Object[] getDisplay3(scala.collection.immutable.Vector<A> v) { return v.display3(); }
1221
public static <A> Object[] getDisplay4(scala.collection.immutable.Vector<A> v) { return v.display4(); }
1322
public static <A> Object[] getDisplay5(scala.collection.immutable.Vector<A> v) { return v.display5(); }
23+
1424
public static <A> scala.Tuple2< scala.Tuple2< scala.collection.Iterator<A>, Object >, scala.collection.Iterator<A> > trieIteratorSplit(scala.collection.Iterator<A> it) {
1525
if (it instanceof scala.collection.immutable.TrieIterator) {
1626
scala.collection.immutable.TrieIterator<A> trie = (scala.collection.immutable.TrieIterator<A>)it;
1727
return trie.split();
1828
}
1929
return null;
2030
}
31+
2132
public static long[] getBitSetInternals(scala.collection.mutable.BitSet bitSet) { return bitSet.elems(); }
2233
}
2334

src/main/scala/scala/compat/java8/StreamConverters.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ trait Priority1StreamConverters extends Priority2StreamConverters {
9292
implicit class RichStream[A](stream: Stream[A]) {
9393
def accumulate = stream.collect(Accumulator.supplier[A], Accumulator.adder[A], Accumulator.merger[A])
9494

95-
def toScala[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, A, Coll[A]]): Coll[A] = {
96-
if (stream.isParallel) accumulate.to[Coll](cbf)
95+
def toScala[Coll[_]](implicit factory: collection.Factory[A, Coll[A]]): Coll[A] = {
96+
if (stream.isParallel) accumulate.to[Coll](factory)
9797
else {
98-
val b = cbf()
98+
val b = factory.newBuilder
9999
stream.forEachOrdered(new java.util.function.Consumer[A]{ def accept(a: A): Unit = { b += a } })
100100
b.result()
101101
}
@@ -264,10 +264,10 @@ with converterImpl.Priority1AccumulatorConverters
264264
implicit final class RichDoubleStream(private val stream: DoubleStream) extends AnyVal {
265265
def accumulate = stream.collect(DoubleAccumulator.supplier, DoubleAccumulator.adder, DoubleAccumulator.merger)
266266

267-
def toScala[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Double, Coll[Double]]): Coll[Double] = {
268-
if (stream.isParallel) accumulate.to[Coll](cbf)
267+
def toScala[Coll[_]](implicit factory: collection.Factory[Double, Coll[Double]]): Coll[Double] = {
268+
if (stream.isParallel) accumulate.to[Coll](factory)
269269
else {
270-
val b = cbf()
270+
val b = factory.newBuilder
271271
stream.forEachOrdered(new java.util.function.DoubleConsumer{ def accept(d: Double): Unit = { b += d } })
272272
b.result()
273273
}
@@ -277,10 +277,10 @@ with converterImpl.Priority1AccumulatorConverters
277277
implicit final class RichIntStream(private val stream: IntStream) extends AnyVal {
278278
def accumulate = stream.collect(IntAccumulator.supplier, IntAccumulator.adder, IntAccumulator.merger)
279279

280-
def toScala[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Int, Coll[Int]]): Coll[Int] = {
281-
if (stream.isParallel) accumulate.to[Coll](cbf)
280+
def toScala[Coll[_]](implicit factory: collection.Factory[Int, Coll[Int]]): Coll[Int] = {
281+
if (stream.isParallel) accumulate.to[Coll](factory)
282282
else {
283-
val b = cbf()
283+
val b = factory.newBuilder
284284
stream.forEachOrdered(new java.util.function.IntConsumer{ def accept(d: Int): Unit = { b += d } })
285285
b.result()
286286
}
@@ -290,10 +290,10 @@ with converterImpl.Priority1AccumulatorConverters
290290
implicit final class RichLongStream(private val stream: LongStream) extends AnyVal {
291291
def accumulate = stream.collect(LongAccumulator.supplier, LongAccumulator.adder, LongAccumulator.merger)
292292

293-
def toScala[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Long, Coll[Long]]): Coll[Long] = {
294-
if (stream.isParallel) accumulate.to[Coll](cbf)
293+
def toScala[Coll[_]](implicit factory: collection.Factory[Long, Coll[Long]]): Coll[Long] = {
294+
if (stream.isParallel) accumulate.to[Coll](factory)
295295
else {
296-
val b = cbf()
296+
val b = factory.newBuilder
297297
stream.forEachOrdered(new java.util.function.LongConsumer{ def accept(d: Long): Unit = { b += d } })
298298
b.result()
299299
}

src/main/scala/scala/compat/java8/collectionImpl/Accumulator.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ final class Accumulator[A] extends AccumulatorLike[A, Accumulator[A]] { self =>
177177
/** Copies the elements in this `Accumulator` to a specified collection.
178178
* Usage example: `acc.to[Vector]`
179179
*/
180-
final def to[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, A, Coll[A]]): Coll[A] = {
180+
final def to[Coll[_]](implicit factory: collection.Factory[A, Coll[A]]): Coll[A] = {
181181
if (totalSize > Int.MaxValue) throw new IllegalArgumentException("Too many elements accumulated for a Scala collection: "+totalSize.toString)
182-
val b = cbf()
182+
val b = factory.newBuilder
183183
b.sizeHint(totalSize.toInt)
184184
var h = 0
185185
var pv = 0L
@@ -217,10 +217,10 @@ object Accumulator {
217217
/** A `BiConsumer` that merges `Accumulator`s, suitable for use with `java.util.stream.Stream`'s `collect` method. */
218218
def merger[A] = new java.util.function.BiConsumer[Accumulator[A], Accumulator[A]]{ def accept(a1: Accumulator[A], a2: Accumulator[A]): Unit = { a1 drain a2 } }
219219

220-
/** Builds an `Accumulator` from any `TraversableOnce` */
221-
def from[A](source: TraversableOnce[A]) = {
220+
/** Builds an `Accumulator` from any `IterableOnce` */
221+
def from[A](source: IterableOnce[A]) = {
222222
val a = new Accumulator[A]
223-
source.foreach(a += _)
223+
source.iterator.foreach(a += _)
224224
a
225225
}
226226
}

src/main/scala/scala/compat/java8/collectionImpl/DoubleAccumulator.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ final class DoubleAccumulator extends AccumulatorLike[Double, DoubleAccumulator]
173173
* Note that the target collection is not specialized.
174174
* Usage example: `acc.to[Vector]`
175175
*/
176-
final def to[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Double, Coll[Double]]): Coll[Double] = {
176+
final def to[Coll[_]](implicit factory: collection.Factory[Double, Coll[Double]]): Coll[Double] = {
177177
if (totalSize > Int.MaxValue) throw new IllegalArgumentException("Too many elements accumulated for a Scala collection: "+totalSize.toString)
178-
val b = cbf()
178+
val b = factory.newBuilder
179179
b.sizeHint(totalSize.toInt)
180180
var h = 0
181181
var pv = 0L
@@ -214,10 +214,10 @@ object DoubleAccumulator {
214214
/** A `BiConsumer` that merges `DoubleAccumulator`s, suitable for use with `java.util.stream.DoubleStream`'s `collect` method. Suitable for `Stream[Double]` also. */
215215
def merger = new java.util.function.BiConsumer[DoubleAccumulator, DoubleAccumulator]{ def accept(a1: DoubleAccumulator, a2: DoubleAccumulator): Unit = { a1 drain a2 } }
216216

217-
/** Builds a `DoubleAccumulator` from any `Double`-valued `TraversableOnce` */
218-
def from[A](source: TraversableOnce[Double]) = {
217+
/** Builds a `DoubleAccumulator` from any `Double`-valued `IterableOnce` */
218+
def from[A](source: IterableOnce[Double]) = {
219219
val a = new DoubleAccumulator
220-
source.foreach(a += _)
220+
source.iterator.foreach(a += _)
221221
a
222222
}
223223
}

src/main/scala/scala/compat/java8/collectionImpl/IntAccumulator.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ final class IntAccumulator extends AccumulatorLike[Int, IntAccumulator] { self =
178178
* Note that the target collection is not specialized.
179179
* Usage example: `acc.to[Vector]`
180180
*/
181-
final def to[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Int, Coll[Int]]): Coll[Int] = {
181+
final def to[Coll[_]](implicit factory: collection.Factory[Int, Coll[Int]]): Coll[Int] = {
182182
if (totalSize > Int.MaxValue) throw new IllegalArgumentException("Too many elements accumulated for a Scala collection: "+totalSize.toString)
183-
val b = cbf()
183+
val b = factory.newBuilder
184184
b.sizeHint(totalSize.toInt)
185185
var h = 0
186186
var pv = 0L
@@ -221,10 +221,10 @@ object IntAccumulator {
221221
/** A `BiConsumer` that merges `IntAccumulator`s, suitable for use with `java.util.stream.IntStream`'s `collect` method. Suitable for `Stream[Int]` also. */
222222
def merger = new java.util.function.BiConsumer[IntAccumulator, IntAccumulator]{ def accept(a1: IntAccumulator, a2: IntAccumulator): Unit = { a1 drain a2 } }
223223

224-
/** Builds an `IntAccumulator` from any `Int`-valued `TraversableOnce` */
225-
def from[A](source: TraversableOnce[Int]) = {
224+
/** Builds an `IntAccumulator` from any `Int`-valued `IterableOnce` */
225+
def from[A](source: IterableOnce[Int]) = {
226226
val a = new IntAccumulator
227-
source.foreach(a += _)
227+
source.iterator.foreach(a += _)
228228
a
229229
}
230230
}

0 commit comments

Comments
 (0)