1 19 package gnu.trove.decorator; 20 21 import gnu.trove.TByteObjectHashMap; 22 import gnu.trove.TByteObjectIterator; 23 import java.util.AbstractMap; 24 import java.util.AbstractSet; 25 import java.util.Collection; 26 import java.util.Iterator; 27 import java.util.Map.Entry; 28 import java.util.Map; 29 import java.util.Set; 30 31 48 public class TByteObjectHashMapDecorator extends AbstractMap implements Map, Cloneable { 49 50 protected TByteObjectHashMap _map; 51 52 55 public TByteObjectHashMapDecorator(TByteObjectHashMap map) { 56 super(); 57 this._map = map; 58 } 59 60 67 public Object clone() { 68 try { 69 TByteObjectHashMapDecorator copy = (TByteObjectHashMapDecorator) super.clone(); 70 copy._map = (TByteObjectHashMap)_map.clone(); 71 return copy; 72 } catch (CloneNotSupportedException e) { 73 throw new InternalError(); } 76 } 77 78 86 public Object put(Object key, Object value) { 87 return wrapValue(_map.put(unwrapKey(key), unwrapValue(value))); 88 } 89 90 97 public boolean equals(Object other) { 98 if (_map.equals(other)) { 99 return true; } else if (other instanceof Map) { 101 Map that = (Map)other; 102 if (that.size() != _map.size()) { 103 return false; } else { Iterator it = that.entrySet().iterator(); 106 for (int i = that.size(); i-- > 0;) { 107 Map.Entry e = (Map.Entry)it.next(); 108 Object key = e.getKey(); 109 Object val = e.getValue(); 110 if (key instanceof Byte && val instanceof Object) { 111 byte k = unwrapKey(key); 112 Object v = unwrapValue(val); 113 if (_map.containsKey(k) && v == _map.get(k)) { 114 } else { 116 return false; } 118 } else { 119 return false; } 121 } 122 return true; } 124 } else { 125 return false; 126 } 127 } 128 129 135 public Object get(Object key) { 136 return _map.get(unwrapKey(key)); 137 } 138 139 140 143 public void clear() { 144 this._map.clear(); 145 } 146 147 153 public Object remove(Object key) { 154 return wrapValue(_map.remove(unwrapKey(key))); 155 } 156 157 162 public Set entrySet() { 163 return new AbstractSet() { 164 public int size() { 165 return _map.size(); 166 } 167 public boolean isEmpty() { 168 return TByteObjectHashMapDecorator.this.isEmpty(); 169 } 170 public boolean contains(Object o) { 171 if (o instanceof Map.Entry) { 172 Object k = ((Map.Entry)o).getKey(); 173 Object v = ((Map.Entry)o).getValue(); 174 return (TByteObjectHashMapDecorator.this.containsKey(k) && 175 TByteObjectHashMapDecorator.this.get(k).equals(v)); 176 } else { 177 return false; 178 } 179 } 180 public Iterator iterator() { 181 return new Iterator() { 182 private final TByteObjectIterator it = _map.iterator(); 183 184 public Object next() { 185 it.advance(); 186 final Object key = wrapKey(it.key()); 187 final Object v = wrapValue(it.value()); 188 return new Map.Entry() { 189 private Object val = v; 190 public boolean equals(Object o) { 191 return ((o instanceof Map.Entry) && 192 ((Map.Entry)o).getKey().equals(key) && 193 ((Map.Entry)o).getValue().equals(val)); 194 } 195 public Object getKey() { 196 return key; 197 } 198 public Object getValue() { 199 return val; 200 } 201 public int hashCode() { 202 return key.hashCode() + val.hashCode(); 203 } 204 public Object setValue(Object value) { 205 val = value; 206 return put(key, value); 207 } 208 }; 209 } 210 211 public boolean hasNext() { 212 return it.hasNext(); 213 } 214 215 public void remove() { 216 it.remove(); 217 } 218 }; 219 } 220 public boolean add(Object o) { 221 throw new UnsupportedOperationException(); 222 } 223 public boolean remove(Object o) { 224 throw new UnsupportedOperationException(); 225 } 226 public boolean addAll(Collection c) { 227 throw new UnsupportedOperationException(); 228 } 229 public boolean retainAll(Collection c) { 230 throw new UnsupportedOperationException(); 231 } 232 public boolean removeAll(Collection c) { 233 throw new UnsupportedOperationException(); 234 } 235 public void clear() { 236 TByteObjectHashMapDecorator.this.clear(); 237 } 238 }; 239 } 240 241 247 public boolean containsValue(Object val) { 248 return _map.containsValue(unwrapValue(val)); 249 } 250 251 257 public boolean containsKey(Object key) { 258 return _map.containsKey(unwrapKey(key)); 259 } 260 261 265 public int size() { 266 return this._map.size(); 267 } 268 269 273 public boolean isEmpty() { 274 return (size() == 0); 275 } 276 277 284 public void putAll(Map map) { 285 Iterator it = map.entrySet().iterator(); 286 for (int i = map.size(); i-- > 0;) { 287 Map.Entry e = (Map.Entry)it.next(); 288 this.put(e.getKey(), e.getValue()); 289 } 290 } 291 292 298 protected Byte wrapKey(byte k) { 299 return new Byte(k); 300 } 301 302 308 protected byte unwrapKey(Object key) { 309 return ((Byte)key).byteValue(); 310 } 311 317 protected final Object wrapValue(Object o) { 318 return o; 319 } 320 321 327 protected final Object unwrapValue(Object value) { 328 return value; 329 } 330 331 } | Popular Tags |