sbt/launch/Cache.scala

21 lines
411 B
Scala
Raw Normal View History

2009-10-18 04:40:02 +02:00
package xsbt.boot
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,V]
def apply(k: K): V =
{
val existing = delegate.get(k)
if(existing == null) 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")
2009-10-18 04:40:02 +02:00
delegate.put(k, v)
v
}
}