diff options
author | Jochen Hoenicke <jochen@gnu.org> | 1999-07-22 12:51:13 +0000 |
---|---|---|
committer | Jochen Hoenicke <jochen@gnu.org> | 1999-07-22 12:51:13 +0000 |
commit | cd3269058d8dac0555265fe6587d8a4fa90b21ac (patch) | |
tree | 6eab9f299862e838e2c4e22c3abeaaf6a33e3f73 /java/util/WeakHashMap.java | |
parent | 3b61257c1a582b8c1aaa0304776cb323b685c019 (diff) | |
download | classpath-cd3269058d8dac0555265fe6587d8a4fa90b21ac.tar.gz |
Make sure that hashed slot number is not negative.
Diffstat (limited to 'java/util/WeakHashMap.java')
-rw-r--r-- | java/util/WeakHashMap.java | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/java/util/WeakHashMap.java b/java/util/WeakHashMap.java index 1ed150c27..8ae69b40d 100644 --- a/java/util/WeakHashMap.java +++ b/java/util/WeakHashMap.java @@ -314,7 +314,7 @@ public class WeakHashMap extends AbstractMap implements Map /** * The slot of this entry. This should be * <pre> - * key.hashCode() % buckets.length + * Math.abs(key.hashCode() % buckets.length) * </pre> * But since the key may be silently removed we have to remember * the slot number. @@ -425,7 +425,7 @@ public class WeakHashMap extends AbstractMap implements Map */ Entry getEntry() { - final Object key = get(); + final Object key = this.get(); if (key == null) return null; return new Entry(key); @@ -478,6 +478,14 @@ public class WeakHashMap extends AbstractMap implements Map buckets = new WeakBucket[initialCapacity]; } + /** + * simply hashes a non-null Object to its array index + */ + private int hash(Object key) + { + return Math.abs(key.hashCode() % buckets.length); + } + /** * Cleans the reference queue. This will poll all references (which * are WeakBuckets) from the queue and remove them from this map. @@ -532,7 +540,7 @@ public class WeakHashMap extends AbstractMap implements Map else { /* add this bucket to its new slot */ - int slot = key.hashCode() % buckets.length; + int slot = hash(key); bucket.slot = slot; bucket.next = buckets[slot]; buckets[slot] = bucket; @@ -552,7 +560,7 @@ public class WeakHashMap extends AbstractMap implements Map { if (key == null) key = NULL_KEY; - int slot = key.hashCode() % buckets.length; + int slot = hash(key); WeakBucket bucket = buckets[slot]; while (bucket != null) { @@ -574,7 +582,7 @@ public class WeakHashMap extends AbstractMap implements Map { if (key == null) key = NULL_KEY; - int slot = key.hashCode() % buckets.length; + int slot = hash(key); WeakBucket bucket = new WeakBucket(key, value, slot); bucket.next = buckets[slot]; buckets[slot] = bucket; |