< prev index next >

src/java.base/share/classes/java/util/ImmutableCollections.java

Print this page

        

@@ -93,10 +93,15 @@
         } else {
             return (List<E>)List.of(coll.toArray());
         }
     }
 
+    @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 {
 
         // all mutating methods throw UnsupportedOperationException
         @Override public void    add(int index, E element) { throw uoe(); }

@@ -402,15 +407,10 @@
         public int size() {
             return e1 != null ? 2 : 1;
         }
 
         @Override
-        public boolean isEmpty() {
-            return false;
-        }
-
-        @Override
         public E get(int index) {
             if (index == 0) {
                 return e0;
             } else if (index == 1 && e1 != null) {
                 return e1;

@@ -483,11 +483,11 @@
             elements = tmp;
         }
 
         @Override
         public boolean isEmpty() {
-            return elements.length == 0;
+            return size() == 0;
         }
 
         @Override
         public int size() {
             return elements.length;

@@ -554,10 +554,15 @@
 
         @Override
         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 {
 
         @Stable
         final E e0;

@@ -582,15 +587,10 @@
         public int size() {
             return (e1 == null) ? 1 : 2;
         }
 
         @Override
-        public boolean isEmpty() {
-            return false;
-        }
-
-        @Override
         public boolean contains(Object o) {
             return o.equals(e0) || o.equals(e1); // implicit nullcheck of o
         }
 
         @Override

@@ -714,15 +714,10 @@
         public int size() {
             return size;
         }
 
         @Override
-        public boolean isEmpty() {
-            return size == 0;
-        }
-
-        @Override
         public boolean contains(Object o) {
             Objects.requireNonNull(o);
             return size > 0 && probe(o) >= 0;
         }
 

@@ -847,10 +842,15 @@
         }
     }
 
     // ---------- 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(); }
         @Override public V computeIfAbsent(K key, Function<? super K,? extends V> mf) { throw uoe(); }
         @Override public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> rf) { throw uoe(); }

@@ -880,34 +880,19 @@
         public Set<Map.Entry<K,V>> entrySet() {
             return Set.of(new KeyValueHolder<>(k0, v0));
         }
 
         @Override
-        public V get(Object o) {
-            return o.equals(k0) ? v0 : null; // implicit nullcheck of o
-        }
-
-        @Override
         public boolean containsKey(Object o) {
             return o.equals(k0); // implicit nullcheck of o
         }
 
         @Override
         public boolean containsValue(Object o) {
             return o.equals(v0); // implicit nullcheck of o
         }
 
-        @Override
-        public int size() {
-            return 1;
-        }
-
-        @Override
-        public boolean isEmpty() {
-            return false;
-        }
-
         private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
             throw new InvalidObjectException("not serial proxy");
         }
 
         private Object writeReplace() {

@@ -1021,15 +1006,10 @@
         @Override
         public int size() {
             return size;
         }
 
-        @Override
-        public boolean isEmpty() {
-            return size == 0;
-        }
-
         class MapNIterator implements Iterator<Map.Entry<K,V>> {
 
             private int remaining;
 
             private int idx;

@@ -1266,11 +1246,11 @@
                     return List.of(array);
                 case IMM_SET:
                     return Set.of(array);
                 case IMM_MAP:
                     if (array.length == 0) {
-                        return ImmutableCollections.MapN.EMPTY_MAP;
+                        return ImmutableCollections.emptyMap();
                     } else if (array.length == 2) {
                         return new ImmutableCollections.Map1<>(array[0], array[1]);
                     } else {
                         return new ImmutableCollections.MapN<>(array);
                     }
< prev index next >