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 UnmodifiableIterator<E>() {
61 private int offset = 0;
64 public boolean hasNext() {
65 return offset < array.length;
70 if (offset >= array.length) {
71 throw new NoSuchElementException();
73 return array[offset++];
80 public Object[] toArray() {
85 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
87 public <T> T[] toArray(@Nonnull final T[] a) {
88 if (a.length < array.length) {
89 return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
92 System.arraycopy(array, 0, a, 0, array.length);
93 if (a.length > array.length) {
94 a[array.length] = null;
100 @SuppressWarnings("checkstyle:parameterName")
101 public boolean add(final E e) {
102 throw new UnsupportedOperationException();
106 @SuppressWarnings("checkstyle:parameterName")
107 public boolean remove(final Object o) {
108 throw new UnsupportedOperationException();
112 @SuppressWarnings("checkstyle:parameterName")
113 public boolean containsAll(@Nonnull final Collection<?> c) {
124 @SuppressWarnings("checkstyle:parameterName")
125 public boolean addAll(@Nonnull final Collection<? extends E> c) {
126 throw new UnsupportedOperationException();
130 @SuppressWarnings("checkstyle:parameterName")
131 public boolean removeAll(@Nonnull final Collection<?> c) {
132 throw new UnsupportedOperationException();
136 @SuppressWarnings("checkstyle:parameterName")
137 public boolean retainAll(@Nonnull final Collection<?> c) {
138 throw new UnsupportedOperationException();
142 public void clear() {
143 throw new UnsupportedOperationException();
147 public int hashCode() {
150 result = 31 * result + e.hashCode();
156 public boolean equals(final Object obj) {
160 if (!(obj instanceof ConstantArrayCollection)) {
164 return Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
168 public String toString() {
169 if (array.length == 0) {
173 final StringBuilder sb = new StringBuilder("[");
175 while (offset < array.length - 1) {
176 sb.append(String.valueOf(array[offset++])).append(", ");
178 return sb.append(String.valueOf(array[offset])).append(']').toString();