8221921: Implement size() / isEmpty() in immutable collections
authorredestad
Thu, 04 Apr 2019 23:21:52 +0200
changeset 54429 82f41fb55b63
parent 54428 6aedb80a6fd4
child 54430 fb25cd198a10
8221921: Implement size() / isEmpty() in immutable collections Reviewed-by: smarks
src/java.base/share/classes/java/util/ImmutableCollections.java
--- a/src/java.base/share/classes/java/util/ImmutableCollections.java	Thu Apr 04 23:21:24 2019 +0200
+++ b/src/java.base/share/classes/java/util/ImmutableCollections.java	Thu Apr 04 23:21:52 2019 +0200
@@ -404,6 +404,11 @@
         }
 
         @Override
+        public boolean isEmpty() {
+            return false;
+        }
+
+        @Override
         public E get(int index) {
             if (index == 0) {
                 return e0;
@@ -480,7 +485,7 @@
 
         @Override
         public boolean isEmpty() {
-            return size() == 0;
+            return elements.length == 0;
         }
 
         @Override
@@ -579,6 +584,11 @@
         }
 
         @Override
+        public boolean isEmpty() {
+            return false;
+        }
+
+        @Override
         public boolean contains(Object o) {
             return o.equals(e0) || o.equals(e1); // implicit nullcheck of o
         }
@@ -706,6 +716,11 @@
         }
 
         @Override
+        public boolean isEmpty() {
+            return size == 0;
+        }
+
+        @Override
         public boolean contains(Object o) {
             Objects.requireNonNull(o);
             return size > 0 && probe(o) >= 0;
@@ -876,6 +891,16 @@
             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");
         }
@@ -993,6 +1018,11 @@
             return size;
         }
 
+        @Override
+        public boolean isEmpty() {
+            return size == 0;
+        }
+
         class MapNIterator implements Iterator<Map.Entry<K,V>> {
 
             private int remaining;