This class implements a special purpose hashtable. It works like a
normal
The overall result is that it's less expensive to use these in
performance-critical locations, in terms both of CPU and memory,
than java.util.Hashtable instances. In this package
it makes a significant difference when normalizing attributes,
which is done for each start-element construct.
| Method from org.apache.tomcat.util.collections.SimpleHashtable Detail: |
public void clear() {
count = 0;
currentBucket = 0;
current = null;
for (int i = 0; i < table.length; i++)
table [i] = null;
}
|
public Object get(String key) {
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key))
return e.value;
}
return null;
}
Returns the value to which the specified key is mapped in this
hashtable ... the key isn't necessarily interned, though. |
public Object getInterned(String key) {
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && (e.key == key))
return e.value;
}
return null;
}
Returns the value to which the specified key is mapped in this hashtable. |
public boolean hasMoreElements() {
if (current != null)
return true;
while (currentBucket < table.length) {
current = table [currentBucket++];
if (current != null)
return true;
}
return false;
}
Used to view this as an enumeration; returns true if there
are more keys to be enumerated. |
public Enumeration keys() {
currentBucket = 0;
current = null;
hasMoreElements();
return this;
}
Returns an enumeration of the keys in this hashtable. |
public Object nextElement() {
Object retval;
if (current == null)
throw new IllegalStateException ();
retval = current.key;
current = current.next;
// Advance to the next position ( we may call next after next,
// without hasMore )
hasMoreElements();
return retval;
}
Used to view this as an enumeration; returns the next key
in the enumeration. |
public Object put(Object key,
Object value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index] ; e != null ; e = e.next) {
// if ((e.hash == hash) && e.key.equals(key)) {
if ((e.hash == hash) && (e.key == key)) {
Object old = e.value;
e.value = value;
return old;
}
}
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
Entry e = new Entry(hash, key, value, tab[index]);
tab[index] = e;
count++;
return null;
}
|
public Object remove(Object key) {
Entry tab[] = table;
Entry prev=null;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
if( dL > 0 ) d("Idx " + index + " " + tab[index] );
for (Entry e = tab[index] ; e != null ; prev=e, e = e.next) {
if( dL > 0 ) d(" > " + prev + " " + e.next + " " + e + " " + e.key);
if ((e.hash == hash) && e.key.equals(key)) {
if( prev!=null ) {
prev.next=e.next;
} else {
tab[index]=e.next;
}
if( dL > 0 ) d("Removing from list " + tab[index] + " " + prev +
" " + e.value);
count--;
Object res=e.value;
e.value=null;
return res;
}
}
return null;
}
|
public int size() {
return count;
}
Returns the number of keys in this hashtable. |