1
package com.thaiopensource.validate.nrl;
3
import com.thaiopensource.util.Equal;
5
import java.util.Vector;
6
import java.util.Hashtable;
7
import java.util.Enumeration;
8
import java.util.NoSuchElementException;
11
private Object rootValue;
12
private Object otherValue;
13
private final Hashtable nameTable = new Hashtable();
15
Object get(Vector context) {
16
return get(context, context.size());
19
boolean put(boolean isRoot, Vector names, Object value) {
20
return put(isRoot, names, names.size(), value);
23
private Object get(Vector context, int len) {
25
ContextMap nestedMap = (ContextMap)nameTable.get(context.elementAt(len - 1));
26
if (nestedMap != null) {
27
Object value = nestedMap.get(context, len - 1);
32
if (rootValue != null && len == 0)
37
private boolean put(boolean isRoot, Vector names, int len, Object value) {
40
if (rootValue != null)
45
if (otherValue != null)
52
Object 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);
58
return nestedMap.put(isRoot, names, len - 1, value);
62
public boolean equals(Object obj) {
63
if (!(obj instanceof ContextMap))
65
ContextMap other = (ContextMap)obj;
66
if (!Equal.equal(this.rootValue, other.rootValue)
67
|| !Equal.equal(this.otherValue, other.otherValue))
69
// We want jing to work with JDK 1.1 so we cannot use Hashtable.equals
70
if (this.nameTable.size() != other.nameTable.size())
72
for (Enumeration e = nameTable.keys(); e.hasMoreElements();) {
73
Object key = e.nextElement();
74
if (!nameTable.get(key).equals(other.nameTable.get(key)))
80
public int hashCode() {
82
if (rootValue != null)
83
hc ^= rootValue.hashCode();
84
if (otherValue != null)
85
hc ^= otherValue.hashCode();
86
for (Enumeration e = nameTable.keys(); e.hasMoreElements();) {
87
Object key = e.nextElement();
89
hc ^= nameTable.get(key).hashCode();
94
static private class Enumerator implements Enumeration {
95
private Object rootValue;
96
private Object otherValue;
97
private Enumeration subMapValues;
98
private final Enumeration subMaps;
100
private Enumerator(ContextMap map) {
101
rootValue = map.rootValue;
102
otherValue = map.otherValue;
103
subMaps = map.nameTable.elements();
106
private void prep() {
107
while ((subMapValues == null || !subMapValues.hasMoreElements()) && subMaps.hasMoreElements())
108
subMapValues = ((ContextMap)subMaps.nextElement()).values();
111
public boolean hasMoreElements() {
113
return rootValue != null || otherValue != null || (subMapValues != null && subMapValues.hasMoreElements());
116
public Object nextElement() {
117
if (rootValue != null) {
118
Object tem = rootValue;
122
if (otherValue != null) {
123
Object tem = otherValue;
128
if (subMapValues == null)
129
throw new NoSuchElementException();
130
return subMapValues.nextElement();
134
Enumeration values() {
135
return new Enumerator(this);