Clean up more Sonar warnings
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ConstantArrayCollection.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.UnmodifiableIterator;
13 import java.io.Serializable;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.NoSuchElementException;
19 import org.eclipse.jdt.annotation.NonNull;
20
21 /**
22  * Internal array-backed {@link List}. It assumes the array does not contain nulls and it does not get modified
23  * externally. These assumptions are not checked. It does not allow modification of the underlying array -- thus it
24  * is very useful for use with {@link ImmutableOffsetMap}.
25  *
26  * @param <E> the type of elements in this list
27  */
28 final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
29     @java.io.Serial
30     private static final long serialVersionUID = 1L;
31
32     private final E @NonNull[] array;
33
34     ConstantArrayCollection(final E @NonNull[] array) {
35         this.array = requireNonNull(array);
36     }
37
38     @Override
39     public int size() {
40         return array.length;
41     }
42
43     @Override
44     public boolean isEmpty() {
45         return array.length == 0;
46     }
47
48     @Override
49     @SuppressWarnings("checkstyle:parameterName")
50     public boolean contains(final Object o) {
51         for (Object wlk : array) {
52             if (o.equals(wlk)) {
53                 return true;
54             }
55         }
56         return false;
57     }
58
59     @Override
60     public @NonNull Iterator<E> iterator() {
61         return new Itr<>(array);
62     }
63
64     @Override
65     public Object @NonNull[] toArray() {
66         return array.clone();
67     }
68
69     @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
70     @Override
71     public <T> T[] toArray(final T[] a) {
72         if (a.length < array.length) {
73             return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
74         }
75
76         System.arraycopy(array, 0, a, 0, array.length);
77         if (a.length > array.length) {
78             a[array.length] = null;
79         }
80         return a;
81     }
82
83     @Override
84     @SuppressWarnings("checkstyle:parameterName")
85     public boolean add(final E e) {
86         throw new UnsupportedOperationException();
87     }
88
89     @Override
90     @SuppressWarnings("checkstyle:parameterName")
91     public boolean remove(final Object o) {
92         throw new UnsupportedOperationException();
93     }
94
95     @Override
96     @SuppressWarnings("checkstyle:parameterName")
97     public boolean containsAll(final Collection<?> c) {
98         for (Object o : c) {
99             if (!contains(o)) {
100                 return false;
101             }
102         }
103
104         return true;
105     }
106
107     @Override
108     @SuppressWarnings("checkstyle:parameterName")
109     public boolean addAll(final Collection<? extends E> c) {
110         throw new UnsupportedOperationException();
111     }
112
113     @Override
114     @SuppressWarnings("checkstyle:parameterName")
115     public boolean removeAll(final Collection<?> c) {
116         throw new UnsupportedOperationException();
117     }
118
119     @Override
120     @SuppressWarnings("checkstyle:parameterName")
121     public boolean retainAll(final Collection<?> c) {
122         throw new UnsupportedOperationException();
123     }
124
125     @Override
126     public void clear() {
127         throw new UnsupportedOperationException();
128     }
129
130     @Override
131     public int hashCode() {
132         int result = 1;
133         for (E e : array) {
134             result = 31 * result + e.hashCode();
135         }
136         return result;
137     }
138
139     @Override
140     public boolean equals(final Object obj) {
141         return obj == this || obj instanceof ConstantArrayCollection<?> other && Arrays.equals(array, other.array);
142     }
143
144     @Override
145     public @NonNull String toString() {
146         if (array.length == 0) {
147             return "[]";
148         }
149
150         final StringBuilder sb = new StringBuilder("[");
151         int offset = 0;
152         while (offset < array.length - 1) {
153             sb.append(String.valueOf(array[offset++])).append(", ");
154         }
155         return sb.append(String.valueOf(array[offset])).append(']').toString();
156     }
157
158     private static final class Itr<E> extends UnmodifiableIterator<E> {
159         private final E @NonNull[] array;
160         private int offset = 0;
161
162         Itr(final E @NonNull[] array) {
163             this.array = array;
164         }
165
166         @Override
167         public boolean hasNext() {
168             return offset < array.length;
169         }
170
171         @Override
172         public E next() {
173             if (offset >= array.length) {
174                 throw new NoSuchElementException();
175             }
176             return array[offset++];
177         }
178     }
179 }