Last active
September 12, 2017 14:16
-
-
Save crakjie/a3b0bded56c58fa14b650031637e31f4 to your computer and use it in GitHub Desktop.
Try to have a case class pooling
This file contains hidden or 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
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