Skip to content

Commit

Permalink
Remove Factory type alias, simplify version-dependent Buildable
Browse files Browse the repository at this point in the history
  • Loading branch information
lrytz committed May 23, 2018
1 parent 1c79131 commit 543b405
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 92 deletions.
41 changes: 9 additions & 32 deletions src/main/scala-2.13+/org/scalacheck/ScalaVersionSpecific.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
package org.scalacheck
/*-------------------------------------------------------------------------*\
** ScalaCheck **
** Copyright (c) 2007-2018 Rickard Nilsson. All rights reserved. **
** http://www.scalacheck.org **
** **
** This software is released under the terms of the Revised BSD License. **
** There is NO WARRANTY. See the file LICENSE for the full text. **
\*------------------------------------------------------------------------ */

import java.util.ArrayList
package org.scalacheck

import scala.collection.{BitSet, Factory}
import scala.collection.mutable.Builder
import rng.Seed

private[scalacheck] object ScalaVersionSpecific {
def toLazyList[T](i: IterableOnce[T]) = LazyList.from(i)

def listFactory[T]: Factory[T, List[T]] =
new Factory[T, List[T]] with Serializable {
def fromSpecific(source: IterableOnce[T]): List[T] = List.from(source)
def newBuilder: Builder[T, List[T]] = List.newBuilder[T]
}

def bitsetFactory[T]: Factory[Int, BitSet] =
new Factory[Int, BitSet] with Serializable {
def fromSpecific(source: IterableOnce[Int]) = BitSet.fromSpecific(source)
def newBuilder: Builder[Int, BitSet] = BitSet.newBuilder
}

def mapFactory[T, U]: Factory[(T, U), Map[T, U]] =
new Factory[(T, U), Map[T, U]] with Serializable {
def fromSpecific(source: IterableOnce[(T, U)]) = Map.from(source)
def newBuilder: Builder[(T, U), Map[T, U]] = Map.newBuilder[T, U]
}
}

private[scalacheck] trait GenVersionSpecific {
Expand All @@ -51,13 +38,3 @@ private[scalacheck] trait CogenVersionSpecific {
implicit def cogenLazyList[A: Cogen]: Cogen[LazyList[A]] =
Cogen.it(_.iterator)
}

private[scalacheck] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
private val al = new ArrayList[T]
def addOne(x: T): this.type = {
al.add(x)
this
}
def clear(): Unit = al.clear()
def result(): ArrayList[T] = al
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*-------------------------------------------------------------------------*\
** ScalaCheck **
** Copyright (c) 2007-2018 Rickard Nilsson. All rights reserved. **
** http://www.scalacheck.org **
** **
** This software is released under the terms of the Revised BSD License. **
** There is NO WARRANTY. See the file LICENSE for the full text. **
\*------------------------------------------------------------------------ */

package org.scalacheck.util

import java.util.ArrayList

import collection.{Map => _, _}
import scala.collection.mutable.Builder


private[util] trait BuildableVersionSpecific {
implicit def buildableFactory[T,C](implicit f: Factory[T,C]) =
new Buildable[T,C] {
def builder = f.newBuilder
}
}

private[util] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
private val al = new ArrayList[T]
def addOne(x: T): this.type = {
al.add(x)
this
}
def clear(): Unit = al.clear()
def result(): ArrayList[T] = al
}

/**
* Factory instances implementing Serializable, so that the objects capturing those can be
* serializable too.
*/
// Named `...CanBuildFroms` for 2.12 source compatibility (`import SerializableCanBuildFroms._`)
// Can be renamed to `SerializableFactories` in a major release.
object SerializableCanBuildFroms {
implicit def listFactory[T]: Factory[T, List[T]] =
new Factory[T, List[T]] with Serializable {
def fromSpecific(source: IterableOnce[T]): List[T] = List.from(source)
def newBuilder: Builder[T, List[T]] = List.newBuilder[T]
}

implicit def bitsetFactory[T]: Factory[Int, BitSet] =
new Factory[Int, BitSet] with Serializable {
def fromSpecific(source: IterableOnce[Int]) = BitSet.fromSpecific(source)
def newBuilder: Builder[Int, BitSet] = BitSet.newBuilder
}

implicit def mapFactory[T, U]: Factory[(T, U), Map[T, U]] =
new Factory[(T, U), Map[T, U]] with Serializable {
def fromSpecific(source: IterableOnce[(T, U)]) = Map.from(source)
def newBuilder: Builder[(T, U), Map[T, U]] = Map.newBuilder[T, U]
}
}
50 changes: 11 additions & 39 deletions src/main/scala-2.13-/org/scalacheck/ScalaVersionSpecific.scala
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
package org.scalacheck
/*-------------------------------------------------------------------------*\
** ScalaCheck **
** Copyright (c) 2007-2018 Rickard Nilsson. All rights reserved. **
** http://www.scalacheck.org **
** **
** This software is released under the terms of the Revised BSD License. **
** There is NO WARRANTY. See the file LICENSE for the full text. **
\*------------------------------------------------------------------------ */

import java.util.ArrayList
package org.scalacheck

import scala.collection.generic.{CanBuildFrom, Sorted}
import scala.collection.generic.Sorted
import scala.collection.immutable.Stream
import scala.collection.mutable.Builder
import scala.collection.{BitSet, TraversableOnce}
import scala.collection.TraversableOnce

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

type Factory[-A, +C] = CanBuildFrom[Nothing, A, C]

def listFactory[T]: CanBuildFrom[List[T], T, List[T]] =
new CanBuildFrom[List[T], T, List[T]] with Serializable {
def apply(from: List[T]) = List.newBuilder[T]
def apply() = List.newBuilder[T]
}

def bitsetFactory[T]: CanBuildFrom[BitSet, Int, BitSet] =
new CanBuildFrom[BitSet, Int, BitSet] with Serializable {
def apply(from: BitSet) = BitSet.newBuilder
def apply() = BitSet.newBuilder
}

def mapFactory[T, U]: CanBuildFrom[Map[T, U], (T, U), Map[T, U]] =
new CanBuildFrom[Map[T, U], (T, U), Map[T, U]] with Serializable {
def apply(from: Map[T, U]) = Map.newBuilder[T, U]
def apply() = Map.newBuilder[T, U]
}

implicit class CBFExt[-A, +C](val cbf: CanBuildFrom[Nothing, A, C]) extends AnyVal {
def newBuilder: Builder[A, C] = cbf()
}

type LazyList[+A] = Stream[A]
val LazyList = Stream

Expand All @@ -55,13 +37,3 @@ private[scalacheck] trait CogenVersionSpecific
private[scalacheck] trait GenSpecificationVersionSpecific {
def infiniteLazyList[T](g: => Gen[T]): Gen[Stream[T]] = Gen.infiniteStream(g)
}

private[scalacheck] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
private val al = new ArrayList[T]
def +=(x: T): this.type = {
al.add(x)
this
}
def clear(): Unit = al.clear()
def result(): ArrayList[T] = al
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*-------------------------------------------------------------------------*\
** ScalaCheck **
** Copyright (c) 2007-2018 Rickard Nilsson. All rights reserved. **
** http://www.scalacheck.org **
** **
** This software is released under the terms of the Revised BSD License. **
** There is NO WARRANTY. See the file LICENSE for the full text. **
\*------------------------------------------------------------------------ */

package org.scalacheck.util

import java.util.ArrayList

import collection.{Map => _, _}
import generic.CanBuildFrom
import scala.collection.mutable.Builder

private[util] trait BuildableVersionSpecific {
implicit def buildableCanBuildFrom[T,F,C](implicit c: CanBuildFrom[F,T,C]) =
new Buildable[T,C] {
def builder = c.apply
}
}

private[util] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
private val al = new ArrayList[T]
def +=(x: T): this.type = {
al.add(x)
this
}
def clear(): Unit = al.clear()
def result(): ArrayList[T] = al
}

/**
* CanBuildFrom instances implementing Serializable, so that the objects capturing those can be
* serializable too.
*/
object SerializableCanBuildFroms {
implicit def listCanBuildFrom[T]: CanBuildFrom[List[T], T, List[T]] =
new CanBuildFrom[List[T], T, List[T]] with Serializable {
def apply(from: List[T]) = List.newBuilder[T]
def apply() = List.newBuilder[T]
}

implicit def bitsetCanBuildFrom[T]: CanBuildFrom[BitSet, Int, BitSet] =
new CanBuildFrom[BitSet, Int, BitSet] with Serializable {
def apply(from: BitSet) = BitSet.newBuilder
def apply() = BitSet.newBuilder
}

implicit def mapCanBuildFrom[T, U]: CanBuildFrom[Map[T, U], (T, U), Map[T, U]] =
new CanBuildFrom[Map[T, U], (T, U), Map[T, U]] with Serializable {
def apply(from: Map[T, U]) = Map.newBuilder[T, U]
def apply() = Map.newBuilder[T, U]
}
}
23 changes: 2 additions & 21 deletions src/main/scala/org/scalacheck/util/Buildable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
package org.scalacheck.util

import scala.collection.{mutable, Map => _, _}
import org.scalacheck.{ScalaVersionSpecific, ArrayListBuilder}
import ScalaVersionSpecific._

trait Buildable[T,C] extends Serializable {
def builder: mutable.Builder[T,C]
Expand All @@ -22,30 +20,13 @@ trait Buildable[T,C] extends Serializable {
}
}

/**
* Factory instances implementing Serializable, so that the objects capturing those can be
* serializable too.
*/
// Names are `..CanBuildFrom` for binary compatibility. Change to `..Factory` in a major release.
object SerializableCanBuildFroms {
implicit def listCanBuildFrom[T]: Factory[T, List[T]] = ScalaVersionSpecific.listFactory
implicit def bitsetCanBuildFrom[T]: Factory[Int, BitSet] = ScalaVersionSpecific.bitsetFactory
implicit def mapCanBuildFrom[T, U]: Factory[(T, U), Map[T, U]] = ScalaVersionSpecific.mapFactory
}

object Buildable {
// Name is `..CanBuildFrom` for binary compatibility. Change to `..Factory` in a major release.
implicit def buildableCanBuildFrom[T,C](implicit f: Factory[T,C]) =
new Buildable[T,C] {
def builder = f.newBuilder
}

object Buildable extends BuildableVersionSpecific {
import java.util.ArrayList
implicit def buildableArrayList[T]: Buildable[T, ArrayList[T]] = new Buildable[T,ArrayList[T]] {
def builder = new ArrayListBuilder[T]
}

}

/*
object Buildable2 {
Expand Down

0 comments on commit 543b405

Please sign in to comment.