1 17 18 package org.apache.tomcat.util.collections; 19 20 import java.util.Enumeration ; 21 22 23 27 28 31 32 59 public final class SimpleHashtable implements Enumeration 60 { 61 62 private static org.apache.commons.logging.Log log= 63 org.apache.commons.logging.LogFactory.getLog( SimpleHashtable.class ); 64 65 private Entry table[]; 67 68 private Entry current = null; 70 private int currentBucket = 0; 71 72 private int count; 74 private int threshold; 75 76 private static final float loadFactor = 0.75f; 77 78 79 85 public SimpleHashtable(int initialCapacity) { 86 if (initialCapacity < 0) 87 throw new IllegalArgumentException ("Illegal Capacity: "+ 88 initialCapacity); 89 if (initialCapacity==0) 90 initialCapacity = 1; 91 table = new Entry[initialCapacity]; 92 threshold = (int)(initialCapacity * loadFactor); 93 } 94 95 98 public SimpleHashtable() { 99 this(11); 100 } 101 102 104 public void clear () 105 { 106 count = 0; 107 currentBucket = 0; 108 current = null; 109 for (int i = 0; i < table.length; i++) 110 table [i] = null; 111 } 112 113 118 public int size() { 119 return count; 120 } 121 122 128 public Enumeration keys() { 129 currentBucket = 0; 130 current = null; 131 hasMoreElements(); 132 return this; 133 } 134 135 139 public boolean hasMoreElements () 140 { 141 if (current != null) 142 return true; 143 while (currentBucket < table.length) { 144 current = table [currentBucket++]; 145 if (current != null) 146 return true; 147 } 148 return false; 149 } 150 151 155 public Object nextElement () 156 { 157 Object retval; 158 159 if (current == null) 160 throw new IllegalStateException (); 161 retval = current.key; 162 current = current.next; 163 hasMoreElements(); 166 return retval; 167 } 168 169 170 173 public Object getInterned (String key) { 174 Entry tab[] = table; 175 int hash = key.hashCode(); 176 int index = (hash & 0x7FFFFFFF) % tab.length; 177 for (Entry e = tab[index] ; e != null ; e = e.next) { 178 if ((e.hash == hash) && (e.key == key)) 179 return e.value; 180 } 181 return null; 182 } 183 184 188 public Object get(String key) { 189 Entry tab[] = table; 190 int hash = key.hashCode(); 191 int index = (hash & 0x7FFFFFFF) % tab.length; 192 for (Entry e = tab[index] ; e != null ; e = e.next) { 193 if ((e.hash == hash) && e.key.equals(key)) 194 return e.value; 195 } 196 return null; 197 } 198 199 206 private void rehash() { 207 int oldCapacity = table.length; 208 Entry oldMap[] = table; 209 210 int newCapacity = oldCapacity * 2 + 1; 211 Entry newMap[] = new Entry[newCapacity]; 212 213 threshold = (int)(newCapacity * loadFactor); 214 table = newMap; 215 216 222 223 for (int i = oldCapacity ; i-- > 0 ;) { 224 for (Entry old = oldMap[i] ; old != null ; ) { 225 Entry e = old; 226 old = old.next; 227 228 int index = (e.hash & 0x7FFFFFFF) % newCapacity; 229 e.next = newMap[index]; 230 newMap[index] = e; 231 } 232 } 233 } 234 235 243 public Object put(Object key, Object value) { 244 if (value == null) { 246 throw new NullPointerException (); 247 } 248 249 Entry tab[] = table; 251 int hash = key.hashCode(); 252 int index = (hash & 0x7FFFFFFF) % tab.length; 253 for (Entry e = tab[index] ; e != null ; e = e.next) { 254 if ((e.hash == hash) && (e.key == key)) { 256 Object old = e.value; 257 e.value = value; 258 return old; 259 } 260 } 261 262 if (count >= threshold) { 263 rehash(); 265 266 tab = table; 267 index = (hash & 0x7FFFFFFF) % tab.length; 268 } 269 270 Entry e = new Entry(hash, key, value, tab[index]); 272 tab[index] = e; 273 count++; 274 return null; 275 } 276 277 public Object remove(Object key) { 278 Entry tab[] = table; 279 Entry prev=null; 280 int hash = key.hashCode(); 281 int index = (hash & 0x7FFFFFFF) % tab.length; 282 if( dL > 0 ) d("Idx " + index + " " + tab[index] ); 283 for (Entry e = tab[index] ; e != null ; prev=e, e = e.next) { 284 if( dL > 0 ) d("> " + prev + " " + e.next + " " + e + " " + e.key); 285 if ((e.hash == hash) && e.key.equals(key)) { 286 if( prev!=null ) { 287 prev.next=e.next; 288 } else { 289 tab[index]=e.next; 290 } 291 if( dL > 0 ) d("Removing from list " + tab[index] + " " + prev + 292 " " + e.value); 293 count--; 294 Object res=e.value; 295 e.value=null; 296 return res; 297 } 298 } 299 return null; 300 } 301 302 305 private static class Entry { 306 int hash; 307 Object key; 308 Object value; 309 Entry next; 310 311 protected Entry(int hash, Object key, Object value, Entry next) { 312 this.hash = hash; 313 this.key = key; 314 this.value = value; 315 this.next = next; 316 } 317 } 318 319 private static final int dL=0; 320 private void d(String s ) { 321 if (log.isDebugEnabled()) 322 log.debug( "SimpleHashtable: " + s ); 323 } 324 } 325 | Popular Tags |