Separate out ConstantArrayCollection iterator 86/78086/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 23 Nov 2018 10:12:15 +0000 (11:12 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 23 Nov 2018 10:14:08 +0000 (11:14 +0100)
This splits it out into its own static class.

Change-Id: I94a86c65eefdba79db2f5ec3a01b13dbad0b41b0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/ConstantArrayCollection.java

index ed84c62d93266bb67715bb48b18d87f8f7616d99..25d813c7e0630fe33becf4b9bc191b66862864fc 100644 (file)
@@ -56,26 +56,11 @@ final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
 
     @Override
     public @NonNull Iterator<E> iterator() {
-        return new UnmodifiableIterator<E>() {
-            private int offset = 0;
-
-            @Override
-            public boolean hasNext() {
-                return offset < array.length;
-            }
-
-            @Override
-            public E next() {
-                if (offset >= array.length) {
-                    throw new NoSuchElementException();
-                }
-                return array[offset++];
-            }
-        };
+        return new Itr<>(array);
     }
 
     @Override
-    public @NonNull Object[] toArray() {
+    public Object @NonNull[] toArray() {
         return array.clone();
     }
 
@@ -174,4 +159,26 @@ final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
         }
         return sb.append(String.valueOf(array[offset])).append(']').toString();
     }
+
+    private static final class Itr<E> extends UnmodifiableIterator<E> {
+        private final E @NonNull[] array;
+        private int offset = 0;
+
+        Itr(final E @NonNull[] array) {
+            this.array = array;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return offset < array.length;
+        }
+
+        @Override
+        public E next() {
+            if (offset >= array.length) {
+                throw new NoSuchElementException();
+            }
+            return array[offset++];
+        }
+    }
 }