Skip to content

Instantly share code, notes, and snippets.

@alextp
Created January 27, 2013 20:35

Revisions

  1. alextp created this gist Jan 27, 2013.
    21 changes: 21 additions & 0 deletions Serialize.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    class StringMapCubbie[T](val m: mutable.Map[String,T]) extends Cubbie {
    var akeys : Seq[String] = null
    var avalues: Seq[T] = null
    setMap(new mutable.Map[String, Any] {
    override def update(key: String, value: Any): Unit = {
    if (key == "keys") {
    akeys = value.asInstanceOf[Traversable[String]].toSeq
    } else if (key == "values") {
    assert(keys != null)
    avalues = value.asInstanceOf[Traversable[T]].toSeq
    for (i <- 0 until keys.size) {
    m(akeys(i)) = avalues(i)
    }
    }
    }
    def += (kv: (String, Any)): this.type = { update(kv._1, kv._2); this }
    def -= (key: String): this.type = sys.error("Can't remove slots from cubbie map!")
    def get(key: String): Option[Any] = if (key == "keys") Some(m.keys.toTraversable) else if (key == "values") Some(m.values.toTraversable) else None
    def iterator: Iterator[(String, Any)] = Seq(("keys", get("keys")), ("values", get("values"))).iterator
    })
    }