2 * Copyright (c) 2013 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.ImmutableCollection;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.ImmutableSet.Builder;
15 import com.google.common.collect.Iterators;
16 import java.io.Serializable;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Iterator;
21 import org.eclipse.jdt.annotation.NonNull;
24 * An unmodifiable view over a {@link Collection}. Unlike the view returned via
25 * {@link Collections#unmodifiableCollection(Collection)}, this class checks its
26 * argument to ensure multiple encapsulation does not occur.
28 * <p>This class checks
29 * the argument so it prevents multiple encapsulation. Subclasses of
30 * {@link ImmutableCollection} are also recognized and not encapsulated.
32 * @param <E> the type of elements in this collection
34 public final class UnmodifiableCollection<E> implements Collection<E>, Serializable {
35 private static final long serialVersionUID = 1L;
36 private static final @NonNull Class<?> UNMODIFIABLE_COLLECTION_CLASS;
37 private static final @NonNull Collection<Class<?>> SINGLETON_CLASSES;
40 UNMODIFIABLE_COLLECTION_CLASS = Collections.unmodifiableCollection(new ArrayList<>()).getClass();
42 final Builder<Class<?>> b = ImmutableSet.builder();
43 b.add(Collections.singleton(null).getClass());
44 b.add(Collections.singletonList(null).getClass());
45 SINGLETON_CLASSES = b.build();
48 private final @NonNull Collection<E> delegate;
50 private UnmodifiableCollection(final @NonNull Collection<E> delegate) {
51 this.delegate = requireNonNull(delegate);
55 * Create an unmodifiable view of the target collection. If the instance is known
56 * to be unmodifiable, that instance is returned.
58 * @param collection Target collection
59 * @return An unmodifiable view of the collection
60 * @throws NullPointerException if {@code collection} is null
62 public static <T> @NonNull Collection<T> create(final @NonNull Collection<T> collection) {
63 if (collection instanceof UnmodifiableCollection || collection instanceof ImmutableCollection
64 || Collections.EMPTY_LIST == collection || Collections.EMPTY_SET == collection
65 || UNMODIFIABLE_COLLECTION_CLASS.isInstance(collection)
66 || SINGLETON_CLASSES.contains(collection.getClass())) {
70 return new UnmodifiableCollection<>(collection);
74 public @NonNull Iterator<E> iterator() {
75 return Iterators.unmodifiableIterator(delegate.iterator());
80 return delegate.size();
84 public boolean isEmpty() {
85 return delegate.isEmpty();
89 @SuppressWarnings("checkstyle:parameterName")
90 public boolean contains(final Object o) {
91 return delegate.contains(o);
95 public Object[] toArray() {
96 return delegate.toArray();
100 @SuppressWarnings("checkstyle:parameterName")
101 public <T> T[] toArray(final T[] a) {
102 return delegate.toArray(a);
106 @SuppressWarnings("checkstyle:parameterName")
107 public boolean containsAll(final Collection<?> c) {
108 return delegate.containsAll(c);
112 @SuppressWarnings("checkstyle:parameterName")
113 public boolean add(final E e) {
114 throw new UnsupportedOperationException();
118 @SuppressWarnings("checkstyle:parameterName")
119 public boolean addAll(final Collection<? extends E> c) {
120 throw new UnsupportedOperationException();
124 @SuppressWarnings("checkstyle:parameterName")
125 public boolean remove(final Object o) {
126 throw new UnsupportedOperationException();
130 @SuppressWarnings("checkstyle:parameterName")
131 public boolean removeAll(final Collection<?> c) {
132 throw new UnsupportedOperationException();
136 @SuppressWarnings("checkstyle:parameterName")
137 public boolean retainAll(final Collection<?> c) {
138 throw new UnsupportedOperationException();
142 public void clear() {
143 throw new UnsupportedOperationException();
147 public @NonNull String toString() {
148 return "UnmodifiableCollection{" + delegate + "}";