BasicMapEntry.java: Re-added.
* 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. From-SVN: r38410
This commit is contained in:
parent
1228c7b6bd
commit
f387d86265
6 changed files with 173 additions and 125 deletions
92
libjava/java/util/BasicMapEntry.java
Normal file
92
libjava/java/util/BasicMapEntry.java
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -60,8 +60,8 @@ import java.io.ObjectOutputStream;
|
|||
* @author Jon Zeppieri
|
||||
* @author Jochen Hoenicke
|
||||
* @author Bryce McKinlay
|
||||
* @version $Revision: 1.2 $
|
||||
* @modified $Id: HashMap.java,v 1.2 2000/12/11 03:47:47 bryce Exp $
|
||||
* @version $Revision: 1.3 $
|
||||
* @modified $Id: HashMap.java,v 1.3 2000/12/17 09:15:51 bryce Exp $
|
||||
*/
|
||||
public class HashMap extends AbstractMap
|
||||
implements Map, Cloneable, Serializable
|
||||
|
@ -69,7 +69,7 @@ public class HashMap extends AbstractMap
|
|||
/** 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 @@ public class HashMap extends AbstractMap
|
|||
* 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 @@ public class HashMap extends AbstractMap
|
|||
{
|
||||
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
|
||||
|
@ -660,8 +618,8 @@ public class HashMap extends AbstractMap
|
|||
* as per the Javasoft spec.
|
||||
*
|
||||
* @author Jon Zeppieri
|
||||
* @version $Revision: 1.2 $
|
||||
* @modified $Id: HashMap.java,v 1.2 2000/12/11 03:47:47 bryce Exp $
|
||||
* @version $Revision: 1.3 $
|
||||
* @modified $Id: HashMap.java,v 1.3 2000/12/17 09:15:51 bryce Exp $
|
||||
*/
|
||||
class HashIterator implements Iterator
|
||||
{
|
||||
|
|
|
@ -64,8 +64,8 @@ import java.io.ObjectOutputStream;
|
|||
* @author Jon Zeppieri
|
||||
* @author Warren Levy
|
||||
* @author Bryce McKinlay
|
||||
* @version $Revision: 1.8 $
|
||||
* @modified $Id: Hashtable.java,v 1.8 2000/12/11 04:54:55 bryce Exp $
|
||||
* @version $Revision: 1.9 $
|
||||
* @modified $Id: Hashtable.java,v 1.9 2000/12/17 09:15:51 bryce Exp $
|
||||
*/
|
||||
public class Hashtable extends Dictionary
|
||||
implements Map, Cloneable, Serializable
|
||||
|
@ -73,7 +73,7 @@ public class Hashtable extends Dictionary
|
|||
/** 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 @@ public class Hashtable extends Dictionary
|
|||
/**
|
||||
* 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 @@ public class Hashtable extends Dictionary
|
|||
* 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 @@ public class Hashtable extends Dictionary
|
|||
return size == 0;
|
||||
}
|
||||
|
||||
/** */
|
||||
public synchronized Enumeration keys()
|
||||
{
|
||||
return new Enumerator(Enumerator.KEYS);
|
||||
|
@ -222,7 +223,7 @@ public class Hashtable extends Dictionary
|
|||
{
|
||||
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 class Hashtable extends Dictionary
|
|||
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 class Hashtable extends Dictionary
|
|||
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 @@ public class Hashtable extends Dictionary
|
|||
{
|
||||
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 @@ public class Hashtable extends Dictionary
|
|||
{
|
||||
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 @@ public class Hashtable extends Dictionary
|
|||
{
|
||||
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 @@ public class Hashtable extends Dictionary
|
|||
|
||||
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 @@ public class Hashtable extends Dictionary
|
|||
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 @@ public class Hashtable extends Dictionary
|
|||
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 @@ public class Hashtable extends Dictionary
|
|||
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 @@ public class Hashtable extends Dictionary
|
|||
*/
|
||||
protected void rehash()
|
||||
{
|
||||
HashMap.Entry[] oldBuckets = buckets;
|
||||
Entry[] oldBuckets = buckets;
|
||||
|
||||
int newcapacity = (buckets.length * 2) + 1;
|
||||
threshold = (int) (newcapacity * loadFactor);
|
||||
|
@ -638,11 +639,11 @@ public class Hashtable extends Dictionary
|
|||
|
||||
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 @@ public class Hashtable extends Dictionary
|
|||
buckets[idx] = e;
|
||||
}
|
||||
|
||||
HashMap.Entry next = e.next;
|
||||
Entry next = e.next;
|
||||
e.next = null;
|
||||
e = next;
|
||||
}
|
||||
|
@ -720,8 +721,8 @@ public class Hashtable extends Dictionary
|
|||
* as per the Javasoft spec.
|
||||
*
|
||||
* @author Jon Zeppieri
|
||||
* @version $Revision: 1.8 $
|
||||
* @modified $Id: Hashtable.java,v 1.8 2000/12/11 04:54:55 bryce Exp $
|
||||
* @version $Revision: 1.9 $
|
||||
* @modified $Id: Hashtable.java,v 1.9 2000/12/17 09:15:51 bryce Exp $
|
||||
*/
|
||||
class HashIterator implements Iterator
|
||||
{
|
||||
|
@ -739,11 +740,11 @@ public class Hashtable extends Dictionary
|
|||
// 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 @@ public class Hashtable extends Dictionary
|
|||
if (count == size)
|
||||
throw new NoSuchElementException();
|
||||
count++;
|
||||
HashMap.Entry e = null;
|
||||
Entry e = null;
|
||||
if (next != null)
|
||||
e = next;
|
||||
|
||||
|
@ -825,8 +826,8 @@ public class Hashtable extends Dictionary
|
|||
* hashtable during enumeration causes indeterminate results. Don't do it!
|
||||
*
|
||||
* @author Jon Zeppieri
|
||||
* @version $Revision: 1.8 $
|
||||
* @modified $Id: Hashtable.java,v 1.8 2000/12/11 04:54:55 bryce Exp $ */
|
||||
* @version $Revision: 1.9 $
|
||||
* @modified $Id: Hashtable.java,v 1.9 2000/12/17 09:15:51 bryce Exp $ */
|
||||
class Enumerator implements Enumeration
|
||||
{
|
||||
static final int KEYS = 0;
|
||||
|
@ -839,7 +840,7 @@ public class Hashtable extends Dictionary
|
|||
// 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 @@ public class Hashtable extends Dictionary
|
|||
if (count >= size)
|
||||
throw new NoSuchElementException();
|
||||
count++;
|
||||
HashMap.Entry e = null;
|
||||
Entry e = null;
|
||||
if (last != null)
|
||||
e = last.next;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue