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 {
30 private static final long serialVersionUID = 1L;
32 private final E @NonNull[] array;
34 ConstantArrayCollection(final E @NonNull[] array) {
35 this.array = requireNonNull(array);
44 public boolean isEmpty() {
45 return array.length == 0;
49 @SuppressWarnings("checkstyle:parameterName")
50 public boolean contains(final Object o) {
51 for (Object wlk : array) {
60 public @NonNull Iterator<E> iterator() {
61 return new Itr<>(array);
65 public Object @NonNull[] toArray() {
69 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
71 public <T> T[] toArray(final T[] a) {
72 if (a.length < array.length) {
73 return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
76 System.arraycopy(array, 0, a, 0, array.length);
77 if (a.length > array.length) {
78 a[array.length] = null;
84 @SuppressWarnings("checkstyle:parameterName")
85 public boolean add(final E e) {
86 throw new UnsupportedOperationException();
90 @SuppressWarnings("checkstyle:parameterName")
91 public boolean remove(final Object o) {
92 throw new UnsupportedOperationException();
96 @SuppressWarnings("checkstyle:parameterName")
97 public boolean containsAll(final Collection<?> c) {
108 @SuppressWarnings("checkstyle:parameterName")
109 public boolean addAll(final Collection<? extends E> c) {
110 throw new UnsupportedOperationException();
114 @SuppressWarnings("checkstyle:parameterName")
115 public boolean removeAll(final Collection<?> c) {
116 throw new UnsupportedOperationException();
120 @SuppressWarnings("checkstyle:parameterName")
121 public boolean retainAll(final Collection<?> c) {
122 throw new UnsupportedOperationException();
126 public void clear() {
127 throw new UnsupportedOperationException();
131 public int hashCode() {
134 result = 31 * result + e.hashCode();
140 public boolean equals(final Object obj) {
141 return obj == this || obj instanceof ConstantArrayCollection<?> other && Arrays.equals(array, other.array);
145 public @NonNull String toString() {
146 if (array.length == 0) {
150 final StringBuilder sb = new StringBuilder("[");
152 while (offset < array.length - 1) {
153 sb.append(String.valueOf(array[offset++])).append(", ");
155 return sb.append(String.valueOf(array[offset])).append(']').toString();
158 private static final class Itr<E> extends UnmodifiableIterator<E> {
159 private final E @NonNull[] array;
160 private int offset = 0;
162 Itr(final E @NonNull[] array) {
167 public boolean hasNext() {
168 return offset < array.length;
173 if (offset >= array.length) {
174 throw new NoSuchElementException();
176 return array[offset++];