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