1 package com.thaiopensource.validate.nrl; 2 3 import com.thaiopensource.util.Equal; 4 5 import java.util.Vector ; 6 import java.util.Hashtable ; 7 import java.util.Enumeration ; 8 import java.util.NoSuchElementException ; 9 10 class ContextMap { 11 private Object rootValue; 12 private Object otherValue; 13 private final Hashtable nameTable = new Hashtable (); 14 15 Object get(Vector context) { 16 return get(context, context.size()); 17 } 18 19 boolean put(boolean isRoot, Vector names, Object value) { 20 return put(isRoot, names, names.size(), value); 21 } 22 23 private Object get(Vector context, int len) { 24 if (len > 0) { 25 ContextMap nestedMap = (ContextMap)nameTable.get(context.elementAt(len - 1)); 26 if (nestedMap != null) { 27 Object 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 names, int len, Object 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 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 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 if (this.nameTable.size() != other.nameTable.size()) 71 return false; 72 for (Enumeration e = nameTable.keys(); e.hasMoreElements();) { 73 Object 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 e = nameTable.keys(); e.hasMoreElements();) { 87 Object 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 { 95 private Object rootValue; 96 private Object otherValue; 97 private Enumeration subMapValues; 98 private final Enumeration 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 nextElement() { 117 if (rootValue != null) { 118 Object tem = rootValue; 119 rootValue = null; 120 return tem; 121 } 122 if (otherValue != null) { 123 Object tem = otherValue; 124 otherValue = null; 125 return tem; 126 } 127 prep(); 128 if (subMapValues == null) 129 throw new NoSuchElementException (); 130 return subMapValues.nextElement(); 131 } 132 } 133 134 Enumeration values() { 135 return new Enumerator(this); 136 } 137 } 138 | Popular Tags |