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 com.google.common.base.Preconditions;
11 import com.google.common.collect.UnmodifiableIterator;
12 import java.io.Serializable;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.NoSuchElementException;
18 import javax.annotation.Nonnull;
21 * Internal array-backed {@link List}. It assumes the array does not contain nulls and it does not get modified
22 * externally. These assumptions are not checked. It does not allow modification of the underlying array -- thus it
23 * is very useful for use with {@link ImmutableOffsetMap}.
25 * @param <E> the type of elements in this list
27 final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
28 private static final long serialVersionUID = 1L;
29 private final E[] array;
31 ConstantArrayCollection(final E[] array) {
32 this.array = Preconditions.checkNotNull(array);
41 public boolean isEmpty() {
42 return array.length == 0;
46 @SuppressWarnings("checkstyle:parameterName")
47 public boolean contains(final Object o) {
48 for (Object wlk : array) {
58 public Iterator<E> iterator() {
59 return new UnmodifiableIterator<E>() {
60 private int offset = 0;
63 public boolean hasNext() {
64 return offset < array.length;
69 if (offset >= array.length) {
70 throw new NoSuchElementException();
72 return array[offset++];
79 public Object[] toArray() {
84 @SuppressWarnings({ "unchecked", "checkstyle:parameterName" })
86 public <T> T[] toArray(@Nonnull final T[] a) {
87 if (a.length < array.length) {
88 return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
91 System.arraycopy(array, 0, a, 0, array.length);
92 if (a.length > array.length) {
93 a[array.length] = null;
99 @SuppressWarnings("checkstyle:parameterName")
100 public boolean add(final E e) {
101 throw new UnsupportedOperationException();
105 @SuppressWarnings("checkstyle:parameterName")
106 public boolean remove(final Object o) {
107 throw new UnsupportedOperationException();
111 @SuppressWarnings("checkstyle:parameterName")
112 public boolean containsAll(@Nonnull final Collection<?> c) {
123 @SuppressWarnings("checkstyle:parameterName")
124 public boolean addAll(@Nonnull final Collection<? extends E> c) {
125 throw new UnsupportedOperationException();
129 @SuppressWarnings("checkstyle:parameterName")
130 public boolean removeAll(@Nonnull final Collection<?> c) {
131 throw new UnsupportedOperationException();
135 @SuppressWarnings("checkstyle:parameterName")
136 public boolean retainAll(@Nonnull final Collection<?> c) {
137 throw new UnsupportedOperationException();
141 public void clear() {
142 throw new UnsupportedOperationException();
146 public int hashCode() {
149 result = 31 * result + e.hashCode();
155 public boolean equals(final Object obj) {
159 if (!(obj instanceof ConstantArrayCollection)) {
163 return Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
167 public String toString() {
168 if (array.length == 0) {
172 final StringBuilder sb = new StringBuilder("[");
174 while (offset < array.length - 1) {
175 sb.append(String.valueOf(array[offset++])).append(", ");
177 return sb.append(String.valueOf(array[offset])).append(']').toString();