diff options
author | Bryce McKinlay <bryce@waitaki.otago.ac.nz> | 2001-09-13 23:20:19 +0000 |
---|---|---|
committer | Bryce McKinlay <bryce@gcc.gnu.org> | 2001-09-14 00:20:19 +0100 |
commit | 6a3d403114ac013e8a5e9a8f9496b6d8fd6a8f99 (patch) | |
tree | e91afa34b8fe397cfc21764b75025a4034a96d56 /libjava | |
parent | 7364d5f851b8430aa3fdd8d9fa30efa5755873ef (diff) | |
download | gcc-6a3d403114ac013e8a5e9a8f9496b6d8fd6a8f99.zip gcc-6a3d403114ac013e8a5e9a8f9496b6d8fd6a8f99.tar.gz gcc-6a3d403114ac013e8a5e9a8f9496b6d8fd6a8f99.tar.bz2 |
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
Diffstat (limited to 'libjava')
-rw-r--r-- | libjava/ChangeLog | 4 | ||||
-rw-r--r-- | libjava/java/util/Hashtable.java | 47 |
2 files changed, 34 insertions, 17 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 5530021..2278b73 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -3,6 +3,10 @@ * java/io/File.java (normalizePath): Use equals() not '==' for string 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> * Makefile.in: Rebuilt. diff --git a/libjava/java/util/Hashtable.java b/libjava/java/util/Hashtable.java index 4475785..48939b2 100644 --- a/libjava/java/util/Hashtable.java +++ b/libjava/java/util/Hashtable.java @@ -833,44 +833,57 @@ public class Hashtable extends Dictionary static final int VALUES = 1; 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. int idx; - // the last Entry returned. + // the last Entry returned by nextEntry(). Entry last; + // Entry which will be returned by the next nextElement() call. + Entry next; Enumerator(int type) { this.type = type; - this.count = 0; 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() { - return count < Hashtable.this.size; + if (next != null) + return true; + next = nextEntry(); + return (next != null); } public Object nextElement() { - if (count >= size) - throw new NoSuchElementException(); - count++; Entry e = null; - if (last != null) - e = last.next; - - while (e == null) + if (next != null) { - e = buckets[--idx]; + e = next; + next = null; } - - last = e; + else + e = nextEntry(); + if (e == null) + throw new NoSuchElementException("Hashtable Enumerator"); if (type == VALUES) return e.value; return e.key; } - } + } } |