class library changes
[IRC.git] / Robust / src / ClassLibrary / MGC / HashMap.java
index 9ec53db3a82af8dba146480ef3b39a4a31f8866d..1edd6c8f59cc7c046e542aa6eb099c3c06e01011 100644 (file)
@@ -3,6 +3,8 @@ public class HashMap implements Map {
   float loadFactor;
   int numItems;
   int threshold;
+  Collection values;
+  Set keys;
 
   public HashMap() {
     init(16, 0.75f);
@@ -143,4 +145,73 @@ public class HashMap implements Map {
     table[bin]=he;
     return null;
   }
+  
+  public Collection values()
+  {
+    if (values == null)
+      // We don't bother overriding many of the optional methods, as doing so
+      // wouldn't provide any significant performance advantage.
+      values = new AbstractCollection()
+      {
+        
+        public int size()
+        {
+          return size;
+        }
+
+        public Iterator iterator()
+        {
+          // Cannot create the iterator directly, because of LinkedHashMap.
+          return HashMapIterator(HashMap.this, 1);
+        }
+
+        public void clear()
+        {
+          HashMap.this.clear();
+        }
+      };
+    return values;
+  }
+  
+  public Set keySet()
+  {
+    if (keys == null)
+      // Create an AbstractSet with custom implementations of those methods
+      // that can be overridden easily and efficiently.
+      keys = new AbstractSet()
+      {
+        public int size()
+        {
+          return size;
+        }
+
+        public Iterator iterator()
+        {
+          // Cannot create the iterator directly, because of LinkedHashMap.
+          //return HashMap.this.iterator(KEYS);
+          return HashMapIterator(HashMap.this, 0);
+        }
+
+        public void clear()
+        {
+          HashMap.this.clear();
+        }
+
+        public boolean contains(Object o)
+        {
+          return containsKey(o);
+        }
+
+        public boolean remove(Object o)
+        {
+          // Test against the size of the HashMap to determine if anything
+          // really got removed. This is necessary because the return value
+          // of HashMap.remove() is ambiguous in the null case.
+          int oldsize = size;
+          HashMap.this.remove(o);
+          return oldsize != size;
+        }
+      };
+    return keys;
+  }
 }