1 11 package org.eclipse.core.internal.resources; 12 13 import java.util.*; 14 import org.eclipse.core.internal.utils.IStringPoolParticipant; 15 import org.eclipse.core.internal.utils.StringPool; 16 17 24 public class MarkerAttributeMap implements Map, IStringPoolParticipant { 25 protected Object [] elements = null; 26 protected int count = 0; 27 28 protected static final int DEFAULT_SIZE = 16; 30 protected static final int GROW_SIZE = 10; 31 32 35 public MarkerAttributeMap() { 36 super(); 37 } 38 39 43 public MarkerAttributeMap(int initialCapacity) { 44 elements = new Object [Math.max(initialCapacity * 2, 0)]; 45 } 46 47 51 public MarkerAttributeMap(Map map) { 52 this(map.size()); 53 putAll(map); 54 } 55 56 59 public void clear() { 60 elements = null; 61 count = 0; 62 } 63 64 67 public boolean containsKey(Object key) { 68 key = ((String ) key).intern(); 69 if (elements == null || count == 0) 70 return false; 71 for (int i = 0; i < elements.length; i = i + 2) 72 if (elements[i] == key) 73 return true; 74 return false; 75 } 76 77 80 public boolean containsValue(Object value) { 81 if (elements == null || count == 0) 82 return false; 83 for (int i = 1; i < elements.length; i = i + 2) 84 if (elements[i] != null && elements[i].equals(value)) 85 return true; 86 return false; 87 } 88 89 95 public Set entrySet() { 96 return toHashMap().entrySet(); 97 } 98 99 102 public boolean equals(Object o) { 103 if (!(o instanceof Map)) 104 return false; 105 Map other = (Map) o; 106 if (count != other.size()) 108 return false; 109 110 if (!keySet().equals(other.keySet())) 112 return false; 113 114 for (int i = 0; i < elements.length; i = i + 2) { 116 if (elements[i] != null && (!elements[i + 1].equals(other.get(elements[i])))) 117 return false; 118 } 119 return true; 120 } 121 122 125 public Object get(Object key) { 126 key = ((String ) key).intern(); 127 if (elements == null || count == 0) 128 return null; 129 for (int i = 0; i < elements.length; i = i + 2) 130 if (elements[i] == key) 131 return elements[i + 1]; 132 return null; 133 } 134 135 139 protected void grow() { 140 Object [] expanded = new Object [elements.length + GROW_SIZE]; 141 System.arraycopy(elements, 0, expanded, 0, elements.length); 142 elements = expanded; 143 } 144 145 148 public int hashCode() { 149 int hash = 0; 150 for (int i = 0; i < elements.length; i = i + 2) { 151 if (elements[i] != null) { 152 hash += elements[i].hashCode(); 153 } 154 } 155 return hash; 156 } 157 158 161 public boolean isEmpty() { 162 return count == 0; 163 } 164 165 171 public Set keySet() { 172 Set result = new HashSet(size()); 173 for (int i = 0; i < elements.length; i = i + 2) { 174 if (elements[i] != null) { 175 result.add(elements[i]); 176 } 177 } 178 return result; 179 } 180 181 184 public Object put(Object key, Object value) { 185 if (key == null) 186 throw new NullPointerException (); 187 if (value == null) 188 return remove(key); 189 key = ((String ) key).intern(); 190 191 if (elements == null) 193 elements = new Object [DEFAULT_SIZE]; 194 if (count == 0) { 195 elements[0] = key; 196 elements[1] = value; 197 count++; 198 return null; 199 } 200 201 for (int i = 0; i < elements.length; i = i + 2) { 203 if (elements[i] == key) { 204 Object oldValue = elements[i + 1]; 205 elements[i + 1] = value; 206 return oldValue; 207 } 208 } 209 210 if (elements.length <= (count * 2)) 213 grow(); 214 for (int i = 0; i < elements.length; i = i + 2) { 215 if (elements[i] == null) { 216 elements[i] = key; 217 elements[i + 1] = value; 218 count++; 219 return null; 220 } 221 } 222 return null; 223 } 224 225 228 public void putAll(Map map) { 229 for (Iterator i = map.keySet().iterator(); i.hasNext();) { 230 Object key = i.next(); 231 Object value = map.get(key); 232 put(key, value); 233 } 234 } 235 236 239 public Object remove(Object key) { 240 key = ((String ) key).intern(); 241 if (elements == null || count == 0) 242 return null; 243 for (int i = 0; i < elements.length; i = i + 2) { 244 if (elements[i] == key) { 245 elements[i] = null; 246 Object result = elements[i + 1]; 247 elements[i + 1] = null; 248 count--; 249 return result; 250 } 251 } 252 return null; 253 } 254 255 258 public int size() { 259 return count; 260 } 261 262 265 public void shareStrings(StringPool set) { 266 Object [] array = elements; 268 if (array == null) 269 return; 270 for (int i = 1; i < array.length; i = i + 2) { 272 Object o = array[i]; 273 if (o instanceof String ) 274 array[i] = set.add((String )o); 275 else if (o instanceof IStringPoolParticipant) 276 ((IStringPoolParticipant)o).shareStrings(set); 277 } 278 } 279 280 283 private HashMap toHashMap() { 284 HashMap result = new HashMap(size()); 285 for (int i = 0; i < elements.length; i = i + 2) { 286 if (elements[i] != null) { 287 result.put(elements[i], elements[i + 1]); 288 } 289 } 290 return result; 291 } 292 293 299 public Collection values() { 300 Set result = new HashSet(size()); 301 for (int i = 1; i < elements.length; i = i + 2) { 302 if (elements[i] != null) { 303 result.add(elements[i]); 304 } 305 } 306 return result; 307 } 308 } 309 | Popular Tags |