01: /*
02: * Created on Aug 17, 2005
03: */
04: package uk.org.ponder.rsf.template;
05:
06: import java.util.HashMap;
07: import java.util.Iterator;
08:
09: import uk.org.ponder.stringutil.CharWrap;
10:
11: /** Maintains a Map of String IDs to XMLLumpList.
12: *
13: * @author Antranig Basman (antranig@caret.cam.ac.uk)
14: */
15:
16: public class XMLLumpMMap {
17: private HashMap idtolumps = new HashMap(8);
18:
19: public String getHeadsDebug() {
20: CharWrap message = new CharWrap();
21: message.append("Heads: (");
22: boolean first = true;
23: for (Iterator keys = idtolumps.keySet().iterator(); keys
24: .hasNext();) {
25: if (!first) {
26: message.append(", ");
27: }
28: message.append((String) keys.next());
29: first = false;
30: }
31: message.append(")");
32: return message.toString();
33: }
34:
35: public XMLLumpList headsForID(String ID) {
36: XMLLumpList togo = (XMLLumpList) idtolumps.get(ID);
37: return togo;
38: }
39:
40: public XMLLumpList headsForIDEnsure(String ID) {
41: XMLLumpList togo = (XMLLumpList) idtolumps.get(ID);
42: if (togo == null) {
43: togo = new XMLLumpList();
44: idtolumps.put(ID, togo);
45: }
46: return togo;
47: }
48:
49: public boolean hasID(String ID) {
50: return idtolumps.get(ID) != null;
51: }
52:
53: /**
54: * Returns an iterator of the String values of the IDs represented here.
55: */
56: public Iterator iterator() {
57: return idtolumps.keySet().iterator();
58: }
59:
60: private int concretes = 0;
61:
62: public int numConcretes() {
63: return concretes;
64: }
65:
66: public void addLump(String ID, XMLLump lump) {
67: XMLLumpList list = headsForIDEnsure(ID);
68: list.add(lump);
69: ++concretes;
70: }
71:
72: public void aggregate(XMLLumpMMap toaccrete) {
73: for (Iterator it = toaccrete.iterator(); it.hasNext();) {
74: String key = (String) it.next();
75: XMLLumpList list = toaccrete.headsForID(key);
76: for (int i = 0; i < list.size(); ++i) {
77: addLump(key, list.lumpAt(i));
78: }
79: }
80: }
81:
82: }
|