diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2017-08-15 00:06:26 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2017-08-15 00:06:26 +0200 |
commit | 6721d325d018756296213ac8f9129bc304a21afb (patch) | |
tree | e08a5ce92802f521be228beae0ddb4ef258d0066 /kamon-core/src/main/scala/kamon/context/Context.scala | |
parent | ac3b72e27765ceec4cc3958b0fa7eaba0299f017 (diff) | |
parent | a949c875684d78818224cd2ca7aaf79aa7878724 (diff) | |
download | Kamon-6721d325d018756296213ac8f9129bc304a21afb.tar.gz Kamon-6721d325d018756296213ac8f9129bc304a21afb.tar.bz2 Kamon-6721d325d018756296213ac8f9129bc304a21afb.zip |
Merge remote-tracking branch 'ivantopo/wip/moving-ot-support-to-a-separeate-project' into kamon-1.0-develop
Diffstat (limited to 'kamon-core/src/main/scala/kamon/context/Context.scala')
-rw-r--r-- | kamon-core/src/main/scala/kamon/context/Context.scala | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/kamon-core/src/main/scala/kamon/context/Context.scala b/kamon-core/src/main/scala/kamon/context/Context.scala new file mode 100644 index 00000000..f8a4662f --- /dev/null +++ b/kamon-core/src/main/scala/kamon/context/Context.scala @@ -0,0 +1,50 @@ +package kamon.context + +class Context private (private[context] val entries: Map[Key[_], Any]) { + def get[T](key: Key[T]): T = + entries.get(key).getOrElse(key.emptyValue).asInstanceOf[T] + + def withKey[T](key: Key[T], value: T): Context = + new Context(entries.updated(key, value)) +} + +object Context { + val Empty = new Context(Map.empty) + + def apply(): Context = + Empty + + def create(): Context = + Empty + + def apply[T](key: Key[T], value: T): Context = + new Context(Map(key -> value)) + + def create[T](key: Key[T], value: T): Context = + apply(key, value) +} + + +trait Key[T] { + def name: String + def emptyValue: T + def broadcast: Boolean +} + +object Key { + + def local[T](name: String, emptyValue: T): Key[T] = + new Default[T](name, emptyValue, false) + + def broadcast[T](name: String, emptyValue: T): Key[T] = + new Default[T](name, emptyValue, true) + + + private class Default[T](val name: String, val emptyValue: T, val broadcast: Boolean) extends Key[T] { + override def hashCode(): Int = + name.hashCode + + override def equals(that: Any): Boolean = + that.isInstanceOf[Default[_]] && that.asInstanceOf[Default[_]].name == this.name + } +}
\ No newline at end of file |