Merge branch 'master' of ../controller
[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     private static final long serialVersionUID = 1L;
30     private final E @NonNull[] array;
31
32     ConstantArrayCollection(final E @NonNull[] array) {
33         this.array = requireNonNull(array);
34     }
35
36     @Override
37     public int size() {
38         return array.length;
39     }
40
41     @Override
42     public boolean isEmpty() {
43         return array.length == 0;
44     }
45
46     @Override
47     @SuppressWarnings("checkstyle:parameterName")
48     public boolean contains(final Object o) {
49         for (Object wlk : array) {
50             if (o.equals(wlk)) {
51                 return true;
52             }
53         }
54         return false;
55     }
56
57     @Override
58     public @NonNull Iterator<E> iterator() {
59         return new Itr<>(array);
60     }
61
62     @Override
63     public Object @NonNull[] toArray() {
64         return array.clone();
65     }
66
67     @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
68     @Override
69     public <T> T[] toArray(final T[] a) {
70         if (a.length < array.length) {
71             return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
72         }
73
74         System.arraycopy(array, 0, a, 0, array.length);
75         if (a.length > array.length) {
76             a[array.length] = null;
77         }
78         return a;
79     }
80
81     @Override
82     @SuppressWarnings("checkstyle:parameterName")
83     public boolean add(final E e) {
84         throw new UnsupportedOperationException();
85     }
86
87     @Override
88     @SuppressWarnings("checkstyle:parameterName")
89     public boolean remove(final Object o) {
90         throw new UnsupportedOperationException();
91     }
92
93     @Override
94     @SuppressWarnings("checkstyle:parameterName")
95     public boolean containsAll(final Collection<?> c) {
96         for (Object o : c) {
97             if (!contains(o)) {
98                 return false;
99             }
100         }
101
102         return true;
103     }
104
105     @Override
106     @SuppressWarnings("checkstyle:parameterName")
107     public boolean addAll(final Collection<? extends E> c) {
108         throw new UnsupportedOperationException();
109     }
110
111     @Override
112     @SuppressWarnings("checkstyle:parameterName")
113     public boolean removeAll(final Collection<?> c) {
114         throw new UnsupportedOperationException();
115     }
116
117     @Override
118     @SuppressWarnings("checkstyle:parameterName")
119     public boolean retainAll(final Collection<?> c) {
120         throw new UnsupportedOperationException();
121     }
122
123     @Override
124     public void clear() {
125         throw new UnsupportedOperationException();
126     }
127
128     @Override
129     public int hashCode() {
130         int result = 1;
131         for (E e : array) {
132             result = 31 * result + e.hashCode();
133         }
134         return result;
135     }
136
137     @Override
138     public boolean equals(final Object obj) {
139         return obj == this || obj instanceof ConstantArrayCollection
140                 && Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
141     }
142
143     @Override
144     public @NonNull String toString() {
145         if (array.length == 0) {
146             return "[]";
147         }
148
149         final StringBuilder sb = new StringBuilder("[");
150         int offset = 0;
151         while (offset < array.length - 1) {
152             sb.append(String.valueOf(array[offset++])).append(", ");
153         }
154         return sb.append(String.valueOf(array[offset])).append(']').toString();
155     }
156
157     private static final class Itr<E> extends UnmodifiableIterator<E> {
158         private final E @NonNull[] array;
159         private int offset = 0;
160
161         Itr(final E @NonNull[] array) {
162             this.array = array;
163         }
164
165         @Override
166         public boolean hasNext() {
167             return offset < array.length;
168         }
169
170         @Override
171         public E next() {
172             if (offset >= array.length) {
173                 throw new NoSuchElementException();
174             }
175             return array[offset++];
176         }
177     }
178 }