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;
20 * Internal array-backed {@link List}. It assumes the array does not contain nulls and it does not get modified
21 * externally. These assumptions are not checked. It does not allow modification of the underlying array -- thus it
22 * is very useful for use with {@link ImmutableOffsetMap}.
24 * @param <E> the type of elements in this list
26 final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
27 private static final long serialVersionUID = 1L;
28 private final E[] array;
30 ConstantArrayCollection(final E[] array) {
31 this.array = Preconditions.checkNotNull(array);
40 public boolean isEmpty() {
41 return array.length == 0;
45 public boolean contains(final Object o) {
46 for (Object wlk : array) {
55 public Iterator<E> iterator() {
56 return new UnmodifiableIterator<E>() {
60 public boolean hasNext() {
61 return i < array.length;
66 if (i >= array.length) {
67 throw new NoSuchElementException();
75 public Object[] toArray() {
79 @SuppressWarnings("unchecked")
81 public <T> T[] toArray(final T[] a) {
82 if (a.length < array.length) {
83 return Arrays.copyOf(array, array.length, (Class<T[]>)a.getClass().getComponentType());
86 System.arraycopy(array, 0, a, 0, array.length);
87 if (a.length > array.length) {
88 a[array.length] = null;
94 public boolean add(final E e) {
95 throw new UnsupportedOperationException();
99 public boolean remove(final Object o) {
100 throw new UnsupportedOperationException();
104 public boolean containsAll(final Collection<?> c) {
115 public boolean addAll(final Collection<? extends E> c) {
116 throw new UnsupportedOperationException();
120 public boolean removeAll(final Collection<?> c) {
121 throw new UnsupportedOperationException();
125 public boolean retainAll(final Collection<?> c) {
126 throw new UnsupportedOperationException();
130 public void clear() {
131 throw new UnsupportedOperationException();