summaryrefslogtreecommitdiff
path: root/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala
diff options
context:
space:
mode:
Diffstat (limited to 'examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala')
-rw-r--r--examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala89
1 files changed, 89 insertions, 0 deletions
diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala
new file mode 100644
index 0000000..f215e6e
--- /dev/null
+++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala
@@ -0,0 +1,89 @@
+/* __ *\
+** ________ ___ / / ___ __ ____ Scala.js API **
+** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | |__/ /____/ **
+** |/____/ **
+\* */
+
+package scala.scalajs.js
+
+import scala.collection.mutable
+import mutable.Builder
+
+import scala.collection.generic.CanBuildFrom
+
+/** Wrapper to use a js.Dictionary as a scala.mutable.Map */
+@inline
+class WrappedDictionary[A](val dict: Dictionary[A])
+ extends mutable.AbstractMap[String, A]
+ with mutable.Map[String, A]
+ with mutable.MapLike[String, A, WrappedDictionary[A]] {
+
+ def get(key: String): Option[A] = {
+ if (contains(key))
+ Some(dict(key))
+ else
+ None
+ }
+
+ override def contains(key: String): Boolean =
+ dict.hasOwnProperty(key)
+
+ def -=(key: String): this.type = {
+ dict.delete(key)
+ this
+ }
+
+ def +=(kv: (String, A)): this.type = {
+ dict(kv._1) = kv._2
+ this
+ }
+
+ def iterator: Iterator[(String, A)] = new Iterator[(String, A)] {
+ private[this] val keys = Object.keys(dict)
+ private[this] var index: Int = 0
+ def hasNext(): Boolean = index < keys.length
+ def next(): (String, A) = {
+ val key = keys(index)
+ index += 1
+ (key, dict(key))
+ }
+ }
+
+ override def keys: Iterable[String] =
+ Object.keys(dict)
+
+ override def empty: WrappedDictionary[A] =
+ new WrappedDictionary(Dictionary.empty)
+
+}
+
+object WrappedDictionary {
+ // Note: We can't extend MutableMapFactory[WrappedDictionary] since
+ // it requires support for any type of key
+
+ def empty[A]: WrappedDictionary[A] = new WrappedDictionary(Dictionary.empty)
+
+ type CBF[A] = CanBuildFrom[WrappedDictionary[_], (String, A), WrappedDictionary[A]]
+ implicit def canBuildFrom[A]: CBF[A] = new CBF[A] {
+ def apply(from: WrappedDictionary[_]): Builder[(String, A), WrappedDictionary[A]] =
+ new WrappedDictionaryBuilder[A]
+ def apply(): Builder[(String, A), WrappedDictionary[A]] =
+ new WrappedDictionaryBuilder[A]
+ }
+
+ class WrappedDictionaryBuilder[A]
+ extends Builder[(String, A), WrappedDictionary[A]] {
+ private[this] var dict: Dictionary[A] = Dictionary.empty
+ def +=(elem: (String, A)): this.type = {
+ dict(elem._1) = elem._2
+ this
+ }
+ def clear(): Unit =
+ dict = Dictionary.empty
+ def result(): WrappedDictionary[A] =
+ new WrappedDictionary(dict)
+ }
+
+}