Hashtable.java (Enumerator): Ensure that if hasMoreElements() returns true...
authorBryce McKinlay <bryce@waitaki.otago.ac.nz>
Thu, 13 Sep 2001 23:20:19 +0000 (23:20 +0000)
committerBryce McKinlay <bryce@gcc.gnu.org>
Thu, 13 Sep 2001 23:20:19 +0000 (00:20 +0100)
* 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

libjava/ChangeLog
libjava/java/util/Hashtable.java

index 5530021402a6f40f7e6b201d793450bbff380460..2278b73c0e6bf2b82b535a850373df501fcabfeb 100644 (file)
@@ -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.
index 4475785bbbb4d34a219ccdb09f56f53024cfd6e4..48939b25f1579df03a4f2367b9ec8e4c9cc3aae1 100644 (file)
@@ -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;
     }
-  }  
+  }
 }