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