From b03b56ea2e7233e1d15d6e300d39115bdcfacddc Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Wed, 8 Sep 2010 14:29:00 -0400 Subject: [PATCH] hierarchical in-memory settings --- util/collection/Settings.scala | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 util/collection/Settings.scala diff --git a/util/collection/Settings.scala b/util/collection/Settings.scala new file mode 100644 index 000000000..ab85fd87d --- /dev/null +++ b/util/collection/Settings.scala @@ -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)) ) + } + } +} \ No newline at end of file