1 11 package org.eclipse.core.internal.runtime; 12 13 import java.util.*; 14 import org.eclipse.core.internal.preferences.StringPool; 15 16 24 public class ObjectMap implements Map { 25 protected static final int DEFAULT_SIZE = 16; 27 protected static final int GROW_SIZE = 10; 28 protected int count = 0; 29 protected Object [] elements = null; 30 31 34 public ObjectMap() { 35 this(DEFAULT_SIZE); 36 } 37 38 44 public ObjectMap(int initialCapacity) { 45 elements = new Object [Math.max(initialCapacity * 2, 0)]; 46 } 47 48 55 public ObjectMap(Map map) { 56 this(map.size()); 57 putAll(map); 58 } 59 60 63 public void clear() { 64 elements = null; 65 count = 0; 66 } 67 68 71 public Object clone() { 72 return new ObjectMap(this); 73 } 74 75 78 public boolean containsKey(Object key) { 79 if (elements == null || count == 0) 80 return false; 81 for (int i = 0; i < elements.length; i = i + 2) 82 if (elements[i] != null && elements[i].equals(key)) 83 return true; 84 return false; 85 } 86 87 90 public boolean containsValue(Object value) { 91 if (elements == null || count == 0) 92 return false; 93 for (int i = 1; i < elements.length; i = i + 2) 94 if (elements[i] != null && elements[i].equals(value)) 95 return true; 96 return false; 97 } 98 99 106 public Set entrySet() { 107 return toHashMap().entrySet(); 108 } 109 110 113 public boolean equals(Object o) { 114 if (!(o instanceof Map)) 115 return false; 116 Map other = (Map) o; 117 if (count != other.size()) 119 return false; 120 if (!keySet().equals(other.keySet())) 122 return false; 123 for (int i = 0; i < elements.length; i = i + 2) { 125 if (elements[i] != null && (!elements[i + 1].equals(other.get(elements[i])))) 126 return false; 127 } 128 return true; 129 } 130 131 134 public Object get(Object key) { 135 if (elements == null || count == 0) 136 return null; 137 for (int i = 0; i < elements.length; i = i + 2) 138 if (elements[i] != null && elements[i].equals(key)) 139 return elements[i + 1]; 140 return null; 141 } 142 143 147 protected void grow() { 148 Object [] expanded = new Object [elements.length + GROW_SIZE]; 149 System.arraycopy(elements, 0, expanded, 0, elements.length); 150 elements = expanded; 151 } 152 153 156 public int hashCode() { 157 int hash = 0; 158 for (int i = 0; i < elements.length; i = i + 2) { 159 if (elements[i] != null) { 160 hash += elements[i].hashCode(); 161 } 162 } 163 return hash; 164 } 165 166 169 public boolean isEmpty() { 170 return count == 0; 171 } 172 173 176 public String [] keys() { 177 String [] result = new String [count]; 178 int next = 0; 179 for (int i = 0; i < elements.length; i = i + 2) 180 if (elements[i] != null) 181 result[next++] = (String ) elements[i]; 182 return result; 183 } 184 185 192 public Set keySet() { 193 Set result = new HashSet(size()); 194 for (int i = 0; i < elements.length; i = i + 2) { 195 if (elements[i] != null) { 196 result.add(elements[i]); 197 } 198 } 199 return result; 200 } 201 202 205 public Object put(Object key, Object value) { 206 if (key == null) 207 throw new NullPointerException (); 208 if (value == null) 209 return remove(key); 210 211 if (elements == null) 213 elements = new Object [DEFAULT_SIZE]; 214 if (count == 0) { 215 elements[0] = key; 216 elements[1] = value; 217 count++; 218 return null; 219 } 220 221 int emptyIndex = -1; 222 for (int i = 0; i < elements.length; i += 2) { 224 if (elements[i] != null) { 225 if (elements[i].equals(key)) { 226 Object oldValue = elements[i + 1]; 227 elements[i + 1] = value; 228 return oldValue; 229 } 230 } else if (emptyIndex == -1) { 231 emptyIndex = i; 233 } 234 } 235 if (emptyIndex == -1) 238 emptyIndex = count * 2; 239 240 if (elements.length <= (count * 2)) 243 grow(); 244 elements[emptyIndex] = key; 245 elements[emptyIndex + 1] = value; 246 count++; 247 return null; 248 } 249 250 253 public void putAll(Map map) { 254 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 255 Object key = i.next(); 256 Object value = map.get(key); 257 put(key, value); 258 } 259 } 260 261 264 public Object remove(Object key) { 265 if (elements == null || count == 0) 266 return null; 267 for (int i = 0; i < elements.length; i = i + 2) { 268 if (elements[i] != null && elements[i].equals(key)) { 269 elements[i] = null; 270 Object result = elements[i + 1]; 271 elements[i + 1] = null; 272 count--; 273 return result; 274 } 275 } 276 return null; 277 } 278 279 282 public void shareStrings(StringPool set) { 283 Object [] array = elements; 285 if (array == null) 286 return; 287 for (int i = 0; i < array.length; i++) { 288 Object o = array[i]; 289 if (o instanceof String ) 290 array[i] = set.add((String ) o); 291 } 292 } 293 294 297 public int size() { 298 return count; 299 } 300 301 304 private HashMap toHashMap() { 305 HashMap result = new HashMap(size()); 306 for (int i = 0; i < elements.length; i = i + 2) { 307 if (elements[i] != null) { 308 result.put(elements[i], elements[i + 1]); 309 } 310 } 311 return result; 312 } 313 314 321 public Collection values() { 322 Set result = new HashSet(size()); 323 for (int i = 1; i < elements.length; i = i + 2) { 324 if (elements[i] != null) { 325 result.add(elements[i]); 326 } 327 } 328 return result; 329 } 330 } | Popular Tags |