summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/Map.scala
blob: 66c8a7866630ef039916ec48f73743fe74c19f3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.collection

import generic._

/** A map from keys of type A to values of type B.
 *  To implement a concrete map, you need to provide implementations of the following methods:
 *  (where This is the type of the map in question):
 *
 *   def get(key: A): Option[B]
 *   def elements: Iterator[(A, B)]
 *   def add[B1 >: B](key: A, value: B1): This
 *   def -(key: A): This
 *
 * If you wish that methods like, take, drop, filter return the same kind of map, you should also
 * override:
 *
 *   def empty: This
 *
 * It might also be a good idea to override methods foreach and size for efficiency.
 *
 * @note If you do not have speicifc implementations for `add` and `-` in mind, you
 *       might consider inheriting from `DefaultMap` instead.
 *
 * @note Of you additions and mutations return the same kind of map as the map you are defining,
 *       you should inherit from `MapTemplate` as well.
 */
trait Map[A, +B] extends Iterable[(A, B)] with MapTemplate[A, B, Map[A, B]] {
  def empty: Map[A, B] = Map.empty

  override protected[this] def newBuilder : Builder[(A, B), Map[A, B], Any] =
    throw new UnsupportedOperationException("Map.newBuilder")

  def mapBuilder[A, B]: Builder[(A, B), Map[A, B], Any] = Map.newBuilder[A, B]
}

/* Factory object for `Map` class */
object Map extends ImmutableMapFactory[immutable.Map] {
  type Coll = Map[_, _]
  def empty[A, B]: immutable.Map[A, B] = immutable.Map.empty
  implicit def builderFactory[A, B]: BuilderFactory[(A, B), Map[A, B], Coll] = new BuilderFactory[(A, B), Map[A, B], Coll] { def apply(from: Coll) = from.mapBuilder[A, B] }
}