Commit 6a3d4031 by Bryce McKinlay Committed by Bryce McKinlay

Hashtable.java (Enumerator): Ensure that if hasMoreElements() returns true...

	* java/util/Hashtable.java (Enumerator): Ensure that if
	hasMoreElements() returns true, nextElement() will always return
	something even if the table has been modified.

From-SVN: r45584
parent 7364d5f8
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
* java/io/File.java (normalizePath): Use equals() not '==' for string * java/io/File.java (normalizePath): Use equals() not '==' for string
comparison. comparison.
* java/util/Hashtable.java (Enumerator): Ensure that if
hasMoreElements() returns true, nextElement() will always return
something even if the table has been modified.
2001-09-12 Tom Tromey <tromey@redhat.com> 2001-09-12 Tom Tromey <tromey@redhat.com>
* Makefile.in: Rebuilt. * Makefile.in: Rebuilt.
......
...@@ -833,44 +833,57 @@ public class Hashtable extends Dictionary ...@@ -833,44 +833,57 @@ public class Hashtable extends Dictionary
static final int VALUES = 1; static final int VALUES = 1;
int type; int type;
// The total number of elements returned by nextElement(). Used to
// determine if there are more elements remaining.
int count;
// current index in the physical hash table. // current index in the physical hash table.
int idx; int idx;
// the last Entry returned. // the last Entry returned by nextEntry().
Entry last; Entry last;
// Entry which will be returned by the next nextElement() call.
Entry next;
Enumerator(int type) Enumerator(int type)
{ {
this.type = type; this.type = type;
this.count = 0;
this.idx = buckets.length; this.idx = buckets.length;
} }
private Entry nextEntry()
{
Entry e = null;
if (last != null)
e = last.next;
while (e == null && idx > 0)
{
e = buckets[--idx];
}
last = e;
return e;
}
public boolean hasMoreElements() public boolean hasMoreElements()
{ {
return count < Hashtable.this.size; if (next != null)
return true;
next = nextEntry();
return (next != null);
} }
public Object nextElement() public Object nextElement()
{ {
if (count >= size)
throw new NoSuchElementException();
count++;
Entry e = null; Entry e = null;
if (last != null) if (next != null)
e = last.next;
while (e == null)
{ {
e = buckets[--idx]; e = next;
next = null;
} }
else
last = e; e = nextEntry();
if (e == null)
throw new NoSuchElementException("Hashtable Enumerator");
if (type == VALUES) if (type == VALUES)
return e.value; return e.value;
return e.key; return e.key;
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment