summaryrefslogtreecommitdiff
path: root/src/library/scala/xml/include/persistent/IndexedStorage.scala
blob: c14c88e5b1ed177060c942e09edf2a83b9f7e86a (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002-2007, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id$


package scala.xml.persistent

import scala.collection.mutable
import java.io.File

/** indexed multiset of xml trees. The index may be an arbitrary totally
 * type, especially one can construct indices by selecting parts of
 * xml nodes.
 */
class IndexedStorage[A](file:  File, index: Index[A]) //@todo
extends CachedFileStorage(file) {

  private var theMap: mutable.Map[A,Node] = new mutable.HashMap[A,Node]()

  super.initialNodes.foreach { x:Node => this += x }

  this.dirty = false

  def += (e: Node): Unit = synchronized {
    log("added element at index '"+index(e)+"'")
    dirty = true
    theMap(index(e)) = e
  }

  def -= (e: Node): Unit = synchronized {
    log("removed element at index '"+index(e)+"'")
    dirty = true
    theMap -= index( e )
  }

  def nodes: Iterator[Node] = synchronized {
    theMap.values
  }

  def lookup(n: A): Option[Node] = theMap.get(n)

}