Skip to content

Commit 543b405

Browse files
committed
Remove Factory type alias, simplify version-dependent Buildable
1 parent 1c79131 commit 543b405

File tree

5 files changed

+138
-92
lines changed

5 files changed

+138
-92
lines changed
Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
1-
package org.scalacheck
1+
/*-------------------------------------------------------------------------*\
2+
** ScalaCheck **
3+
** Copyright (c) 2007-2018 Rickard Nilsson. All rights reserved. **
4+
** http://www.scalacheck.org **
5+
** **
6+
** This software is released under the terms of the Revised BSD License. **
7+
** There is NO WARRANTY. See the file LICENSE for the full text. **
8+
\*------------------------------------------------------------------------ */
29

3-
import java.util.ArrayList
10+
package org.scalacheck
411

5-
import scala.collection.{BitSet, Factory}
6-
import scala.collection.mutable.Builder
712
import rng.Seed
813

914
private[scalacheck] object ScalaVersionSpecific {
1015
def toLazyList[T](i: IterableOnce[T]) = LazyList.from(i)
11-
12-
def listFactory[T]: Factory[T, List[T]] =
13-
new Factory[T, List[T]] with Serializable {
14-
def fromSpecific(source: IterableOnce[T]): List[T] = List.from(source)
15-
def newBuilder: Builder[T, List[T]] = List.newBuilder[T]
16-
}
17-
18-
def bitsetFactory[T]: Factory[Int, BitSet] =
19-
new Factory[Int, BitSet] with Serializable {
20-
def fromSpecific(source: IterableOnce[Int]) = BitSet.fromSpecific(source)
21-
def newBuilder: Builder[Int, BitSet] = BitSet.newBuilder
22-
}
23-
24-
def mapFactory[T, U]: Factory[(T, U), Map[T, U]] =
25-
new Factory[(T, U), Map[T, U]] with Serializable {
26-
def fromSpecific(source: IterableOnce[(T, U)]) = Map.from(source)
27-
def newBuilder: Builder[(T, U), Map[T, U]] = Map.newBuilder[T, U]
28-
}
2916
}
3017

3118
private[scalacheck] trait GenVersionSpecific {
@@ -51,13 +38,3 @@ private[scalacheck] trait CogenVersionSpecific {
5138
implicit def cogenLazyList[A: Cogen]: Cogen[LazyList[A]] =
5239
Cogen.it(_.iterator)
5340
}
54-
55-
private[scalacheck] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
56-
private val al = new ArrayList[T]
57-
def addOne(x: T): this.type = {
58-
al.add(x)
59-
this
60-
}
61-
def clear(): Unit = al.clear()
62-
def result(): ArrayList[T] = al
63-
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*-------------------------------------------------------------------------*\
2+
** ScalaCheck **
3+
** Copyright (c) 2007-2018 Rickard Nilsson. All rights reserved. **
4+
** http://www.scalacheck.org **
5+
** **
6+
** This software is released under the terms of the Revised BSD License. **
7+
** There is NO WARRANTY. See the file LICENSE for the full text. **
8+
\*------------------------------------------------------------------------ */
9+
10+
package org.scalacheck.util
11+
12+
import java.util.ArrayList
13+
14+
import collection.{Map => _, _}
15+
import scala.collection.mutable.Builder
16+
17+
18+
private[util] trait BuildableVersionSpecific {
19+
implicit def buildableFactory[T,C](implicit f: Factory[T,C]) =
20+
new Buildable[T,C] {
21+
def builder = f.newBuilder
22+
}
23+
}
24+
25+
private[util] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
26+
private val al = new ArrayList[T]
27+
def addOne(x: T): this.type = {
28+
al.add(x)
29+
this
30+
}
31+
def clear(): Unit = al.clear()
32+
def result(): ArrayList[T] = al
33+
}
34+
35+
/**
36+
* Factory instances implementing Serializable, so that the objects capturing those can be
37+
* serializable too.
38+
*/
39+
// Named `...CanBuildFroms` for 2.12 source compatibility (`import SerializableCanBuildFroms._`)
40+
// Can be renamed to `SerializableFactories` in a major release.
41+
object SerializableCanBuildFroms {
42+
implicit def listFactory[T]: Factory[T, List[T]] =
43+
new Factory[T, List[T]] with Serializable {
44+
def fromSpecific(source: IterableOnce[T]): List[T] = List.from(source)
45+
def newBuilder: Builder[T, List[T]] = List.newBuilder[T]
46+
}
47+
48+
implicit def bitsetFactory[T]: Factory[Int, BitSet] =
49+
new Factory[Int, BitSet] with Serializable {
50+
def fromSpecific(source: IterableOnce[Int]) = BitSet.fromSpecific(source)
51+
def newBuilder: Builder[Int, BitSet] = BitSet.newBuilder
52+
}
53+
54+
implicit def mapFactory[T, U]: Factory[(T, U), Map[T, U]] =
55+
new Factory[(T, U), Map[T, U]] with Serializable {
56+
def fromSpecific(source: IterableOnce[(T, U)]) = Map.from(source)
57+
def newBuilder: Builder[(T, U), Map[T, U]] = Map.newBuilder[T, U]
58+
}
59+
}
Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
1-
package org.scalacheck
1+
/*-------------------------------------------------------------------------*\
2+
** ScalaCheck **
3+
** Copyright (c) 2007-2018 Rickard Nilsson. All rights reserved. **
4+
** http://www.scalacheck.org **
5+
** **
6+
** This software is released under the terms of the Revised BSD License. **
7+
** There is NO WARRANTY. See the file LICENSE for the full text. **
8+
\*------------------------------------------------------------------------ */
29

3-
import java.util.ArrayList
10+
package org.scalacheck
411

5-
import scala.collection.generic.{CanBuildFrom, Sorted}
12+
import scala.collection.generic.Sorted
613
import scala.collection.immutable.Stream
7-
import scala.collection.mutable.Builder
8-
import scala.collection.{BitSet, TraversableOnce}
14+
import scala.collection.TraversableOnce
915

1016
private[scalacheck] object ScalaVersionSpecific {
1117
def toLazyList[T](i: TraversableOnce[T]) = i.toStream
1218

13-
type Factory[-A, +C] = CanBuildFrom[Nothing, A, C]
14-
15-
def listFactory[T]: CanBuildFrom[List[T], T, List[T]] =
16-
new CanBuildFrom[List[T], T, List[T]] with Serializable {
17-
def apply(from: List[T]) = List.newBuilder[T]
18-
def apply() = List.newBuilder[T]
19-
}
20-
21-
def bitsetFactory[T]: CanBuildFrom[BitSet, Int, BitSet] =
22-
new CanBuildFrom[BitSet, Int, BitSet] with Serializable {
23-
def apply(from: BitSet) = BitSet.newBuilder
24-
def apply() = BitSet.newBuilder
25-
}
26-
27-
def mapFactory[T, U]: CanBuildFrom[Map[T, U], (T, U), Map[T, U]] =
28-
new CanBuildFrom[Map[T, U], (T, U), Map[T, U]] with Serializable {
29-
def apply(from: Map[T, U]) = Map.newBuilder[T, U]
30-
def apply() = Map.newBuilder[T, U]
31-
}
32-
33-
implicit class CBFExt[-A, +C](val cbf: CanBuildFrom[Nothing, A, C]) extends AnyVal {
34-
def newBuilder: Builder[A, C] = cbf()
35-
}
36-
3719
type LazyList[+A] = Stream[A]
3820
val LazyList = Stream
3921

@@ -55,13 +37,3 @@ private[scalacheck] trait CogenVersionSpecific
5537
private[scalacheck] trait GenSpecificationVersionSpecific {
5638
def infiniteLazyList[T](g: => Gen[T]): Gen[Stream[T]] = Gen.infiniteStream(g)
5739
}
58-
59-
private[scalacheck] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
60-
private val al = new ArrayList[T]
61-
def +=(x: T): this.type = {
62-
al.add(x)
63-
this
64-
}
65-
def clear(): Unit = al.clear()
66-
def result(): ArrayList[T] = al
67-
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*-------------------------------------------------------------------------*\
2+
** ScalaCheck **
3+
** Copyright (c) 2007-2018 Rickard Nilsson. All rights reserved. **
4+
** http://www.scalacheck.org **
5+
** **
6+
** This software is released under the terms of the Revised BSD License. **
7+
** There is NO WARRANTY. See the file LICENSE for the full text. **
8+
\*------------------------------------------------------------------------ */
9+
10+
package org.scalacheck.util
11+
12+
import java.util.ArrayList
13+
14+
import collection.{Map => _, _}
15+
import generic.CanBuildFrom
16+
import scala.collection.mutable.Builder
17+
18+
private[util] trait BuildableVersionSpecific {
19+
implicit def buildableCanBuildFrom[T,F,C](implicit c: CanBuildFrom[F,T,C]) =
20+
new Buildable[T,C] {
21+
def builder = c.apply
22+
}
23+
}
24+
25+
private[util] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
26+
private val al = new ArrayList[T]
27+
def +=(x: T): this.type = {
28+
al.add(x)
29+
this
30+
}
31+
def clear(): Unit = al.clear()
32+
def result(): ArrayList[T] = al
33+
}
34+
35+
/**
36+
* CanBuildFrom instances implementing Serializable, so that the objects capturing those can be
37+
* serializable too.
38+
*/
39+
object SerializableCanBuildFroms {
40+
implicit def listCanBuildFrom[T]: CanBuildFrom[List[T], T, List[T]] =
41+
new CanBuildFrom[List[T], T, List[T]] with Serializable {
42+
def apply(from: List[T]) = List.newBuilder[T]
43+
def apply() = List.newBuilder[T]
44+
}
45+
46+
implicit def bitsetCanBuildFrom[T]: CanBuildFrom[BitSet, Int, BitSet] =
47+
new CanBuildFrom[BitSet, Int, BitSet] with Serializable {
48+
def apply(from: BitSet) = BitSet.newBuilder
49+
def apply() = BitSet.newBuilder
50+
}
51+
52+
implicit def mapCanBuildFrom[T, U]: CanBuildFrom[Map[T, U], (T, U), Map[T, U]] =
53+
new CanBuildFrom[Map[T, U], (T, U), Map[T, U]] with Serializable {
54+
def apply(from: Map[T, U]) = Map.newBuilder[T, U]
55+
def apply() = Map.newBuilder[T, U]
56+
}
57+
}

src/main/scala/org/scalacheck/util/Buildable.scala

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
package org.scalacheck.util
1111

1212
import scala.collection.{mutable, Map => _, _}
13-
import org.scalacheck.{ScalaVersionSpecific, ArrayListBuilder}
14-
import ScalaVersionSpecific._
1513

1614
trait Buildable[T,C] extends Serializable {
1715
def builder: mutable.Builder[T,C]
@@ -22,30 +20,13 @@ trait Buildable[T,C] extends Serializable {
2220
}
2321
}
2422

25-
/**
26-
* Factory instances implementing Serializable, so that the objects capturing those can be
27-
* serializable too.
28-
*/
29-
// Names are `..CanBuildFrom` for binary compatibility. Change to `..Factory` in a major release.
30-
object SerializableCanBuildFroms {
31-
implicit def listCanBuildFrom[T]: Factory[T, List[T]] = ScalaVersionSpecific.listFactory
32-
implicit def bitsetCanBuildFrom[T]: Factory[Int, BitSet] = ScalaVersionSpecific.bitsetFactory
33-
implicit def mapCanBuildFrom[T, U]: Factory[(T, U), Map[T, U]] = ScalaVersionSpecific.mapFactory
34-
}
35-
36-
object Buildable {
37-
// Name is `..CanBuildFrom` for binary compatibility. Change to `..Factory` in a major release.
38-
implicit def buildableCanBuildFrom[T,C](implicit f: Factory[T,C]) =
39-
new Buildable[T,C] {
40-
def builder = f.newBuilder
41-
}
42-
23+
object Buildable extends BuildableVersionSpecific {
4324
import java.util.ArrayList
4425
implicit def buildableArrayList[T]: Buildable[T, ArrayList[T]] = new Buildable[T,ArrayList[T]] {
4526
def builder = new ArrayListBuilder[T]
4627
}
47-
4828
}
29+
4930
/*
5031
object Buildable2 {
5132

0 commit comments

Comments
 (0)