1 package org.jruby.util.collections; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 public class SlottedHashMap implements Map { 12 private SlottedHashMap parent; 13 private Map map; 14 private List children; 15 private Object owner; 16 17 public class Slot { 18 private Object value; 19 20 public void setValue(Object value) { 21 this.value = value; 22 } 23 24 public Object getValue() { 25 return value; 26 } 27 28 public SlottedHashMap getParent() { 29 return SlottedHashMap.this; 30 } 31 32 public String toString() { 33 return "Slot#" + hashCode() + "[parent = " + getParent().hashCode() + ", value = " + getValue() + "]"; 34 } 35 } 36 37 public SlottedHashMap(Object owner) { 38 super(); 39 children = new ArrayList (); 40 map = new HashMap (); 41 this.owner = owner; 42 } 43 44 public SlottedHashMap(Object owner, SlottedHashMap smap) { 45 this(owner); 46 parent = smap; 47 smap.addChild(this); 48 49 for (Iterator iter = parent.entrySet().iterator(); iter.hasNext();) { 50 Map.Entry entry = (Map.Entry )iter.next(); 51 52 map.put(entry.getKey(), entry.getValue()); 53 } 54 } 55 56 public void addChild(SlottedHashMap smap) { 57 children.add(smap); 58 } 59 60 public void setParent(SlottedHashMap smap) { 61 this.parent = smap; 62 } 63 64 71 public Object put(Object key, Object value) { 72 Slot slot = (Slot)map.get(key); 73 if (slot == null || slot.getParent() != this) { 74 slot = new Slot(); 75 } 76 77 slot.setValue(value); 78 79 for (Iterator iter = children.iterator(); iter.hasNext();) { 81 Object obj = iter.next(); 82 SlottedHashMap shm = (SlottedHashMap)obj; 83 84 shm.put(key, value, this); 85 } 86 87 return map.put(key, slot); 88 } 89 90 98 protected Object put(Object key, Object value, SlottedHashMap parent) { 99 Slot slot = getSlot(key); 100 if (slot != null) { 101 if (slot.getParent() == this) { 102 return get(key); 104 } 105 } 106 107 slot = parent.getSlot(key); 108 109 for (Iterator iter = children.iterator(); iter.hasNext();) { 111 SlottedHashMap shm = (SlottedHashMap)iter.next(); 112 shm.put(key, value, parent); 113 } 114 115 return map.put(key, slot); 116 } 117 118 public Object get(Object key) { 119 Slot slot = getSlot(key); 120 121 if (slot == null) return null; 122 123 return slot.getValue(); 124 } 125 126 public Slot getSlot(Object key) { 127 return (Slot)map.get(key); 128 } 129 130 public int size() { 131 return map.size(); 132 } 133 134 public void clear() { 135 throw new RuntimeException ("clear not implemented"); 136 } 137 138 public boolean isEmpty() { 139 return map.isEmpty(); 140 } 141 142 public boolean containsKey(Object key) { 143 return map.containsKey(key); 144 } 145 146 public boolean containsValue(Object value) { 147 return false; 149 } 150 151 public Collection values() { 152 return null; 154 } 155 156 public void putAll(Map t) { 157 159 } 160 161 public Set entrySet() { 162 return map.entrySet(); 163 } 164 165 public Set keySet() { 166 return map.keySet(); 167 } 168 169 public Object remove(Object key) { 170 Slot slot = getSlot(key); 171 172 if (slot != null) { 173 if (slot.getParent() == this) { 174 map.remove(key); 175 176 for (Iterator iter = children.iterator(); iter.hasNext();) { 178 SlottedHashMap child = (SlottedHashMap)iter.next(); 179 180 child.remove(key, this); 181 } 182 183 return slot.getValue(); 184 } else { 185 } 187 } 188 189 return null; 190 } 191 192 protected void remove(Object key, SlottedHashMap parent) { 193 Slot slot = getSlot(key); 194 195 if (slot.getParent() == this) { 196 return; 198 } else { 199 map.remove(key); 200 } 201 202 for (Iterator iter = children.iterator(); iter.hasNext();) { 204 SlottedHashMap child = (SlottedHashMap)iter.next(); 205 206 child.remove(key, parent); 207 } 208 } 209 210 public Object getOwner() { 211 return owner; 212 } 213 } 214 | Popular Tags |