Skip to content

Commit

Permalink
Compatibility with 2.13 collections
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed Jun 15, 2018
1 parent 6d64fa0 commit da376f7
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 223 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ jdk:
- oraclejdk8

scala:
# no 2.13 for now in cross-build because of
# https://github.com/scala/scala-java8-compat/issues/97
- 2.11.12
- 2.12.6
- 2.13.0-M4

env:
global:
Expand Down
4 changes: 1 addition & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import ScalaModulePlugin._

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

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

Expand Down
34 changes: 17 additions & 17 deletions src/main/java/scala/compat/java8/ScalaStreamSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ScalaStreamSupport {
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <T> Stream<T> stream(scala.collection.IndexedSeq<T> coll) {
return StreamSupport.stream(new StepsAnyIndexedSeq<T>(coll, 0, coll.length()), false);
return StreamSupport.stream(new StepsAnyIndexedSeq<T>((scala.collection.IndexedSeq)coll, 0, coll.length()), false);
}

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

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

Expand All @@ -117,7 +117,7 @@ public static <V> Stream<V> streamValues(scala.collection.mutable.HashMap<? supe
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <K, V> Stream< scala.Tuple2<K, V> > stream(scala.collection.mutable.HashMap<K, V> coll) {
scala.collection.mutable.HashEntry< K, scala.collection.mutable.DefaultEntry<K, V> >[] tbl =
Object[] tbl =
CollectionInternals.getTable(coll);
return StreamSupport.stream(new StepsAnyDefaultHashTable<K, V>(tbl, 0, tbl.length), false);
}
Expand Down Expand Up @@ -226,7 +226,7 @@ public static <T> Stream<T> stream(scala.collection.Iterable<T> coll) {
* @param coll The collection to traverse
* @return A Stream view of the collection which, by default, executes sequentially.
*/
public static <T> Stream<T> streamAccumulated(scala.collection.TraversableOnce<T> coll) {
public static <T> Stream<T> streamAccumulated(scala.collection.IterableOnce<T> coll) {
scala.compat.java8.collectionImpl.Accumulator<T> acc = scala.compat.java8.collectionImpl.Accumulator.from(coll);
return StreamSupport.stream(acc.spliterator(), false);
}
Expand Down Expand Up @@ -321,7 +321,7 @@ public static DoubleStream doubleStream(scala.collection.immutable.HashSet<Doubl
* @return A DoubleStream view of the collection which, by default, executes sequentially.
*/
public static DoubleStream doubleStreamKeys(scala.collection.mutable.HashMap<Double, ?> coll) {
scala.collection.mutable.HashEntry[] tbl = CollectionInternals.getTable(coll);
Object[] tbl = CollectionInternals.getTable(coll);
return StreamSupport.doubleStream(new StepsDoubleHashTableKey(tbl, 0, tbl.length), false);
}

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

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

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

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

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

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

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

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

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/scala/compat/java8/runtime/CollectionInternals.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
// No imports! All type names are fully qualified to avoid confusion!

public class CollectionInternals {
public static <A> Object[] getTable(scala.collection.mutable.FlatHashTable<A> fht) { return fht.hashTableContents().table(); }
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(); }
public static <A> Object[] getTable(scala.collection.mutable.HashSet<A> hs) { return hs.getTable().table(); }
public static <A> Object[] getTable(scala.collection.mutable.LinkedHashSet<A> hm) { return hm.getTable().table(); }

public static <A, B> Object[] getTable(scala.collection.mutable.HashMap<A, B> hm) { return hm.getTable().table(); }
public static <A, B> Object[] getTable(scala.collection.mutable.LinkedHashMap<A, B> hm) { return hm.getTable().table(); }

public static <K> K hashEntryKey(Object hashEntry) { return ((scala.collection.mutable.HashEntry<K, ?>)hashEntry).key(); }
public static Object hashEntryNext(Object hashEntry) { return ((scala.collection.mutable.HashEntry<?, ?>)hashEntry).next(); }
public static <V> V linkedEntryValue(Object hashEntry) { return ((scala.collection.mutable.LinkedHashMap.LinkedEntry<?, V>)hashEntry).value(); }
public static <V> V defaultEntryValue(Object hashEntry) { return ((scala.collection.mutable.DefaultEntry<?, V>)hashEntry).value(); }

public static <A> Object[] getDisplay0(scala.collection.immutable.Vector<A> v) { return v.display0(); }
public static <A> Object[] getDisplay1(scala.collection.immutable.Vector<A> v) { return v.display1(); }
public static <A> Object[] getDisplay2(scala.collection.immutable.Vector<A> v) { return v.display2(); }
public static <A> Object[] getDisplay3(scala.collection.immutable.Vector<A> v) { return v.display3(); }
public static <A> Object[] getDisplay4(scala.collection.immutable.Vector<A> v) { return v.display4(); }
public static <A> Object[] getDisplay5(scala.collection.immutable.Vector<A> v) { return v.display5(); }

public static <A> scala.Tuple2< scala.Tuple2< scala.collection.Iterator<A>, Object >, scala.collection.Iterator<A> > trieIteratorSplit(scala.collection.Iterator<A> it) {
if (it instanceof scala.collection.immutable.TrieIterator) {
scala.collection.immutable.TrieIterator<A> trie = (scala.collection.immutable.TrieIterator<A>)it;
return trie.split();
}
return null;
}

public static long[] getBitSetInternals(scala.collection.mutable.BitSet bitSet) { return bitSet.elems(); }
}

24 changes: 12 additions & 12 deletions src/main/scala/scala/compat/java8/StreamConverters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ trait Priority1StreamConverters extends Priority2StreamConverters {
implicit class RichStream[A](stream: Stream[A]) {
def accumulate = stream.collect(Accumulator.supplier[A], Accumulator.adder[A], Accumulator.merger[A])

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

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

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

def toScala[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Long, Coll[Long]]): Coll[Long] = {
if (stream.isParallel) accumulate.to[Coll](cbf)
def toScala[Coll[_]](implicit factory: collection.Factory[Long, Coll[Long]]): Coll[Long] = {
if (stream.isParallel) accumulate.to[Coll](factory)
else {
val b = cbf()
val b = factory.newBuilder
stream.forEachOrdered(new java.util.function.LongConsumer{ def accept(d: Long): Unit = { b += d } })
b.result()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ final class Accumulator[A] extends AccumulatorLike[A, Accumulator[A]] { self =>
/** Copies the elements in this `Accumulator` to a specified collection.
* Usage example: `acc.to[Vector]`
*/
final def to[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, A, Coll[A]]): Coll[A] = {
final def to[Coll[_]](implicit factory: collection.Factory[A, Coll[A]]): Coll[A] = {
if (totalSize > Int.MaxValue) throw new IllegalArgumentException("Too many elements accumulated for a Scala collection: "+totalSize.toString)
val b = cbf()
val b = factory.newBuilder
b.sizeHint(totalSize.toInt)
var h = 0
var pv = 0L
Expand Down Expand Up @@ -217,10 +217,10 @@ object Accumulator {
/** A `BiConsumer` that merges `Accumulator`s, suitable for use with `java.util.stream.Stream`'s `collect` method. */
def merger[A] = new java.util.function.BiConsumer[Accumulator[A], Accumulator[A]]{ def accept(a1: Accumulator[A], a2: Accumulator[A]): Unit = { a1 drain a2 } }

/** Builds an `Accumulator` from any `TraversableOnce` */
def from[A](source: TraversableOnce[A]) = {
/** Builds an `Accumulator` from any `IterableOnce` */
def from[A](source: IterableOnce[A]) = {
val a = new Accumulator[A]
source.foreach(a += _)
source.iterator.foreach(a += _)
a
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ final class DoubleAccumulator extends AccumulatorLike[Double, DoubleAccumulator]
* Note that the target collection is not specialized.
* Usage example: `acc.to[Vector]`
*/
final def to[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Double, Coll[Double]]): Coll[Double] = {
final def to[Coll[_]](implicit factory: collection.Factory[Double, Coll[Double]]): Coll[Double] = {
if (totalSize > Int.MaxValue) throw new IllegalArgumentException("Too many elements accumulated for a Scala collection: "+totalSize.toString)
val b = cbf()
val b = factory.newBuilder
b.sizeHint(totalSize.toInt)
var h = 0
var pv = 0L
Expand Down Expand Up @@ -214,10 +214,10 @@ object DoubleAccumulator {
/** A `BiConsumer` that merges `DoubleAccumulator`s, suitable for use with `java.util.stream.DoubleStream`'s `collect` method. Suitable for `Stream[Double]` also. */
def merger = new java.util.function.BiConsumer[DoubleAccumulator, DoubleAccumulator]{ def accept(a1: DoubleAccumulator, a2: DoubleAccumulator): Unit = { a1 drain a2 } }

/** Builds a `DoubleAccumulator` from any `Double`-valued `TraversableOnce` */
def from[A](source: TraversableOnce[Double]) = {
/** Builds a `DoubleAccumulator` from any `Double`-valued `IterableOnce` */
def from[A](source: IterableOnce[Double]) = {
val a = new DoubleAccumulator
source.foreach(a += _)
source.iterator.foreach(a += _)
a
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ final class IntAccumulator extends AccumulatorLike[Int, IntAccumulator] { self =
* Note that the target collection is not specialized.
* Usage example: `acc.to[Vector]`
*/
final def to[Coll[_]](implicit cbf: collection.generic.CanBuildFrom[Nothing, Int, Coll[Int]]): Coll[Int] = {
final def to[Coll[_]](implicit factory: collection.Factory[Int, Coll[Int]]): Coll[Int] = {
if (totalSize > Int.MaxValue) throw new IllegalArgumentException("Too many elements accumulated for a Scala collection: "+totalSize.toString)
val b = cbf()
val b = factory.newBuilder
b.sizeHint(totalSize.toInt)
var h = 0
var pv = 0L
Expand Down Expand Up @@ -221,10 +221,10 @@ object IntAccumulator {
/** A `BiConsumer` that merges `IntAccumulator`s, suitable for use with `java.util.stream.IntStream`'s `collect` method. Suitable for `Stream[Int]` also. */
def merger = new java.util.function.BiConsumer[IntAccumulator, IntAccumulator]{ def accept(a1: IntAccumulator, a2: IntAccumulator): Unit = { a1 drain a2 } }

/** Builds an `IntAccumulator` from any `Int`-valued `TraversableOnce` */
def from[A](source: TraversableOnce[Int]) = {
/** Builds an `IntAccumulator` from any `Int`-valued `IterableOnce` */
def from[A](source: IterableOnce[Int]) = {
val a = new IntAccumulator
source.foreach(a += _)
source.iterator.foreach(a += _)
a
}
}
Expand Down

0 comments on commit da376f7

Please sign in to comment.