sbt/launch/Cache.scala

34 lines
687 B
Scala
Raw Normal View History

2009-10-18 04:40:02 +02:00
package xsbt.boot
import java.lang.ref.{Reference, SoftReference}
2009-10-18 04:40:02 +02:00
import java.util.HashMap
final class Cache[K,V](create: K => V) extends NotNull
2009-10-18 04:40:02 +02:00
{
private[this] val delegate = new HashMap[K,Reference[V]]
2009-10-18 04:40:02 +02:00
def apply(k: K): V =
{
val existingRef = delegate.get(k)
if(existingRef == null)
newEntry(k)
else
{
val existing = existingRef.get
if(existing == null)
{
println("Cache value for '" + k + "' was garbage collected, recreating it...")
newEntry(k)
}
else
existing
}
2009-10-18 04:40:02 +02:00
}
private[this] def newEntry(k: K): V =
{
val v = create(k)
Pre.assert(v != null, "Value for key " + k + " was null")
delegate.put(k, new SoftReference(v))
2009-10-18 04:40:02 +02:00
v
}
}