1 19 package gnu.trove.decorator; 20 21 import gnu.trove.TFloatDoubleHashMap; 22 import gnu.trove.TFloatDoubleIterator; 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 TFloatDoubleHashMapDecorator extends AbstractMap implements Map, Cloneable { 49 50 protected TFloatDoubleHashMap _map; 51 52 55 public TFloatDoubleHashMapDecorator(TFloatDoubleHashMap map) { 56 super(); 57 this._map = map; 58 } 59 60 67 public Object clone() { 68 try { 69 TFloatDoubleHashMapDecorator copy = (TFloatDoubleHashMapDecorator) super.clone(); 70 copy._map = (TFloatDoubleHashMap)_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 Float && val instanceof Double) { 111 float k = unwrapKey(key); 112 double 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 float k = unwrapKey(key); 137 double v = _map.get(k); 138 if (v == 0) { 142 return _map.containsKey(k) ? wrapValue(v) : null; 143 } else { 144 return wrapValue(v); 145 } 146 } 147 148 149 152 public void clear() { 153 this._map.clear(); 154 } 155 156 162 public Object remove(Object key) { 163 return wrapValue(_map.remove(unwrapKey(key))); 164 } 165 166 171 public Set entrySet() { 172 return new AbstractSet() { 173 public int size() { 174 return _map.size(); 175 } 176 public boolean isEmpty() { 177 return TFloatDoubleHashMapDecorator.this.isEmpty(); 178 } 179 public boolean contains(Object o) { 180 if (o instanceof Map.Entry) { 181 Object k = ((Map.Entry)o).getKey(); 182 Object v = ((Map.Entry)o).getValue(); 183 return (TFloatDoubleHashMapDecorator.this.containsKey(k) && 184 TFloatDoubleHashMapDecorator.this.get(k).equals(v)); 185 } else { 186 return false; 187 } 188 } 189 public Iterator iterator() { 190 return new Iterator() { 191 private final TFloatDoubleIterator it = _map.iterator(); 192 193 public Object next() { 194 it.advance(); 195 final Object key = wrapKey(it.key()); 196 final Object v = wrapValue(it.value()); 197 return new Map.Entry() { 198 private Object val = v; 199 public boolean equals(Object o) { 200 return ((o instanceof Map.Entry) && 201 ((Map.Entry)o).getKey().equals(key) && 202 ((Map.Entry)o).getValue().equals(val)); 203 } 204 public Object getKey() { 205 return key; 206 } 207 public Object getValue() { 208 return val; 209 } 210 public int hashCode() { 211 return key.hashCode() + val.hashCode(); 212 } 213 public Object setValue(Object value) { 214 val = value; 215 return put(key, value); 216 } 217 }; 218 } 219 220 public boolean hasNext() { 221 return it.hasNext(); 222 } 223 224 public void remove() { 225 it.remove(); 226 } 227 }; 228 } 229 public boolean add(Object o) { 230 throw new UnsupportedOperationException(); 231 } 232 public boolean remove(Object o) { 233 throw new UnsupportedOperationException(); 234 } 235 public boolean addAll(Collection c) { 236 throw new UnsupportedOperationException(); 237 } 238 public boolean retainAll(Collection c) { 239 throw new UnsupportedOperationException(); 240 } 241 public boolean removeAll(Collection c) { 242 throw new UnsupportedOperationException(); 243 } 244 public void clear() { 245 TFloatDoubleHashMapDecorator.this.clear(); 246 } 247 }; 248 } 249 250 256 public boolean containsValue(Object val) { 257 return _map.containsValue(unwrapValue(val)); 258 } 259 260 266 public boolean containsKey(Object key) { 267 return _map.containsKey(unwrapKey(key)); 268 } 269 270 274 public int size() { 275 return this._map.size(); 276 } 277 278 282 public boolean isEmpty() { 283 return (size() == 0); 284 } 285 286 293 public void putAll(Map map) { 294 Iterator it = map.entrySet().iterator(); 295 for (int i = map.size(); i-- > 0;) { 296 Map.Entry e = (Map.Entry)it.next(); 297 this.put(e.getKey(), e.getValue()); 298 } 299 } 300 301 307 protected Float wrapKey(float k) { 308 return new Float(k); 309 } 310 311 317 protected float unwrapKey(Object key) { 318 return ((Float)key).floatValue(); 319 } 320 326 protected Double wrapValue(double k) { 327 return new Double(k); 328 } 329 330 336 protected double unwrapValue(Object value) { 337 return ((Double)value).doubleValue(); 338 } 339 340 } | Popular Tags |