-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Factory type alias, simplify version-dependent Buildable
- Loading branch information
Showing
5 changed files
with
138 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/scala-2.13+/org/scalacheck/util/BuildableVersionSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/scala-2.13-/org/scalacheck/util/BuildableVersionSpecific.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters