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 org.eclipse.jdt.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 @NonNull[] array;
32 ConstantArrayCollection(final E @NonNull[] 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) {
58 public @NonNull Iterator<E> iterator() {
59 return new Itr<>(array);
63 public Object @NonNull[] toArray() {
67 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
69 public <T> T[] toArray(final T[] a) {
70 if (a.length < array.length) {
71 return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
74 System.arraycopy(array, 0, a, 0, array.length);
75 if (a.length > array.length) {
76 a[array.length] = null;
82 @SuppressWarnings("checkstyle:parameterName")
83 public boolean add(final E e) {
84 throw new UnsupportedOperationException();
88 @SuppressWarnings("checkstyle:parameterName")
89 public boolean remove(final Object o) {
90 throw new UnsupportedOperationException();
94 @SuppressWarnings("checkstyle:parameterName")
95 public boolean containsAll(final Collection<?> c) {
106 @SuppressWarnings("checkstyle:parameterName")
107 public boolean addAll(final Collection<? extends E> c) {
108 throw new UnsupportedOperationException();
112 @SuppressWarnings("checkstyle:parameterName")
113 public boolean removeAll(final Collection<?> c) {
114 throw new UnsupportedOperationException();
118 @SuppressWarnings("checkstyle:parameterName")
119 public boolean retainAll(final Collection<?> c) {
120 throw new UnsupportedOperationException();
124 public void clear() {
125 throw new UnsupportedOperationException();
129 public int hashCode() {
132 result = 31 * result + e.hashCode();
138 public boolean equals(final Object obj) {
139 return obj == this || obj instanceof ConstantArrayCollection
140 && Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
144 public @NonNull String toString() {
145 if (array.length == 0) {
149 final StringBuilder sb = new StringBuilder("[");
151 while (offset < array.length - 1) {
152 sb.append(String.valueOf(array[offset++])).append(", ");
154 return sb.append(String.valueOf(array[offset])).append(']').toString();
157 private static final class Itr<E> extends UnmodifiableIterator<E> {
158 private final E @NonNull[] array;
159 private int offset = 0;
161 Itr(final E @NonNull[] array) {
166 public boolean hasNext() {
167 return offset < array.length;
172 if (offset >= array.length) {
173 throw new NoSuchElementException();
175 return array[offset++];