This is the mail archive of the
java-patches@sources.redhat.com
mailing list for the Java project.
Bring back BasicMapEntry
- To: java-patches at sources dot redhat dot com
- Subject: Bring back BasicMapEntry
- From: Bryce McKinlay <bryce at albatross dot co dot nz>
- Date: Thu, 21 Dec 2000 15:00:12 +1300
After looking at TreeMap I've decided that the BasicMapEntry class was
worthwhile after all. This also cleans up Hashtable a bit by having
Hashtable.Entry extend BasicMapEntry and not HashMap.Entry.
regards
[ bryce ]
2000-12-21 Bryce McKinlay <bryce@albatross.co.nz>
* java/util/BasicMapEntry.java: Re-added.
* java/util/HashMap.java (Entry): Extend BasicMapEntry.
(putAll): Test for BasicMapEntry.
* java/util/Hashtable.java (Entry): Extend BasicMapEntry.
(putAll): Test for BasicMapEntry.
Change references from `HashMap.Entry' to `Entry' in various places.
* Makefile.am: Add BasicMapEntry.java.
* Makefile.in: Rebuilt.
Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/egcs/libjava/Makefile.am,v
retrieving revision 1.119
diff -u -r1.119 Makefile.am
--- Makefile.am 2000/12/13 15:45:29 1.119
+++ Makefile.am 2000/12/21 01:51:09
@@ -929,6 +929,7 @@
java/util/AbstractSet.java \
java/util/ArrayList.java \
java/util/Arrays.java \
+java/util/BasicMapEntry.java \
java/util/BitSet.java \
java/util/Calendar.java \
java/util/Collection.java \
Index: java/util/BasicMapEntry.java
===================================================================
RCS file: BasicMapEntry.java
diff -N BasicMapEntry.java
--- /dev/null Tue May 5 13:32:27 1998
+++ BasicMapEntry.java Wed Dec 20 17:51:09 2000
@@ -0,0 +1,92 @@
+/* BasicMapEntry.java -- a class providing a plain-vanilla implementation of
+ the Map.Entry interface; could be used anywhere in java.util
+ Copyright (C) 1998, 2000 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+As a special exception, if you link this library with other files to
+produce an executable, this library does not by itself cause the
+resulting executable to be covered by the GNU General Public License.
+This exception does not however invalidate any other reasons why the
+executable file might be covered by the GNU General Public License. */
+
+
+package java.util;
+
+/**
+ * A class which implements Map.Entry. It is shared by HashMap, TreeMap, and
+ * Hashtable.
+ *
+ * @author Jon Zeppieri
+ * @version $Revision: 1.5 $
+ * @modified $Id: BasicMapEntry.java,v 1.5 2000/10/26 10:19:00 bryce Exp $
+ */
+class BasicMapEntry implements Map.Entry
+{
+ Object key;
+ Object value;
+
+ BasicMapEntry(Object newKey, Object newValue)
+ {
+ key = newKey;
+ value = newValue;
+ }
+
+ public final boolean equals(Object o)
+ {
+ if (!(o instanceof Map.Entry))
+ return false;
+ Map.Entry e = (Map.Entry) o;
+ return (key == null ? e.getKey() == null : key.equals(e.getKey())
+ && value == null ? e.getValue() == null
+ : value.equals(e.getValue()));
+ }
+
+ public final Object getKey()
+ {
+ return key;
+ }
+
+ public final Object getValue()
+ {
+ return value;
+ }
+
+ public final int hashCode()
+ {
+ int kc = (key == null ? 0 : key.hashCode());
+ int vc = (value == null ? 0 : value.hashCode());
+ return kc ^ vc;
+ }
+
+ /**
+ * sets the value of this Map.Entry. Note that this is overriden by
+ * Hashtable.Entry, which does not permit a null value.
+ */
+ public Object setValue(Object newVal)
+ {
+ Object r = value;
+ value = newVal;
+ return r;
+ }
+
+ public final String toString()
+ {
+ return key + "=" + value;
+ }
+}
Index: java/util/HashMap.java
===================================================================
RCS file: /cvs/gcc/egcs/libjava/java/util/HashMap.java,v
retrieving revision 1.3
diff -u -r1.3 HashMap.java
--- HashMap.java 2000/12/17 09:15:51 1.3
+++ HashMap.java 2000/12/21 01:51:09
@@ -69,7 +69,7 @@
/** Default number of buckets. This is the value the JDK 1.3 uses. Some
* early documentation specified this value as 101. That is incorrect. */
private static final int DEFAULT_CAPACITY = 11;
- /** The defaulty load factor; this is explicitly specified by Sun */
+ /** The defaulty load factor; this is explicitly specified by the spec. */
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private static final long serialVersionUID = 362498820763181265L;
@@ -104,56 +104,14 @@
* Class to represent an entry in the hash table. Holds a single key-value
* pair.
*/
- static class Entry implements Map.Entry
+ static class Entry extends BasicMapEntry
{
- Object key;
- Object value;
Entry next;
Entry(Object key, Object value)
{
- this.key = key;
- this.value = value;
+ super(key, value);
}
-
- public boolean equals(Object o)
- {
- if (!(o instanceof Map.Entry))
- return false;
- Map.Entry e = (Map.Entry) o;
- return (key == null ? e.getKey() == null : key.equals(e.getKey())
- && value == null ? e.getValue() == null :
- value.equals(e.getValue()));
- }
-
- public Object getKey()
- {
- return key;
- }
-
- public Object getValue()
- {
- return value;
- }
-
- public int hashCode()
- {
- int kc = (key == null ? 0 : key.hashCode());
- int vc = (value == null ? 0 : value.hashCode());
- return kc ^ vc;
- }
-
- public Object setValue(Object newVal)
- {
- Object r = value;
- value = newVal;
- return r;
- }
-
- public String toString()
- {
- return key + "=" + value;
- }
}
/**
@@ -368,9 +326,9 @@
{
Map.Entry e = (Map.Entry) itr.next();
// Optimize in case the Entry is one of our own.
- if (e instanceof Entry)
+ if (e instanceof BasicMapEntry)
{
- Entry entry = (Entry) e;
+ BasicMapEntry entry = (BasicMapEntry) e;
put(entry.key, entry.value);
}
else
Index: java/util/Hashtable.java
===================================================================
RCS file: /cvs/gcc/egcs/libjava/java/util/Hashtable.java,v
retrieving revision 1.9
diff -u -r1.9 Hashtable.java
--- Hashtable.java 2000/12/17 09:15:51 1.9
+++ Hashtable.java 2000/12/21 01:51:10
@@ -73,7 +73,7 @@
/** Default number of buckets. This is the value the JDK 1.3 uses. Some
* early documentation specified this value as 101. That is incorrect. */
private static final int DEFAULT_CAPACITY = 11;
- /** The defaulty load factor; this is explicitly specified by Sun */
+ /** The defaulty load factor; this is explicitly specified by the spec. */
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private static final long serialVersionUID = 1421746759512286392L;
@@ -93,7 +93,7 @@
/**
* Array containing the actual key-value mappings
*/
- transient HashMap.Entry[] buckets;
+ transient Entry[] buckets;
/**
* counts the number of modifications this Hashtable has undergone, used
@@ -109,14 +109,16 @@
* pair. A Hashtable Entry is identical to a HashMap Entry, except that
* `null' is not allowed for keys and values.
*/
- static class Entry extends HashMap.Entry
+ static class Entry extends BasicMapEntry
{
+ Entry next;
+
Entry(Object key, Object value)
{
super(key, value);
}
- public Object setValue(Object newVal)
+ public final Object setValue(Object newVal)
{
if (newVal == null)
throw new NullPointerException();
@@ -195,7 +197,6 @@
return size == 0;
}
- /** */
public synchronized Enumeration keys()
{
return new Enumerator(Enumerator.KEYS);
@@ -222,7 +223,7 @@
{
for (int i = 0; i < buckets.length; i++)
{
- HashMap.Entry e = buckets[i];
+ Entry e = buckets[i];
while (e != null)
{
if (value.equals(e.value))
@@ -255,7 +256,7 @@
public synchronized boolean containsKey(Object key)
{
int idx = hash(key);
- HashMap.Entry e = buckets[idx];
+ Entry e = buckets[idx];
while (e != null)
{
if (key.equals(e.key))
@@ -274,7 +275,7 @@
public synchronized Object get(Object key)
{
int idx = hash(key);
- HashMap.Entry e = buckets[idx];
+ Entry e = buckets[idx];
while (e != null)
{
if (key.equals(e.key))
@@ -294,7 +295,7 @@
{
modCount++;
int idx = hash(key);
- HashMap.Entry e = buckets[idx];
+ Entry e = buckets[idx];
// Hashtable does not accept null values. This method doesn't dereference
// `value' anywhere, so check for it explicitly.
@@ -342,8 +343,8 @@
{
modCount++;
int idx = hash(key);
- HashMap.Entry e = buckets[idx];
- HashMap.Entry last = null;
+ Entry e = buckets[idx];
+ Entry last = null;
while (e != null)
{
@@ -371,9 +372,9 @@
{
Map.Entry e = (Map.Entry) itr.next();
// Optimize in case the Entry is one of our own.
- if (e instanceof Entry)
+ if (e instanceof BasicMapEntry)
{
- Entry entry = (Entry) e;
+ BasicMapEntry entry = (BasicMapEntry) e;
put(entry.key, entry.value);
}
else
@@ -411,8 +412,8 @@
for (int i=0; i < buckets.length; i++)
{
- HashMap.Entry e = buckets[i];
- HashMap.Entry last = null;
+ Entry e = buckets[i];
+ Entry last = null;
while (e != null)
{
@@ -536,7 +537,7 @@
if (!(o instanceof Map.Entry))
return false;
Map.Entry me = (Map.Entry) o;
- HashMap.Entry e = getEntry(me);
+ Entry e = getEntry(me);
return (e != null);
}
@@ -545,7 +546,7 @@
if (!(o instanceof Map.Entry))
return false;
Map.Entry me = (Map.Entry) o;
- HashMap.Entry e = getEntry(me);
+ Entry e = getEntry(me);
if (e != null)
{
Hashtable.this.remove(e.key);
@@ -609,10 +610,10 @@
return Math.abs(key.hashCode() % buckets.length);
}
- private HashMap.Entry getEntry(Map.Entry me)
+ private Entry getEntry(Map.Entry me)
{
int idx = hash(me.getKey());
- HashMap.Entry e = buckets[idx];
+ Entry e = buckets[idx];
while (e != null)
{
if (e.equals(me))
@@ -630,7 +631,7 @@
*/
protected void rehash()
{
- HashMap.Entry[] oldBuckets = buckets;
+ Entry[] oldBuckets = buckets;
int newcapacity = (buckets.length * 2) + 1;
threshold = (int) (newcapacity * loadFactor);
@@ -638,11 +639,11 @@
for (int i = 0; i < oldBuckets.length; i++)
{
- HashMap.Entry e = oldBuckets[i];
+ Entry e = oldBuckets[i];
while (e != null)
{
int idx = hash(e.key);
- HashMap.Entry dest = buckets[idx];
+ Entry dest = buckets[idx];
if (dest != null)
{
@@ -655,7 +656,7 @@
buckets[idx] = e;
}
- HashMap.Entry next = e.next;
+ Entry next = e.next;
e.next = null;
e = next;
}
@@ -739,11 +740,11 @@
// Current index in the physical hash table.
int idx;
// The last Entry returned by a next() call.
- HashMap.Entry last;
+ Entry last;
// The next entry that should be returned by next(). It is set to something
// if we're iterating through a bucket that contains multiple linked
// entries. It is null if next() needs to find a new bucket.
- HashMap.Entry next;
+ Entry next;
/* Construct a new HashIterator with the supplied type:
KEYS, VALUES, or ENTRIES */
@@ -771,7 +772,7 @@
if (count == size)
throw new NoSuchElementException();
count++;
- HashMap.Entry e = null;
+ Entry e = null;
if (next != null)
e = next;
@@ -839,7 +840,7 @@
// current index in the physical hash table.
int idx;
// the last Entry returned.
- HashMap.Entry last;
+ Entry last;
Enumerator(int type)
{
@@ -858,7 +859,7 @@
if (count >= size)
throw new NoSuchElementException();
count++;
- HashMap.Entry e = null;
+ Entry e = null;
if (last != null)
e = last.next;