hierarchical in-memory settings

This commit is contained in:
Mark Harrah 2010-09-08 14:29:00 -04:00
parent 0c12c5e2bd
commit b03b56ea2e
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package sbt
sealed trait Settings
{
def get[T](key: AttributeKey[T], path: List[String]): Option[T]
def set[T](key: AttributeKey[T], path: List[String], value: T): Settings
}
object Settings
{
def empty: Settings = new Basic(Map.empty)
def x = 3
private[this] class Basic(val roots: Map[ List[String], AttributeMap ]) extends Settings
{
def get[T](key: AttributeKey[T], path: List[String]): Option[T] =
{
def notFound = path match {
case Nil => None
case x :: xs => get(key, xs)
}
(roots get path) flatMap ( _ get key ) orElse notFound
}
def set[T](key: AttributeKey[T], path: List[String], value: T): Settings =
{
val amap = (roots get path) getOrElse AttributeMap.empty
new Basic( roots updated(path, amap put(key, value)) )
}
}
}