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 public boolean contains(final Object o) {
47 for (Object wlk : array) {
57 public Iterator<E> iterator() {
58 return new UnmodifiableIterator<E>() {
62 public boolean hasNext() {
63 return i < array.length;
68 if (i >= array.length) {
69 throw new NoSuchElementException();
78 public Object[] toArray() {
83 @SuppressWarnings("unchecked")
85 public <T> T[] toArray(@Nonnull final T[] a) {
86 if (a.length < array.length) {
87 return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
90 System.arraycopy(array, 0, a, 0, array.length);
91 if (a.length > array.length) {
92 a[array.length] = null;
98 public boolean add(final E e) {
99 throw new UnsupportedOperationException();
103 public boolean remove(final Object o) {
104 throw new UnsupportedOperationException();
108 public boolean containsAll(@Nonnull final Collection<?> c) {
119 public boolean addAll(@Nonnull final Collection<? extends E> c) {
120 throw new UnsupportedOperationException();
124 public boolean removeAll(@Nonnull final Collection<?> c) {
125 throw new UnsupportedOperationException();
129 public boolean retainAll(@Nonnull final Collection<?> c) {
130 throw new UnsupportedOperationException();
134 public void clear() {
135 throw new UnsupportedOperationException();
139 public int hashCode() {
142 result = 31 * result + e.hashCode();
148 public boolean equals(final Object obj) {
152 if (!(obj instanceof ConstantArrayCollection)) {
156 return Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
160 public String toString() {
161 if (array.length == 0) {
165 final StringBuilder sb = new StringBuilder("[");
167 while (i < array.length - 1) {
168 sb.append(String.valueOf(array[i++])).append(", ");
170 return sb.append(String.valueOf(array[i])).append(']').toString();