2009-10-18 04:40:02 +02:00
|
|
|
package xsbt.boot
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap
|
|
|
|
|
|
2009-10-19 04:25:50 +02:00
|
|
|
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)
|
2009-10-19 04:25:50 +02:00
|
|
|
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)
|
2009-10-19 04:25:50 +02:00
|
|
|
Pre.assert(v != null, "Value for key " + k + " was null")
|
2009-10-18 04:40:02 +02:00
|
|
|
delegate.put(k, v)
|
|
|
|
|
v
|
|
|
|
|
}
|
2009-10-19 04:25:50 +02:00
|
|
|
}
|