8221981: Simplify Map/List/Set.of() implementation
authorredestad
Thu, 04 Apr 2019 23:21:24 +0200
changeset 54428 6aedb80a6fd4
parent 54427 6aa05983e9d3
child 54429 82f41fb55b63
8221981: Simplify Map/List/Set.of() implementation Reviewed-by: smarks
src/java.base/share/classes/java/util/ImmutableCollections.java
src/java.base/share/classes/java/util/List.java
src/java.base/share/classes/java/util/Map.java
src/java.base/share/classes/java/util/Set.java
--- a/src/java.base/share/classes/java/util/ImmutableCollections.java	Thu Apr 04 23:19:26 2019 +0200
+++ b/src/java.base/share/classes/java/util/ImmutableCollections.java	Thu Apr 04 23:21:24 2019 +0200
@@ -95,11 +95,6 @@
         }
     }
 
-    @SuppressWarnings("unchecked")
-    static <E> List<E> emptyList() {
-        return (List<E>) ListN.EMPTY_LIST;
-    }
-
     static abstract class AbstractImmutableList<E> extends AbstractImmutableCollection<E>
             implements List<E>, RandomAccess {
 
@@ -556,11 +551,6 @@
         public abstract int hashCode();
     }
 
-    @SuppressWarnings("unchecked")
-    static <E> Set<E> emptySet() {
-        return (Set<E>) SetN.EMPTY_SET;
-    }
-
     static final class Set12<E> extends AbstractImmutableSet<E>
             implements Serializable {
 
@@ -844,11 +834,6 @@
 
     // ---------- Map Implementations ----------
 
-    @SuppressWarnings("unchecked")
-    static <K,V> Map<K,V> emptyMap() {
-        return (Map<K,V>) MapN.EMPTY_MAP;
-    }
-
     abstract static class AbstractImmutableMap<K,V> extends AbstractMap<K,V> implements Serializable {
         @Override public void clear() { throw uoe(); }
         @Override public V compute(K key, BiFunction<? super K,? super V,? extends V> rf) { throw uoe(); }
@@ -1248,7 +1233,7 @@
                     return Set.of(array);
                 case IMM_MAP:
                     if (array.length == 0) {
-                        return ImmutableCollections.emptyMap();
+                        return ImmutableCollections.MapN.EMPTY_MAP;
                     } else if (array.length == 2) {
                         return new ImmutableCollections.Map1<>(array[0], array[1]);
                     } else {
--- a/src/java.base/share/classes/java/util/List.java	Thu Apr 04 23:19:26 2019 +0200
+++ b/src/java.base/share/classes/java/util/List.java	Thu Apr 04 23:21:24 2019 +0200
@@ -787,8 +787,9 @@
      *
      * @since 9
      */
+    @SuppressWarnings("unchecked")
     static <E> List<E> of() {
-        return ImmutableCollections.emptyList();
+        return (List<E>) ImmutableCollections.ListN.EMPTY_LIST;
     }
 
     /**
@@ -1031,7 +1032,9 @@
     static <E> List<E> of(E... elements) {
         switch (elements.length) { // implicit null check of elements
             case 0:
-                return ImmutableCollections.emptyList();
+                @SuppressWarnings("unchecked")
+                var list = (List<E>) ImmutableCollections.ListN.EMPTY_LIST;
+                return list;
             case 1:
                 return new ImmutableCollections.List12<>(elements[0]);
             case 2:
--- a/src/java.base/share/classes/java/util/Map.java	Thu Apr 04 23:19:26 2019 +0200
+++ b/src/java.base/share/classes/java/util/Map.java	Thu Apr 04 23:21:24 2019 +0200
@@ -1286,8 +1286,9 @@
      *
      * @since 9
      */
+    @SuppressWarnings("unchecked")
     static <K, V> Map<K, V> of() {
-        return ImmutableCollections.emptyMap();
+        return (Map<K,V>) ImmutableCollections.MapN.EMPTY_MAP;
     }
 
     /**
@@ -1604,7 +1605,9 @@
     @SuppressWarnings("varargs")
     static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
         if (entries.length == 0) { // implicit null check of entries array
-            return ImmutableCollections.emptyMap();
+            @SuppressWarnings("unchecked")
+            var map = (Map<K,V>) ImmutableCollections.MapN.EMPTY_MAP;
+            return map;
         } else if (entries.length == 1) {
             // implicit null check of the array slot
             return new ImmutableCollections.Map1<>(entries[0].getKey(),
--- a/src/java.base/share/classes/java/util/Set.java	Thu Apr 04 23:19:26 2019 +0200
+++ b/src/java.base/share/classes/java/util/Set.java	Thu Apr 04 23:21:24 2019 +0200
@@ -448,8 +448,9 @@
      *
      * @since 9
      */
+    @SuppressWarnings("unchecked")
     static <E> Set<E> of() {
-        return ImmutableCollections.emptySet();
+        return (Set<E>) ImmutableCollections.SetN.EMPTY_SET;
     }
 
     /**
@@ -692,7 +693,9 @@
     static <E> Set<E> of(E... elements) {
         switch (elements.length) { // implicit null check of elements
             case 0:
-                return ImmutableCollections.emptySet();
+                @SuppressWarnings("unchecked")
+                var set = (Set<E>) ImmutableCollections.SetN.EMPTY_SET;
+                return set;
             case 1:
                 return new ImmutableCollections.Set12<>(elements[0]);
             case 2: