1 11 package org.eclipse.core.internal.utils; 12 13 import java.util.*; 14 15 21 public class ObjectMap implements Map, IStringPoolParticipant { 22 23 protected static final int DEFAULT_SIZE = 16; 25 protected static final int GROW_SIZE = 10; 26 protected int count = 0; 27 protected Object [] elements = null; 28 29 32 public ObjectMap() { 33 this(DEFAULT_SIZE); 34 } 35 36 40 public ObjectMap(int initialCapacity) { 41 if (initialCapacity > 0) 42 elements = new Object [Math.max(initialCapacity * 2, 0)]; 43 } 44 45 50 public ObjectMap(Map map) { 51 this(map.size()); 52 putAll(map); 53 } 54 55 58 public void clear() { 59 elements = null; 60 count = 0; 61 } 62 63 66 public Object clone() { 67 return new ObjectMap(this); 68 } 69 70 73 public boolean containsKey(Object key) { 74 if (elements == null || count == 0) 75 return false; 76 for (int i = 0; i < elements.length; i = i + 2) 77 if (elements[i] != null && elements[i].equals(key)) 78 return true; 79 return false; 80 } 81 82 85 public boolean containsValue(Object value) { 86 if (elements == null || count == 0) 87 return false; 88 for (int i = 1; i < elements.length; i = i + 2) 89 if (elements[i] != null && elements[i].equals(value)) 90 return true; 91 return false; 92 } 93 94 100 public Set entrySet() { 101 return toHashMap().entrySet(); 102 } 103 104 107 public boolean equals(Object o) { 108 if (!(o instanceof Map)) 109 return false; 110 Map other = (Map) o; 111 if (count != other.size()) 113 return false; 114 115 if (!keySet().equals(other.keySet())) 117 return false; 118 119 for (int i = 0; i < elements.length; i = i + 2) { 121 if (elements[i] != null && (!elements[i + 1].equals(other.get(elements[i])))) 122 return false; 123 } 124 return true; 125 } 126 127 130 public Object get(Object key) { 131 if (elements == null || count == 0) 132 return null; 133 for (int i = 0; i < elements.length; i = i + 2) 134 if (elements[i] != null && elements[i].equals(key)) 135 return elements[i + 1]; 136 return null; 137 } 138 139 143 protected void grow() { 144 Object [] expanded = new Object [elements.length + GROW_SIZE]; 145 System.arraycopy(elements, 0, expanded, 0, elements.length); 146 elements = expanded; 147 } 148 149 152 public int hashCode() { 153 int hash = 0; 154 for (int i = 0; i < elements.length; i = i + 2) { 155 if (elements[i] != null) { 156 hash += elements[i].hashCode(); 157 } 158 } 159 return hash; 160 } 161 162 165 public boolean isEmpty() { 166 return count == 0; 167 } 168 169 175 public Set keySet() { 176 Set result = new HashSet(size()); 177 for (int i = 0; i < elements.length; i = i + 2) { 178 if (elements[i] != null) { 179 result.add(elements[i]); 180 } 181 } 182 return result; 183 } 184 185 188 public Object put(Object key, Object value) { 189 if (key == null) 190 throw new NullPointerException (); 191 if (value == null) 192 return remove(key); 193 194 if (elements == null) 196 elements = new Object [DEFAULT_SIZE]; 197 if (count == 0) { 198 elements[0] = key; 199 elements[1] = value; 200 count++; 201 return null; 202 } 203 204 int emptyIndex = -1; 205 for (int i = 0; i < elements.length; i += 2) { 207 if (elements[i] != null) { 208 if (elements[i].equals(key)) { 209 Object oldValue = elements[i + 1]; 210 elements[i + 1] = value; 211 return oldValue; 212 } 213 } else if (emptyIndex == -1) { 214 emptyIndex = i; 216 } 217 } 218 if (emptyIndex == -1) 221 emptyIndex = count * 2; 222 223 if (elements.length <= (count * 2)) 226 grow(); 227 elements[emptyIndex] = key; 228 elements[emptyIndex + 1] = value; 229 count++; 230 return null; 231 } 232 233 236 public void putAll(Map map) { 237 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 238 Object key = i.next(); 239 Object value = map.get(key); 240 put(key, value); 241 } 242 } 243 244 247 public Object remove(Object key) { 248 if (elements == null || count == 0) 249 return null; 250 for (int i = 0; i < elements.length; i = i + 2) { 251 if (elements[i] != null && elements[i].equals(key)) { 252 elements[i] = null; 253 Object result = elements[i + 1]; 254 elements[i + 1] = null; 255 count--; 256 return result; 257 } 258 } 259 return null; 260 } 261 262 265 public int size() { 266 return count; 267 } 268 269 272 public void shareStrings(StringPool set) { 273 Object [] array = elements; 275 if (array == null) 276 return; 277 for (int i = 0; i < array.length; i++) { 278 Object o = array[i]; 279 if (o instanceof String ) 280 array[i] = set.add((String ) o); 281 if (o instanceof IStringPoolParticipant) 282 ((IStringPoolParticipant) o).shareStrings(set); 283 } 284 } 285 286 289 private HashMap toHashMap() { 290 HashMap result = new HashMap(size()); 291 for (int i = 0; i < elements.length; i = i + 2) { 292 if (elements[i] != null) { 293 result.put(elements[i], elements[i + 1]); 294 } 295 } 296 return result; 297 } 298 299 305 public Collection values() { 306 Set result = new HashSet(size()); 307 for (int i = 1; i < elements.length; i = i + 2) { 308 if (elements[i] != null) { 309 result.add(elements[i]); 310 } 311 } 312 return result; 313 } 314 } 315 | Popular Tags |