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