1 7 package org.jboss.cache.aop; 8 9 import org.jboss.cache.Fqn; 10 import org.jboss.cache.Node; 11 import org.jboss.util.NestedRuntimeException; 12 13 import java.util.*; 14 15 18 public class CachedMap extends AbstractMap implements Map 19 { 20 21 protected class Entry implements Map.Entry 22 { 23 24 Object key; 25 26 public Entry(Object key) 27 { 28 this.key = key; 29 } 30 31 public Object getKey() 32 { 33 return key; 34 } 35 36 public Object getValue() 37 { 38 try { 39 return cache.getObject(new Fqn(fqn, key)); 40 } catch (Exception e) { 41 throw new NestedRuntimeException(e); 42 } 43 } 44 45 public Object setValue(Object value) 46 { 47 try { 48 return cache.putObject(new Fqn(fqn, key), value); 49 } catch (Exception e) { 50 throw new NestedRuntimeException(e); 51 } 52 } 53 54 public int hashCode() 55 { 56 Object value = getValue(); 57 return ((key == null) ? 0 : key.hashCode()) 58 ^ ((value == null) ? 0 : value.hashCode()); 59 } 60 61 public boolean equals(Object obj) 62 { 63 if (!(obj instanceof Map.Entry)) 64 return false; 65 Map.Entry entry = (Map.Entry) obj; 66 Object value = getValue(); 67 return ( 68 key == null 69 ? entry.getKey() == null 70 : key.equals(entry.getKey())) 71 && (value == null 72 ? entry.getValue() == null 73 : value.equals(entry.getValue())); 74 } 75 } 76 77 protected TreeCacheAop cache; 78 protected Fqn fqn; 79 80 protected Node getNode() 81 { 82 try { 83 return cache.get(fqn); 84 } catch (Exception e) { 85 throw new NestedRuntimeException(e); 86 } 87 } 88 89 protected CachedMap(TreeCacheAop cache, Fqn fqn) 90 { 91 this.cache = cache; 92 this.fqn = fqn; 93 } 94 95 public Object get(Object key) 96 { 97 try { 98 return cache.getObject(new Fqn(fqn, key)); 99 } catch (Exception e) { 100 throw new NestedRuntimeException(e); 101 } 102 } 103 104 public Object put(Object key, Object value) 105 { 106 try { 107 return cache.putObject(new Fqn(fqn, key), value); 108 } catch (Exception e) { 109 throw new NestedRuntimeException(e); 110 } 111 } 112 113 public void putAll(Map map) 114 { 115 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 116 Map.Entry entry = (Map.Entry) i.next(); 117 put(entry.getKey(), entry.getValue()); 118 } 119 } 120 121 public Object remove(Object key) 122 { 123 try { 124 return cache.removeObject(new Fqn(fqn, key)); 125 } catch (Exception e) { 126 throw new NestedRuntimeException(e); 127 } 128 } 129 130 public void clear() 131 { 132 for (Iterator i = keySet().iterator(); i.hasNext();) { 133 remove(i.next()); 134 } 135 } 136 137 public int size() 138 { 139 Map children = getNode().getChildren(); 140 return children == null ? 0 : children.size(); 141 } 142 143 public boolean isEmpty() 144 { 145 return size() == 0; 146 } 147 148 public boolean containsKey(Object object) 149 { 150 Map children = getNode().getChildren(); 151 return children == null ? false : children.containsKey(object); 152 } 153 154 public boolean containsValue(Object object) 155 { 156 return false; 157 } 158 159 public Set entrySet() 160 { 161 final CachedMap map = this; 162 163 return new AbstractSet() 164 { 165 166 public int size() 167 { 168 Map children = getNode().getChildren(); 169 return children == null ? 0 : children.size(); 170 } 171 172 public Iterator iterator() 173 { 174 Map children = getNode().getChildren(); 175 final Iterator i = 176 children == null 177 ? Collections.EMPTY_LIST.iterator() 178 : children.keySet().iterator(); 179 180 return new Iterator() 181 { 182 Object lastKey; 184 public boolean hasNext() 185 { 186 return i.hasNext(); 187 } 188 189 public Object next() 190 { 191 return new Entry(lastKey = i.next()); 192 } 193 194 public void remove() 195 { 196 map.remove(lastKey); 197 } 198 }; 199 } 200 }; 201 } 202 203 public Collection values() 204 { 205 return null; } 207 208 public Set keySet() 209 { 210 Map children = getNode().getChildren(); 211 return children == null ? Collections.EMPTY_SET : children.keySet(); 212 } 213 214 public int hashCode() 215 { 216 int result = 0; 217 for (Iterator i = entrySet().iterator(); i.hasNext();) { 218 result += i.next().hashCode(); 219 } 220 return result; 221 } 222 223 public boolean equals(Object object) 224 { 225 if (object == this) 226 return true; 227 if (!(object instanceof Map)) 228 return false; 229 Map map = (Map) object; 230 if (size() != map.size()) 231 return false; 232 for (Iterator i = entrySet().iterator(); i.hasNext();) { 233 Map.Entry entry = (Map.Entry) i.next(); 234 Object value = entry.getValue(); 235 if (value != null) { 236 if (map.get(entry.getKey()) != null 237 || !map.containsKey(entry.getKey())) 238 return false; 239 } else { 240 if (!value.equals(map.get(entry.getKey()))) 241 return false; 242 } 243 } 244 return true; 245 } 246 247 } 248 | Popular Tags |