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