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