KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > nrl > ContextMap


1 package com.thaiopensource.validate.nrl;
2
3 import com.thaiopensource.util.Equal;
4
5 import java.util.Vector JavaDoc;
6 import java.util.Hashtable JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.NoSuchElementException JavaDoc;
9
10 class ContextMap {
11   private Object JavaDoc rootValue;
12   private Object JavaDoc otherValue;
13   private final Hashtable JavaDoc nameTable = new Hashtable JavaDoc();
14
15   Object JavaDoc get(Vector JavaDoc context) {
16     return get(context, context.size());
17   }
18
19   boolean put(boolean isRoot, Vector JavaDoc names, Object JavaDoc value) {
20     return put(isRoot, names, names.size(), value);
21   }
22
23   private Object JavaDoc get(Vector JavaDoc context, int len) {
24     if (len > 0) {
25       ContextMap nestedMap = (ContextMap)nameTable.get(context.elementAt(len - 1));
26       if (nestedMap != null) {
27         Object JavaDoc value = nestedMap.get(context, len - 1);
28         if (value != null)
29           return value;
30       }
31     }
32     if (rootValue != null && len == 0)
33       return rootValue;
34     return otherValue;
35   }
36
37   private boolean put(boolean isRoot, Vector JavaDoc names, int len, Object JavaDoc value) {
38     if (len == 0) {
39       if (isRoot) {
40         if (rootValue != null)
41           return false;
42         rootValue = value;
43       }
44       else {
45         if (otherValue != null)
46           return false;
47         otherValue = value;
48       }
49       return true;
50     }
51     else {
52       Object JavaDoc name = names.elementAt(len - 1);
53       ContextMap nestedMap = (ContextMap)nameTable.get(name);
54       if (nestedMap == null) {
55         nestedMap = new ContextMap();
56         nameTable.put(name, nestedMap);
57       }
58       return nestedMap.put(isRoot, names, len - 1, value);
59     }
60   }
61
62   public boolean equals(Object JavaDoc obj) {
63     if (!(obj instanceof ContextMap))
64       return false;
65     ContextMap other = (ContextMap)obj;
66     if (!Equal.equal(this.rootValue, other.rootValue)
67         || !Equal.equal(this.otherValue, other.otherValue))
68       return false;
69     // We want jing to work with JDK 1.1 so we cannot use Hashtable.equals
70
if (this.nameTable.size() != other.nameTable.size())
71       return false;
72     for (Enumeration JavaDoc e = nameTable.keys(); e.hasMoreElements();) {
73       Object JavaDoc key = e.nextElement();
74       if (!nameTable.get(key).equals(other.nameTable.get(key)))
75         return false;
76     }
77     return true;
78   }
79
80   public int hashCode() {
81     int hc = 0;
82     if (rootValue != null)
83       hc ^= rootValue.hashCode();
84     if (otherValue != null)
85       hc ^= otherValue.hashCode();
86     for (Enumeration JavaDoc e = nameTable.keys(); e.hasMoreElements();) {
87       Object JavaDoc key = e.nextElement();
88       hc ^= key.hashCode();
89       hc ^= nameTable.get(key).hashCode();
90     }
91     return hc;
92   }
93
94   static private class Enumerator implements Enumeration JavaDoc {
95     private Object JavaDoc rootValue;
96     private Object JavaDoc otherValue;
97     private Enumeration JavaDoc subMapValues;
98     private final Enumeration JavaDoc subMaps;
99
100     private Enumerator(ContextMap map) {
101       rootValue = map.rootValue;
102       otherValue = map.otherValue;
103       subMaps = map.nameTable.elements();
104     }
105
106     private void prep() {
107       while ((subMapValues == null || !subMapValues.hasMoreElements()) && subMaps.hasMoreElements())
108         subMapValues = ((ContextMap)subMaps.nextElement()).values();
109     }
110
111     public boolean hasMoreElements() {
112       prep();
113       return rootValue != null || otherValue != null || (subMapValues != null && subMapValues.hasMoreElements());
114     }
115
116     public Object JavaDoc nextElement() {
117       if (rootValue != null) {
118         Object JavaDoc tem = rootValue;
119         rootValue = null;
120         return tem;
121       }
122       if (otherValue != null) {
123         Object JavaDoc tem = otherValue;
124         otherValue = null;
125         return tem;
126       }
127       prep();
128       if (subMapValues == null)
129         throw new NoSuchElementException JavaDoc();
130       return subMapValues.nextElement();
131     }
132   }
133
134   Enumeration JavaDoc values() {
135     return new Enumerator(this);
136   }
137 }
138
Popular Tags