2010-08-22 04:55:42 +02:00
|
|
|
/* sbt -- Simple Build Tool
|
|
|
|
|
* Copyright 2010 Mark Harrah
|
|
|
|
|
*/
|
|
|
|
|
package sbt
|
|
|
|
|
|
|
|
|
|
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
|
2010-08-28 01:17:03 +02:00
|
|
|
sealed trait AttributeKey[T] {
|
|
|
|
|
def label: String
|
|
|
|
|
}
|
2010-08-22 04:55:42 +02:00
|
|
|
object AttributeKey
|
|
|
|
|
{
|
2010-08-28 01:17:03 +02:00
|
|
|
def apply[T](name: String): AttributeKey[T] = new AttributeKey[T] { def label = name }
|
2010-08-22 04:55:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait AttributeMap
|
|
|
|
|
{
|
|
|
|
|
def apply[T](k: AttributeKey[T]): T
|
|
|
|
|
def get[T](k: AttributeKey[T]): Option[T]
|
|
|
|
|
def contains[T](k: AttributeKey[T]): Boolean
|
|
|
|
|
def put[T](k: AttributeKey[T], value: T): AttributeMap
|
2010-08-28 01:17:03 +02:00
|
|
|
def keys: Iterable[AttributeKey[_]]
|
|
|
|
|
def ++(o: AttributeMap): AttributeMap
|
|
|
|
|
def entries: Iterable[AttributeEntry[_]]
|
|
|
|
|
def isEmpty: Boolean
|
2010-08-22 04:55:42 +02:00
|
|
|
}
|
|
|
|
|
object AttributeMap
|
|
|
|
|
{
|
2010-08-28 01:17:03 +02:00
|
|
|
val empty: AttributeMap = new BasicAttributeMap(Map.empty)
|
2010-08-22 04:55:42 +02:00
|
|
|
}
|
2010-08-28 01:17:03 +02:00
|
|
|
private class BasicAttributeMap(private val backing: Map[AttributeKey[_], Any]) extends AttributeMap
|
2010-08-22 04:55:42 +02:00
|
|
|
{
|
2010-08-28 01:17:03 +02:00
|
|
|
def isEmpty: Boolean = backing.isEmpty
|
2010-08-22 04:55:42 +02:00
|
|
|
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) )
|
2010-08-28 01:17:03 +02:00
|
|
|
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
|
2010-09-18 02:46:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final case class Attributed[D](data: D)(val metadata: AttributeMap)
|
|
|
|
|
{
|
|
|
|
|
def put[T](key: AttributeKey[T], value: T): Attributed[D] = Attributed(data)(metadata.put(key, value))
|
|
|
|
|
}
|
|
|
|
|
object Attributed
|
|
|
|
|
{
|
|
|
|
|
implicit def blank[T](data: T): Attributed[T] = Attributed(data)(AttributeMap.empty)
|
2010-08-22 04:55:42 +02:00
|
|
|
}
|