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;
22 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
25 * An unmodifiable view over a {@link Collection}. Unlike the view returned via
26 * {@link Collections#unmodifiableCollection(Collection)}, this class checks its
27 * argument to ensure multiple encapsulation does not occur.
29 * <p>This class checks
30 * the argument so it prevents multiple encapsulation. Subclasses of
31 * {@link ImmutableCollection} are also recognized and not encapsulated.
33 * @param <E> the type of elements in this collection
35 public final class UnmodifiableCollection<E> implements Collection<E>, Serializable {
36 private static final long serialVersionUID = 1L;
37 private static final @NonNull Class<?> UNMODIFIABLE_COLLECTION_CLASS;
38 private static final @NonNull Collection<Class<?>> SINGLETON_CLASSES;
41 UNMODIFIABLE_COLLECTION_CLASS = Collections.unmodifiableCollection(new ArrayList<>()).getClass();
43 final Builder<Class<?>> b = ImmutableSet.builder();
44 b.add(Collections.singleton(null).getClass());
45 b.add(Collections.singletonList(null).getClass());
46 SINGLETON_CLASSES = b.build();
49 private final @NonNull Collection<E> delegate;
51 private UnmodifiableCollection(final @NonNull Collection<E> delegate) {
52 this.delegate = requireNonNull(delegate);
56 * Create an unmodifiable view of the target collection. If the instance is known
57 * to be unmodifiable, that instance is returned.
59 * @param collection Target collection
60 * @return An unmodifiable view of the collection
61 * @throws NullPointerException if {@code collection} is null
64 public static <T> @NonNull Collection<T> create(final @NonNull Collection<T> collection) {
65 if (collection instanceof UnmodifiableCollection || collection instanceof ImmutableCollection
66 || Collections.EMPTY_LIST == collection || Collections.EMPTY_SET == collection
67 || UNMODIFIABLE_COLLECTION_CLASS.isInstance(collection)
68 || SINGLETON_CLASSES.contains(collection.getClass())) {
72 return new UnmodifiableCollection<>(collection);
76 public @NonNull Iterator<E> iterator() {
77 return Iterators.unmodifiableIterator(delegate.iterator());
82 return delegate.size();
86 public boolean isEmpty() {
87 return delegate.isEmpty();
91 @SuppressWarnings("checkstyle:parameterName")
92 public boolean contains(final Object o) {
93 return delegate.contains(o);
97 public Object[] toArray() {
98 return delegate.toArray();
102 @SuppressWarnings("checkstyle:parameterName")
103 public <T> T[] toArray(final T[] a) {
104 return delegate.toArray(a);
108 @SuppressWarnings("checkstyle:parameterName")
109 public boolean containsAll(final Collection<?> c) {
110 return delegate.containsAll(c);
114 @SuppressWarnings("checkstyle:parameterName")
115 public boolean add(final E e) {
116 throw new UnsupportedOperationException();
120 @SuppressWarnings("checkstyle:parameterName")
121 public boolean addAll(final Collection<? extends E> c) {
122 throw new UnsupportedOperationException();
126 @SuppressWarnings("checkstyle:parameterName")
127 public boolean remove(final Object o) {
128 throw new UnsupportedOperationException();
132 @SuppressWarnings("checkstyle:parameterName")
133 public boolean removeAll(final Collection<?> c) {
134 throw new UnsupportedOperationException();
138 @SuppressWarnings("checkstyle:parameterName")
139 public boolean retainAll(final Collection<?> c) {
140 throw new UnsupportedOperationException();
144 public void clear() {
145 throw new UnsupportedOperationException();
149 public @NonNull String toString() {
150 return "UnmodifiableCollection{" + delegate + "}";