All Downloads are FREE. Search and download functionalities are using the official Maven repository.

skinny.micro.data.MultiMap.scala Maven / Gradle / Ivy

package skinny.micro.data

import scala.language.implicitConversions

import scala.collection.immutable.Map

/**
 * Map which contains multi values for each key.
 */
class MultiMap(wrapped: Map[String, Seq[String]] = Map.empty) extends Map[String, Seq[String]] {

  def get(key: String): Option[Seq[String]] = {
    (wrapped.get(key) orElse wrapped.get(key + "[]"))
  }

  def get(key: Symbol): Option[Seq[String]] = get(key.name)

  def +[B1 >: Seq[String]](kv: (String, B1)): MultiMap = {
    new MultiMap(wrapped + kv.asInstanceOf[(String, Seq[String])])
  }

  def -(key: String): MultiMap = new MultiMap(wrapped - key)

  def iterator: Iterator[(String, Seq[String])] = wrapped.iterator

  override def default(a: String): Seq[String] = wrapped.default(a)

}

object MultiMap {

  def apply(): MultiMap = new MultiMap

  def apply[SeqType <: Seq[String]](wrapped: Map[String, SeqType]): MultiMap = new MultiMap(wrapped)

  def empty: MultiMap = apply()

  implicit def map2MultiMap(map: Map[String, Seq[String]]): MultiMap = new MultiMap(map)

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy