From 65bf3316cf384588453604be6b4f0ed3751a8b0f Mon Sep 17 00:00:00 2001 From: tromey Date: Tue, 9 Jan 2007 19:58:05 +0000 Subject: Merged gcj-eclipse branch to trunk. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@120621 138bc75d-0d04-0410-961f-82ee72b054a4 --- libjava/classpath/java/util/HashMap.java | 138 ++++++++++++++++--------------- 1 file changed, 71 insertions(+), 67 deletions(-) (limited to 'libjava/classpath/java/util/HashMap.java') diff --git a/libjava/classpath/java/util/HashMap.java b/libjava/classpath/java/util/HashMap.java index a734af48405..92022a7d55e 100644 --- a/libjava/classpath/java/util/HashMap.java +++ b/libjava/classpath/java/util/HashMap.java @@ -96,8 +96,8 @@ import java.io.Serializable; * @since 1.2 * @status updated to 1.4 */ -public class HashMap extends AbstractMap - implements Map, Cloneable, Serializable +public class HashMap extends AbstractMap + implements Map, Cloneable, Serializable { /** * Default number of buckets. This is the value the JDK 1.3 uses. Some @@ -136,7 +136,7 @@ public class HashMap extends AbstractMap * Array containing the actual key-value mappings. * Package visible for use by nested and subclasses. */ - transient HashEntry[] buckets; + transient HashEntry[] buckets; /** * Counts the number of modifications this HashMap has undergone, used @@ -154,7 +154,7 @@ public class HashMap extends AbstractMap /** * The cache for {@link #entrySet()}. */ - private transient Set entries; + private transient Set> entries; /** * Class to represent an entry in the hash table. Holds a single key-value @@ -162,19 +162,19 @@ public class HashMap extends AbstractMap * * @author Eric Blake (ebb9@email.byu.edu) */ - static class HashEntry extends AbstractMap.BasicMapEntry + static class HashEntry extends AbstractMap.SimpleEntry { /** * The next entry in the linked list. Package visible for use by subclass. */ - HashEntry next; + HashEntry next; /** * Simple constructor. * @param key the key * @param value the value */ - HashEntry(Object key, Object value) + HashEntry(K key, V value) { super(key, value); } @@ -194,7 +194,7 @@ public class HashMap extends AbstractMap * * @return the value of this key as it is removed */ - Object cleanup() + V cleanup() { return value; } @@ -220,7 +220,7 @@ public class HashMap extends AbstractMap * NOTE: key / value pairs are not cloned in this constructor. * @throws NullPointerException if m is null */ - public HashMap(Map m) + public HashMap(Map m) { this(Math.max(m.size() * 2, DEFAULT_CAPACITY), DEFAULT_LOAD_FACTOR); putAll(m); @@ -256,7 +256,7 @@ public class HashMap extends AbstractMap if (initialCapacity == 0) initialCapacity = 1; - buckets = new HashEntry[initialCapacity]; + buckets = (HashEntry[]) new HashEntry[initialCapacity]; this.loadFactor = loadFactor; threshold = (int) (initialCapacity * loadFactor); } @@ -292,10 +292,10 @@ public class HashMap extends AbstractMap * @see #put(Object, Object) * @see #containsKey(Object) */ - public Object get(Object key) + public V get(Object key) { int idx = hash(key); - HashEntry e = buckets[idx]; + HashEntry e = buckets[idx]; while (e != null) { if (equals(key, e.key)) @@ -316,7 +316,7 @@ public class HashMap extends AbstractMap public boolean containsKey(Object key) { int idx = hash(key); - HashEntry e = buckets[idx]; + HashEntry e = buckets[idx]; while (e != null) { if (equals(key, e.key)) @@ -339,17 +339,17 @@ public class HashMap extends AbstractMap * @see #get(Object) * @see Object#equals(Object) */ - public Object put(Object key, Object value) + public V put(K key, V value) { int idx = hash(key); - HashEntry e = buckets[idx]; + HashEntry e = buckets[idx]; while (e != null) { if (equals(key, e.key)) { e.access(); // Must call this for bookkeeping in LinkedHashMap. - Object r = e.value; + V r = e.value; e.value = value; return r; } @@ -378,23 +378,25 @@ public class HashMap extends AbstractMap * * @param m the map to be hashed into this */ - public void putAll(Map m) + public void putAll(Map m) { - Iterator itr = m.entrySet().iterator(); - while (itr.hasNext()) + Map addMap; + + addMap = (Map) m; + for (Map.Entry e : addMap.entrySet()) { - Map.Entry e = (Map.Entry) itr.next(); // Optimize in case the Entry is one of our own. - if (e instanceof AbstractMap.BasicMapEntry) + if (e instanceof AbstractMap.SimpleEntry) { - AbstractMap.BasicMapEntry entry = (AbstractMap.BasicMapEntry) e; + AbstractMap.SimpleEntry entry + = (AbstractMap.SimpleEntry) e; put(entry.key, entry.value); } else put(e.getKey(), e.getValue()); } } - + /** * Removes from the HashMap and returns the value which is mapped by the * supplied key. If the key maps to nothing, then the HashMap remains @@ -405,11 +407,11 @@ public class HashMap extends AbstractMap * @param key the key used to locate the value to remove * @return whatever the key mapped to, if present */ - public Object remove(Object key) + public V remove(Object key) { int idx = hash(key); - HashEntry e = buckets[idx]; - HashEntry last = null; + HashEntry e = buckets[idx]; + HashEntry last = null; while (e != null) { @@ -455,7 +457,7 @@ public class HashMap extends AbstractMap { for (int i = buckets.length - 1; i >= 0; i--) { - HashEntry e = buckets[i]; + HashEntry e = buckets[i]; while (e != null) { if (equals(value, e.value)) @@ -474,16 +476,16 @@ public class HashMap extends AbstractMap */ public Object clone() { - HashMap copy = null; + HashMap copy = null; try { - copy = (HashMap) super.clone(); + copy = (HashMap) super.clone(); } catch (CloneNotSupportedException x) { // This is impossible. } - copy.buckets = new HashEntry[buckets.length]; + copy.buckets = (HashEntry[]) new HashEntry[buckets.length]; copy.putAllInternal(this); // Clear the entry cache. AbstractMap.clone() does the others. copy.entries = null; @@ -499,19 +501,19 @@ public class HashMap extends AbstractMap * @see #values() * @see #entrySet() */ - public Set keySet() + public Set keySet() { if (keys == null) // Create an AbstractSet with custom implementations of those methods // that can be overridden easily and efficiently. - keys = new AbstractSet() + keys = new AbstractSet() { public int size() { return size; } - public Iterator iterator() + public Iterator iterator() { // Cannot create the iterator directly, because of LinkedHashMap. return HashMap.this.iterator(KEYS); @@ -550,19 +552,19 @@ public class HashMap extends AbstractMap * @see #keySet() * @see #entrySet() */ - public Collection values() + public Collection values() { if (values == null) // We don't bother overriding many of the optional methods, as doing so // wouldn't provide any significant performance advantage. - values = new AbstractCollection() + values = new AbstractCollection() { public int size() { return size; } - public Iterator iterator() + public Iterator iterator() { // Cannot create the iterator directly, because of LinkedHashMap. return HashMap.this.iterator(VALUES); @@ -589,19 +591,19 @@ public class HashMap extends AbstractMap * @see #values() * @see Map.Entry */ - public Set entrySet() + public Set> entrySet() { if (entries == null) // Create an AbstractSet with custom implementations of those methods // that can be overridden easily and efficiently. - entries = new AbstractSet() + entries = new AbstractSet>() { public int size() { return size; } - public Iterator iterator() + public Iterator> iterator() { // Cannot create the iterator directly, because of LinkedHashMap. return HashMap.this.iterator(ENTRIES); @@ -619,7 +621,7 @@ public class HashMap extends AbstractMap public boolean remove(Object o) { - HashEntry e = getEntry(o); + HashEntry e = getEntry(o); if (e != null) { HashMap.this.remove(e.key); @@ -641,9 +643,9 @@ public class HashMap extends AbstractMap * @param callRemove whether to call the removeEldestEntry method * @see #put(Object, Object) */ - void addEntry(Object key, Object value, int idx, boolean callRemove) + void addEntry(K key, V value, int idx, boolean callRemove) { - HashEntry e = new HashEntry(key, value); + HashEntry e = new HashEntry(key, value); e.next = buckets[idx]; buckets[idx] = e; } @@ -657,14 +659,14 @@ public class HashMap extends AbstractMap * @see #entrySet() */ // Package visible, for use in nested classes. - final HashEntry getEntry(Object o) + final HashEntry getEntry(Object o) { if (! (o instanceof Map.Entry)) return null; - Map.Entry me = (Map.Entry) o; - Object key = me.getKey(); + Map.Entry me = (Map.Entry) o; + K key = me.getKey(); int idx = hash(key); - HashEntry e = buckets[idx]; + HashEntry e = buckets[idx]; while (e != null) { if (equals(e.key, key)) @@ -693,9 +695,10 @@ public class HashMap extends AbstractMap * @param type {@link #KEYS}, {@link #VALUES}, or {@link #ENTRIES} * @return the appropriate iterator */ - Iterator iterator(int type) + Iterator iterator(int type) { - return new HashIterator(type); + // FIXME: bogus cast here. + return new HashIterator(type); } /** @@ -705,15 +708,16 @@ public class HashMap extends AbstractMap * * @param m the map to initialize this from */ - void putAllInternal(Map m) + void putAllInternal(Map m) { - Iterator itr = m.entrySet().iterator(); + Map addMap; + + addMap = (Map) m; size = 0; - while (itr.hasNext()) + for (Map.Entry e : addMap.entrySet()) { size++; - Map.Entry e = (Map.Entry) itr.next(); - Object key = e.getKey(); + K key = e.getKey(); int idx = hash(key); addEntry(key, e.getValue(), idx, false); } @@ -730,20 +734,20 @@ public class HashMap extends AbstractMap */ private void rehash() { - HashEntry[] oldBuckets = buckets; + HashEntry[] oldBuckets = buckets; int newcapacity = (buckets.length * 2) + 1; threshold = (int) (newcapacity * loadFactor); - buckets = new HashEntry[newcapacity]; + buckets = (HashEntry[]) new HashEntry[newcapacity]; for (int i = oldBuckets.length - 1; i >= 0; i--) { - HashEntry e = oldBuckets[i]; + HashEntry e = oldBuckets[i]; while (e != null) { int idx = hash(e.key); - HashEntry dest = buckets[idx]; - HashEntry next = e.next; + HashEntry dest = buckets[idx]; + HashEntry next = e.next; e.next = buckets[idx]; buckets[idx] = e; e = next; @@ -769,10 +773,10 @@ public class HashMap extends AbstractMap s.writeInt(buckets.length); s.writeInt(size); // Avoid creating a wasted Set by creating the iterator directly. - Iterator it = iterator(ENTRIES); + Iterator> it = iterator(ENTRIES); while (it.hasNext()) { - HashEntry entry = (HashEntry) it.next(); + HashEntry entry = it.next(); s.writeObject(entry.key); s.writeObject(entry.value); } @@ -796,13 +800,13 @@ public class HashMap extends AbstractMap s.defaultReadObject(); // Read and use capacity, followed by key/value pairs. - buckets = new HashEntry[s.readInt()]; + buckets = (HashEntry[]) new HashEntry[s.readInt()]; int len = s.readInt(); size = len; while (len-- > 0) { Object key = s.readObject(); - addEntry(key, s.readObject(), hash(key), false); + addEntry((K) key, (V) s.readObject(), hash(key), false); } } @@ -813,7 +817,7 @@ public class HashMap extends AbstractMap * * @author Jon Zeppieri */ - private final class HashIterator implements Iterator + private final class HashIterator implements Iterator { /** * The type of this Iterator: {@link #KEYS}, {@link #VALUES}, @@ -861,7 +865,7 @@ public class HashMap extends AbstractMap * @throws ConcurrentModificationException if the HashMap was modified * @throws NoSuchElementException if there is none */ - public Object next() + public T next() { if (knownMod != modCount) throw new ConcurrentModificationException(); @@ -876,10 +880,10 @@ public class HashMap extends AbstractMap next = e.next; last = e; if (type == VALUES) - return e.value; + return (T) e.value; if (type == KEYS) - return e.key; - return e; + return (T) e.key; + return (T) e; } /** -- cgit v1.2.1