Skip to content

Instantly share code, notes, and snippets.

@crakjie
Last active September 12, 2017 14:16
Show Gist options
  • Save crakjie/a3b0bded56c58fa14b650031637e31f4 to your computer and use it in GitHub Desktop.
Save crakjie/a3b0bded56c58fa14b650031637e31f4 to your computer and use it in GitHub Desktop.
Try to have a case class pooling
abstract case class Toto private[Toto] (i: Int, j: String) {
val hashCodeVal : Int
override def hashCode() : Int = hashCodeVal
def copy(i: Int = i, j: String = j) = {
Toto(i,j)
}
}
object Toto {
val totoPool = scala.collection.mutable.WeakHashMap[Int, Toto]()
def apply(i: Int, j: String): Toto = {
val hash = hashCode(i,j)
println(totoPool)
totoPool.getOrElseUpdate(hash, new Toto(i: Int, j: String) {
override val hashCodeVal : Int = hash
})
}
def hashCode(i: Int, j: String) : Int = {
(i,j).hashCode()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment