2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
10 import static java.util.Objects.requireNonNull;
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;
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}.
26 * @param <E> the type of elements in this list
28 final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
29 private static final long serialVersionUID = 1L;
30 private final E[] array;
32 ConstantArrayCollection(final E[] array) {
33 this.array = requireNonNull(array);
42 public boolean isEmpty() {
43 return array.length == 0;
47 @SuppressWarnings("checkstyle:parameterName")
48 public boolean contains(final Object o) {
49 for (Object wlk : array) {
59 public Iterator<E> iterator() {
60 return new Itr<>(array);
65 public Object[] toArray() {
70 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
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());
77 System.arraycopy(array, 0, a, 0, array.length);
78 if (a.length > array.length) {
79 a[array.length] = null;
85 @SuppressWarnings("checkstyle:parameterName")
86 public boolean add(final E e) {
87 throw new UnsupportedOperationException();
91 @SuppressWarnings("checkstyle:parameterName")
92 public boolean remove(final Object o) {
93 throw new UnsupportedOperationException();
97 @SuppressWarnings("checkstyle:parameterName")
98 public boolean containsAll(@Nonnull final Collection<?> c) {
109 @SuppressWarnings("checkstyle:parameterName")
110 public boolean addAll(@Nonnull final Collection<? extends E> c) {
111 throw new UnsupportedOperationException();
115 @SuppressWarnings("checkstyle:parameterName")
116 public boolean removeAll(@Nonnull final Collection<?> c) {
117 throw new UnsupportedOperationException();
121 @SuppressWarnings("checkstyle:parameterName")
122 public boolean retainAll(@Nonnull final Collection<?> c) {
123 throw new UnsupportedOperationException();
127 public void clear() {
128 throw new UnsupportedOperationException();
132 public int hashCode() {
135 result = 31 * result + e.hashCode();
141 public boolean equals(final Object obj) {
145 if (!(obj instanceof ConstantArrayCollection)) {
149 return Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
153 public String toString() {
154 if (array.length == 0) {
158 final StringBuilder sb = new StringBuilder("[");
160 while (offset < array.length - 1) {
161 sb.append(String.valueOf(array[offset++])).append(", ");
163 return sb.append(String.valueOf(array[offset])).append(']').toString();
166 private static final class Itr<E> extends UnmodifiableIterator<E> {
167 private final E[] array;
168 private int offset = 0;
170 Itr(final E[] array) {
175 public boolean hasNext() {
176 return offset < array.length;
181 if (offset >= array.length) {
182 throw new NoSuchElementException();
184 return array[offset++];