Cache the entry set, key set, and values collection for unmodifiable maps to reduce object creation on repeated calls.

Patch by: amitmanjhi
Review by: me


git-svn-id: https://google-web-toolkit.googlecode.com/svn/releases/1.6@3916 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/util/Collections.java b/user/super/com/google/gwt/emul/java/util/Collections.java
index 50963d9..b4fd16b 100644
--- a/user/super/com/google/gwt/emul/java/util/Collections.java
+++ b/user/super/com/google/gwt/emul/java/util/Collections.java
@@ -227,7 +227,10 @@
       }
     }
 
+    private transient UnmodifiableSet<Map.Entry<K, V>> entrySet;
+    private transient UnmodifiableSet<K> keySet;
     private final Map<? extends K, ? extends V> map;
+    private transient UnmodifiableCollection<V> values;
 
     public UnmodifiableMap(Map<? extends K, ? extends V> map) {
       this.map = map;
@@ -246,7 +249,10 @@
     }
 
     public Set<Map.Entry<K, V>> entrySet() {
-      return new UnmodifiableEntrySet<K, V>(map.entrySet());
+      if (entrySet == null) {
+        entrySet = new UnmodifiableEntrySet<K, V>(map.entrySet());
+      }
+      return entrySet;
     }
 
     public boolean equals(Object o) {
@@ -266,7 +272,10 @@
     }
 
     public Set<K> keySet() {
-      return unmodifiableSet(map.keySet());
+      if (keySet == null) {
+        keySet = new UnmodifiableSet<K>(map.keySet());
+      }
+      return keySet;
     }
 
     public V put(K key, V value) {
@@ -290,7 +299,10 @@
     }
 
     public Collection<V> values() {
-      return unmodifiableCollection(map.values());
+      if (values == null) {
+        values = new UnmodifiableCollection<V>(map.values());
+      }
+      return values;
     }
   }