1 7 8 16 17 package com.sun.corba.se.impl.util; 18 19 import java.util.Dictionary ; 20 import java.util.Enumeration ; 21 import java.util.NoSuchElementException ; 22 23 29 32 class IdentityHashtableEntry { 33 int hash; 34 Object key; 35 Object value; 36 IdentityHashtableEntry next; 37 } 38 39 public final class IdentityHashtable extends Dictionary { 40 43 private transient IdentityHashtableEntry table[]; 44 45 48 private transient int count; 49 50 53 private int threshold; 54 55 58 private float loadFactor; 59 60 71 public IdentityHashtable(int initialCapacity, float loadFactor) { 72 if ((initialCapacity <= 0) || (loadFactor <= 0.0)) { 73 throw new IllegalArgumentException (); 74 } 75 this.loadFactor = loadFactor; 76 table = new IdentityHashtableEntry[initialCapacity]; 77 threshold = (int)(initialCapacity * loadFactor); 78 } 79 80 87 public IdentityHashtable(int initialCapacity) { 88 this(initialCapacity, 0.75f); 89 } 90 91 97 public IdentityHashtable() { 98 this(101, 0.75f); 99 } 100 101 107 public int size() { 108 return count; 109 } 110 111 118 public boolean isEmpty() { 119 return count == 0; 120 } 121 122 130 public Enumeration keys() { 131 return new IdentityHashtableEnumerator(table, true); 132 } 133 134 144 public Enumeration elements() { 145 return new IdentityHashtableEnumerator(table, false); 146 } 147 148 161 public boolean contains(Object value) { 162 if (value == null) { 163 throw new NullPointerException (); 164 } 165 166 IdentityHashtableEntry tab[] = table; 167 for (int i = tab.length ; i-- > 0 ;) { 168 for (IdentityHashtableEntry e = tab[i] ; e != null ; e = e.next) { 169 if (e.value == value) { 170 return true; 171 } 172 } 173 } 174 return false; 175 } 176 177 186 public boolean containsKey(Object key) { 187 IdentityHashtableEntry tab[] = table; 188 int hash = System.identityHashCode(key); 189 int index = (hash & 0x7FFFFFFF) % tab.length; 190 for (IdentityHashtableEntry e = tab[index] ; e != null ; e = e.next) { 191 if ((e.hash == hash) && e.key == key) { 192 return true; 193 } 194 } 195 return false; 196 } 197 198 208 public Object get(Object key) { 209 IdentityHashtableEntry tab[] = table; 210 int hash = System.identityHashCode(key); 211 int index = (hash & 0x7FFFFFFF) % tab.length; 212 for (IdentityHashtableEntry e = tab[index] ; e != null ; e = e.next) { 213 if ((e.hash == hash) && e.key == key) { 214 return e.value; 215 } 216 } 217 return null; 218 } 219 220 228 protected void rehash() { 229 int oldCapacity = table.length; 230 IdentityHashtableEntry oldTable[] = table; 231 232 int newCapacity = oldCapacity * 2 + 1; 233 IdentityHashtableEntry newTable[] = new IdentityHashtableEntry[newCapacity]; 234 235 threshold = (int)(newCapacity * loadFactor); 236 table = newTable; 237 238 240 for (int i = oldCapacity ; i-- > 0 ;) { 241 for (IdentityHashtableEntry old = oldTable[i] ; old != null ; ) { 242 IdentityHashtableEntry e = old; 243 old = old.next; 244 245 int index = (e.hash & 0x7FFFFFFF) % newCapacity; 246 e.next = newTable[index]; 247 newTable[index] = e; 248 } 249 } 250 } 251 252 269 public Object put(Object key, Object value) { 270 if (value == null) { 272 throw new NullPointerException (); 273 } 274 275 IdentityHashtableEntry tab[] = table; 277 int hash = System.identityHashCode(key); 278 int index = (hash & 0x7FFFFFFF) % tab.length; 279 for (IdentityHashtableEntry e = tab[index] ; e != null ; e = e.next) { 280 if ((e.hash == hash) && e.key == key) { 281 Object old = e.value; 282 e.value = value; 283 return old; 284 } 285 } 286 287 if (count >= threshold) { 288 rehash(); 290 return put(key, value); 291 } 292 293 IdentityHashtableEntry e = new IdentityHashtableEntry(); 295 e.hash = hash; 296 e.key = key; 297 e.value = value; 298 e.next = tab[index]; 299 tab[index] = e; 300 count++; 301 return null; 302 } 303 304 313 public Object remove(Object key) { 314 IdentityHashtableEntry tab[] = table; 315 int hash = System.identityHashCode(key); 316 int index = (hash & 0x7FFFFFFF) % tab.length; 317 for (IdentityHashtableEntry e = tab[index], prev = null ; e != null ; prev = e, e = e.next) { 318 if ((e.hash == hash) && e.key == key) { 319 if (prev != null) { 320 prev.next = e.next; 321 } else { 322 tab[index] = e.next; 323 } 324 count--; 325 return e.value; 326 } 327 } 328 return null; 329 } 330 331 336 public void clear() { 337 IdentityHashtableEntry tab[] = table; 338 for (int index = tab.length; --index >= 0; ) 339 tab[index] = null; 340 count = 0; 341 } 342 343 349 public String toString() { 350 int max = size() - 1; 351 StringBuffer buf = new StringBuffer (); 352 Enumeration k = keys(); 353 Enumeration e = elements(); 354 buf.append("{"); 355 356 for (int i = 0; i <= max; i++) { 357 String s1 = k.nextElement().toString(); 358 String s2 = e.nextElement().toString(); 359 buf.append(s1 + "=" + s2); 360 if (i < max) { 361 buf.append(", "); 362 } 363 } 364 buf.append("}"); 365 return buf.toString(); 366 } 367 } 368 | Popular Tags |