1 11 package org.eclipse.core.internal.jobs; 12 13 import java.util.*; 14 15 23 public class ObjectMap implements Map { 24 protected static final int DEFAULT_SIZE = 16; 26 protected static final int GROW_SIZE = 10; 27 protected int count = 0; 28 protected Object [] elements = null; 29 30 36 public ObjectMap(int initialCapacity) { 37 if (initialCapacity > 0) 38 elements = new Object [Math.max(initialCapacity * 2, 0)]; 39 } 40 41 48 public ObjectMap(Map map) { 49 this(map.size()); 50 putAll(map); 51 } 52 53 56 public void clear() { 57 elements = null; 58 count = 0; 59 } 60 61 64 public Object clone() { 65 return new ObjectMap(this); 66 } 67 68 71 public boolean containsKey(Object key) { 72 if (elements == null || count == 0) 73 return false; 74 for (int i = 0; i < elements.length; i = i + 2) 75 if (elements[i] != null && elements[i].equals(key)) 76 return true; 77 return false; 78 } 79 80 83 public boolean containsValue(Object value) { 84 if (elements == null || count == 0) 85 return false; 86 for (int i = 1; i < elements.length; i = i + 2) 87 if (elements[i] != null && elements[i].equals(value)) 88 return true; 89 return false; 90 } 91 92 99 public Set entrySet() { 100 return toHashMap().entrySet(); 101 } 102 103 106 public boolean equals(Object o) { 107 if (!(o instanceof Map)) 108 return false; 109 Map other = (Map) o; 110 if (count != other.size()) 112 return false; 113 if (!keySet().equals(other.keySet())) 115 return false; 116 for (int i = 0; i < elements.length; i = i + 2) { 118 if (elements[i] != null && (!elements[i + 1].equals(other.get(elements[i])))) 119 return false; 120 } 121 return true; 122 } 123 124 127 public Object get(Object key) { 128 if (elements == null || count == 0) 129 return null; 130 for (int i = 0; i < elements.length; i = i + 2) 131 if (elements[i] != null && elements[i].equals(key)) 132 return elements[i + 1]; 133 return null; 134 } 135 136 140 protected void grow() { 141 Object [] expanded = new Object [elements.length + GROW_SIZE]; 142 System.arraycopy(elements, 0, expanded, 0, elements.length); 143 elements = expanded; 144 } 145 146 149 public int hashCode() { 150 int hash = 0; 151 for (int i = 0; i < elements.length; i = i + 2) { 152 if (elements[i] != null) { 153 hash += elements[i].hashCode(); 154 } 155 } 156 return hash; 157 } 158 159 162 public boolean isEmpty() { 163 return count == 0; 164 } 165 166 169 public String [] keys() { 170 String [] result = new String [count]; 171 int next = 0; 172 for (int i = 0; i < elements.length; i = i + 2) 173 if (elements[i] != null) 174 result[next++] = (String ) elements[i]; 175 return result; 176 } 177 178 185 public Set keySet() { 186 Set result = new HashSet(size()); 187 for (int i = 0; i < elements.length; i = i + 2) { 188 if (elements[i] != null) { 189 result.add(elements[i]); 190 } 191 } 192 return result; 193 } 194 195 198 public Object put(Object key, Object value) { 199 if (key == null) 200 throw new NullPointerException (); 201 if (value == null) 202 return remove(key); 203 204 if (elements == null) 206 elements = new Object [DEFAULT_SIZE]; 207 if (count == 0) { 208 elements[0] = key; 209 elements[1] = value; 210 count++; 211 return null; 212 } 213 214 int emptyIndex = -1; 215 for (int i = 0; i < elements.length; i += 2) { 217 if (elements[i] != null) { 218 if (elements[i].equals(key)) { 219 Object oldValue = elements[i + 1]; 220 elements[i + 1] = value; 221 return oldValue; 222 } 223 } else if (emptyIndex == -1) { 224 emptyIndex = i; 226 } 227 } 228 if (emptyIndex == -1) 231 emptyIndex = count * 2; 232 233 if (elements.length <= (count * 2)) 236 grow(); 237 elements[emptyIndex] = key; 238 elements[emptyIndex + 1] = value; 239 count++; 240 return null; 241 } 242 243 246 public void putAll(Map map) { 247 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 248 Object key = i.next(); 249 Object value = map.get(key); 250 put(key, value); 251 } 252 } 253 254 257 public Object remove(Object key) { 258 if (elements == null || count == 0) 259 return null; 260 for (int i = 0; i < elements.length; i = i + 2) { 261 if (elements[i] != null && elements[i].equals(key)) { 262 elements[i] = null; 263 Object result = elements[i + 1]; 264 elements[i + 1] = null; 265 count--; 266 return result; 267 } 268 } 269 return null; 270 } 271 272 275 public void shareStrings(StringPool set) { 276 Object [] array = elements; 278 if (array == null) 279 return; 280 for (int i = 0; i < array.length; i++) { 281 Object o = array[i]; 282 if (o instanceof String ) 283 array[i] = set.add((String ) o); 284 } 285 } 286 287 290 public int size() { 291 return count; 292 } 293 294 297 private HashMap toHashMap() { 298 HashMap result = new HashMap(size()); 299 for (int i = 0; i < elements.length; i = i + 2) { 300 if (elements[i] != null) { 301 result.put(elements[i], elements[i + 1]); 302 } 303 } 304 return result; 305 } 306 307 314 public Collection values() { 315 Set result = new HashSet(size()); 316 for (int i = 1; i < elements.length; i = i + 2) { 317 if (elements[i] != null) { 318 result.add(elements[i]); 319 } 320 } 321 return result; 322 } 323 } | Popular Tags |