Decouple HashCodeBuilder from Builder
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ConstantArrayCollection.java
index 1dcfaa2a568b9df60ce4cd07ab966fb8c71428ec..d843a36ddede5ba1e507d69a9917261084c1c7d2 100644 (file)
@@ -16,7 +16,7 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
-import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNull;
 
 /**
  * Internal array-backed {@link List}. It assumes the array does not contain nulls and it does not get modified
@@ -27,9 +27,9 @@ import javax.annotation.Nonnull;
  */
 final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
     private static final long serialVersionUID = 1L;
-    private final E[] array;
+    private final E @NonNull[] array;
 
-    ConstantArrayCollection(final E[] array) {
+    ConstantArrayCollection(final E @NonNull[] array) {
         this.array = requireNonNull(array);
     }
 
@@ -54,37 +54,19 @@ final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
         return false;
     }
 
-    @Nonnull
     @Override
-    public 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++];
-            }
-        };
+    public @NonNull Iterator<E> iterator() {
+        return new Itr<>(array);
     }
 
-    @Nonnull
     @Override
-    public Object[] toArray() {
+    public Object @NonNull[] toArray() {
         return array.clone();
     }
 
-    @Nonnull
     @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
     @Override
-    public <T> T[] toArray(@Nonnull final T[] a) {
+    public <T> T[] toArray(final T[] a) {
         if (a.length < array.length) {
             return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
         }
@@ -110,7 +92,7 @@ final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public boolean containsAll(@Nonnull final Collection<?> c) {
+    public boolean containsAll(final Collection<?> c) {
         for (Object o : c) {
             if (!contains(o)) {
                 return false;
@@ -122,19 +104,19 @@ final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public boolean addAll(@Nonnull final Collection<? extends E> c) {
+    public boolean addAll(final Collection<? extends E> c) {
         throw new UnsupportedOperationException();
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public boolean removeAll(@Nonnull final Collection<?> c) {
+    public boolean removeAll(final Collection<?> c) {
         throw new UnsupportedOperationException();
     }
 
     @Override
     @SuppressWarnings("checkstyle:parameterName")
-    public boolean retainAll(@Nonnull final Collection<?> c) {
+    public boolean retainAll(final Collection<?> c) {
         throw new UnsupportedOperationException();
     }
 
@@ -154,18 +136,12 @@ final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
 
     @Override
     public boolean equals(final Object obj) {
-        if (obj == this) {
-            return true;
-        }
-        if (!(obj instanceof ConstantArrayCollection)) {
-            return false;
-        }
-
-        return Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
+        return obj == this || obj instanceof ConstantArrayCollection
+                && Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
     }
 
     @Override
-    public String toString() {
+    public @NonNull String toString() {
         if (array.length == 0) {
             return "[]";
         }
@@ -177,4 +153,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++];
+        }
+    }
 }