From 1dc3f0ed56f5f9bab32d8380c7deefd9fef3b074 Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Wed, 22 Jun 2011 19:50:41 -0400 Subject: [PATCH] add missing file --- compile/inc/FileValueCache.scala | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 compile/inc/FileValueCache.scala diff --git a/compile/inc/FileValueCache.scala b/compile/inc/FileValueCache.scala new file mode 100644 index 000000000..36742747e --- /dev/null +++ b/compile/inc/FileValueCache.scala @@ -0,0 +1,49 @@ +package sbt +package inc + + import java.io.File + import java.util.concurrent.ConcurrentHashMap + +sealed trait FileValueCache[T] +{ + def clear(): Unit + def get: File => T +} + +private[this] final class FileValueCache0[T](getStamp: File => Stamp, make: File => T)(implicit equiv: Equiv[Stamp]) extends FileValueCache[T] +{ + private[this] val backing = new ConcurrentHashMap[File, FileCache] + + def clear(): Unit = backing.clear() + def get = file => { + val ifAbsent = new FileCache(file) + val cache = backing.putIfAbsent(file, ifAbsent) + (if(cache eq null) ifAbsent else cache).get() + } + + private[this] final class FileCache(file: File) + { + private[this] var stampedValue: Option[(Stamp,T)] = None + def get(): T = synchronized + { + val latest = getStamp(file) + stampedValue match + { + case Some( (stamp, value) ) if(equiv.equiv(latest, stamp)) => value + case _ => update(latest) + } + } + + private[this] def update(stamp: Stamp): T = + { + val value = make(file) + stampedValue = Some((stamp, value)) + value + } + } +} +object FileValueCache +{ + def apply[T](f: File => T): FileValueCache[T] = make(Stamp.lastModified)(f) + def make[T](stamp: File => Stamp)(f: File => T): FileValueCache[T] = new FileValueCache0[T](stamp, f) +} \ No newline at end of file