From dd8d58a9c06a44272ca83d0e38a5157f1f1a6120 Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Fri, 27 Aug 2010 19:17:03 -0400 Subject: [PATCH] cross-configurations --- util/collection/Attributes.scala | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/util/collection/Attributes.scala b/util/collection/Attributes.scala index 231cda300..488142ad7 100644 --- a/util/collection/Attributes.scala +++ b/util/collection/Attributes.scala @@ -8,10 +8,12 @@ import Types._ // T must be invariant to work properly. // Because it is sealed and the only instances go through make, // a single AttributeKey instance cannot conform to AttributeKey[T] for different Ts -sealed trait AttributeKey[T] +sealed trait AttributeKey[T] { + def label: String +} object AttributeKey { - def make[T]: AttributeKey[T] = new AttributeKey[T] {} + def apply[T](name: String): AttributeKey[T] = new AttributeKey[T] { def label = name } } trait AttributeMap @@ -20,15 +22,35 @@ trait AttributeMap def get[T](k: AttributeKey[T]): Option[T] def contains[T](k: AttributeKey[T]): Boolean def put[T](k: AttributeKey[T], value: T): AttributeMap + def keys: Iterable[AttributeKey[_]] + def ++(o: AttributeMap): AttributeMap + def entries: Iterable[AttributeEntry[_]] + def isEmpty: Boolean } object AttributeMap { - def empty: AttributeMap = new BasicAttributeMap(Map.empty) + val empty: AttributeMap = new BasicAttributeMap(Map.empty) } -private class BasicAttributeMap(backing: Map[AttributeKey[_], Any]) extends AttributeMap +private class BasicAttributeMap(private val backing: Map[AttributeKey[_], Any]) extends AttributeMap { + def isEmpty: Boolean = backing.isEmpty def apply[T](k: AttributeKey[T]) = backing(k).asInstanceOf[T] def get[T](k: AttributeKey[T]) = backing.get(k).asInstanceOf[Option[T]] def contains[T](k: AttributeKey[T]) = backing.contains(k) def put[T](k: AttributeKey[T], value: T): AttributeMap = new BasicAttributeMap( backing.updated(k, value) ) + def keys: Iterable[AttributeKey[_]] = backing.keys + def ++(o: AttributeMap): AttributeMap = + o match { + case bam: BasicAttributeMap => new BasicAttributeMap(backing ++ bam.backing) + case _ => o ++ this + } + def entries: Iterable[AttributeEntry[_]] = + for( (k: AttributeKey[kt], v) <- backing) yield AttributeEntry(k, v.asInstanceOf[kt]) + override def toString = entries.mkString("(", ", ", ")") +} + +// type inference required less generality +final case class AttributeEntry[T](a: AttributeKey[T], b: T) +{ + override def toString = a.label + ": " + b } \ No newline at end of file