Skip to content

Commit

Permalink
Add buildable for java.util.HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Nov 28, 2023
1 parent 256d467 commit 5bfe51a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@

package org.scalacheck.util

import java.util.ArrayList
import java.util.{ArrayList, HashMap}

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

private[util] trait BuildableVersionSpecific {
import scala.collection.JavaConverters._
import scala.language.implicitConversions

implicit def wrapArrayList[T](xs: ArrayList[T]): Traversable[T] = xs.asScala
implicit def wrapHashMap[K, V](xs: HashMap[K, V]): Traversable[(K, V)] = xs.asScala

implicit def buildableCanBuildFrom[T, F, C](implicit c: CanBuildFrom[F, T, C]): Buildable[T, C] =
new Buildable[T, C] {
def builder = c.apply
Expand All @@ -32,6 +38,18 @@ private[util] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
def result(): ArrayList[T] = al
}

private[util] class HashMapBuilder[K, V] extends Builder[(K, V), HashMap[K, V]] {
private val hm = new HashMap[K, V]

def +=(x: (K, V)): this.type = {
val (k, v) = x
hm.put(k, v)
this
}
def clear(): Unit = hm.clear()
def result(): HashMap[K, V] = hm
}

/** CanBuildFrom instances implementing Serializable, so that the objects capturing those can be serializable too.
*/
object SerializableCanBuildFroms {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@

package org.scalacheck.util

import java.util.ArrayList
import java.util.{ArrayList, HashMap}
import scala.collection.mutable.Builder
import scala.collection.{Map => _, _}

private[util] trait BuildableVersionSpecific {
import scala.jdk.CollectionConverters._

implicit def wrapArrayList[T](xs: ArrayList[T]): Iterable[T] = xs.asScala
implicit def wrapHashMap[K, V](xs: HashMap[K, V]): Iterable[(K, V)] = xs.asScala

implicit def buildableFactory[T, C](implicit f: Factory[T, C]): Buildable[T, C] =
new Buildable[T, C] {
def builder = f.newBuilder
Expand All @@ -30,6 +35,17 @@ private[util] class ArrayListBuilder[T] extends Builder[T, ArrayList[T]] {
def result(): ArrayList[T] = al
}

private[util] class HashMapBuilder[K, V] extends Builder[(K, V), HashMap[K, V]] {
private val hm = new HashMap[K, V]
def addOne(x: (K, V)): this.type = {
val (k, v) = x
hm.put(k, v)
this
}
def clear(): Unit = hm.clear()
def result(): HashMap[K, V] = hm
}

/** Factory instances implementing Serializable, so that the objects capturing those can be serializable too.
*/
// Named `...CanBuildFroms` for 2.12 source compatibility (`import SerializableCanBuildFroms._`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ trait Buildable[T, C] extends Serializable {
}

object Buildable extends BuildableVersionSpecific {
import java.util.ArrayList
import java.util.{ArrayList, HashMap}

implicit def buildableArrayList[T]: Buildable[T, ArrayList[T]] =
new Buildable[T, ArrayList[T]] {
def builder = new ArrayListBuilder[T]
}

implicit def buildableHashMap[K, V]: Buildable[(K, V), HashMap[K, V]] =
new Buildable[(K, V), HashMap[K, V]] {
def builder = new HashMapBuilder[K, V]
}

def buildableSeq[T]: Buildable[T, Seq[T]] =
new Buildable[T, Seq[T]] {
def builder: mutable.Builder[T, Seq[T]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package util

import scala.collection._

import Buildable._
import ScalaVersionSpecific._

object BuildableSpecification {
Expand Down Expand Up @@ -47,4 +48,15 @@ object BuildableSpecification {
implicit val iterableGen: Gen[immutable.Iterable[String]] = container[immutable.Iterable]

implicit val trieIteratorGen: Gen[immutable.Queue[String]] = container[immutable.Queue]

implicit val arrayListGen: Gen[java.util.ArrayList[String]] = container[java.util.ArrayList]

def buildable[C[_, _]](implicit
evb: Buildable[(String, Long), C[String, Long]],
evt: C[String, Long] => Traversable[(String, Long)]
) = Gen.buildableOf[C[String, Long], (String, Long)](for (str <- Gen.alphaStr; lng <- Gen.long) yield (str, lng))

implicit val mapGen: Gen[Map[String, Long]] = buildable[Map]

implicit val hashMapGen: Gen[java.util.HashMap[String, Long]] = buildable[java.util.HashMap]
}

0 comments on commit 5bfe51a

Please sign in to comment.