1 |
/* Hashtable.java -- a class providing a basic hashtable data structure, |
/* Hashtable.java -- a class providing a basic hashtable data structure, |
2 |
mapping Object --> Object |
mapping Object --> Object |
3 |
Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
6 |
|
|
64 |
* @author Jon Zeppieri |
* @author Jon Zeppieri |
65 |
* @author Warren Levy |
* @author Warren Levy |
66 |
* @author Bryce McKinlay |
* @author Bryce McKinlay |
67 |
|
* @author Eric Blake <ebb9@email.byu.edu> |
68 |
*/ |
*/ |
69 |
public class Hashtable extends Dictionary |
public class Hashtable extends Dictionary |
70 |
implements Map, Cloneable, Serializable |
implements Map, Cloneable, Serializable |
72 |
/** Default number of buckets. This is the value the JDK 1.3 uses. Some |
/** Default number of buckets. This is the value the JDK 1.3 uses. Some |
73 |
* early documentation specified this value as 101. That is incorrect. */ |
* early documentation specified this value as 101. That is incorrect. */ |
74 |
private static final int DEFAULT_CAPACITY = 11; |
private static final int DEFAULT_CAPACITY = 11; |
75 |
/** The defaulty load factor; this is explicitly specified by the spec. */ |
/** The default load factor; this is explicitly specified by the spec. */ |
76 |
private static final float DEFAULT_LOAD_FACTOR = 0.75f; |
private static final float DEFAULT_LOAD_FACTOR = 0.75f; |
77 |
|
|
78 |
private static final long serialVersionUID = 1421746759512286392L; |
private static final long serialVersionUID = 1421746759512286392L; |
123 |
throw new NullPointerException(); |
throw new NullPointerException(); |
124 |
return super.setValue(newVal); |
return super.setValue(newVal); |
125 |
} |
} |
126 |
|
|
127 |
|
Entry copy() |
128 |
|
{ |
129 |
|
Entry e = new Entry(key, value); |
130 |
|
if (next != null) |
131 |
|
e.next = next.copy(); |
132 |
|
return e; |
133 |
|
} |
134 |
} |
} |
135 |
|
|
136 |
/** |
/** |
153 |
*/ |
*/ |
154 |
public Hashtable(Map m) |
public Hashtable(Map m) |
155 |
{ |
{ |
156 |
int size = Math.max(m.size() * 2, DEFAULT_CAPACITY); |
this(Math.max(m.size() * 2, DEFAULT_CAPACITY)); |
|
buckets = new Entry[size]; |
|
|
threshold = (int) (size * loadFactor); |
|
157 |
putAll(m); |
putAll(m); |
158 |
} |
} |
159 |
|
|
230 |
*/ |
*/ |
231 |
public synchronized boolean contains(Object value) |
public synchronized boolean contains(Object value) |
232 |
{ |
{ |
233 |
|
// if Hashtable is empty, this method doesn't dereference |
234 |
|
// `value' anywhere, so check for it explicitly. |
235 |
|
if (value == null) |
236 |
|
throw new NullPointerException(); |
237 |
for (int i = 0; i < buckets.length; i++) |
for (int i = 0; i < buckets.length; i++) |
238 |
{ |
{ |
239 |
Entry e = buckets[i]; |
Entry e = buckets[i]; |
400 |
public synchronized void clear() |
public synchronized void clear() |
401 |
{ |
{ |
402 |
modCount++; |
modCount++; |
403 |
for (int i=0; i < buckets.length; i++) |
buckets = new Entry[buckets.length]; |
|
{ |
|
|
buckets[i] = null; |
|
|
} |
|
404 |
size = 0; |
size = 0; |
405 |
} |
} |
406 |
|
|
423 |
for (int i=0; i < buckets.length; i++) |
for (int i=0; i < buckets.length; i++) |
424 |
{ |
{ |
425 |
Entry e = buckets[i]; |
Entry e = buckets[i]; |
426 |
Entry last = null; |
if (e != null) |
427 |
|
copy.buckets[i] = e.copy(); |
|
while (e != null) |
|
|
{ |
|
|
if (last == null) |
|
|
{ |
|
|
copy.buckets[i] = new Entry(e.key, e.value); |
|
|
last = copy.buckets[i]; |
|
|
} |
|
|
else |
|
|
{ |
|
|
last.next = new Entry(e.key, e.value); |
|
|
last = last.next; |
|
|
} |
|
|
e = e.next; |
|
|
} |
|
428 |
} |
} |
429 |
return copy; |
return copy; |
430 |
} |
} |