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.Iterators;
15 import java.io.Serializable;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Iterator;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
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 Collections.unmodifiableCollection(new ArrayList<>()).getClass();
38 private static final @NonNull ImmutableSet<Class<?>> SINGLETON_CLASSES = ImmutableSet.<Class<?>>builder()
39 .add(Collections.singleton(null).getClass())
40 .add(Collections.singletonList(null).getClass())
43 private final @NonNull Collection<E> delegate;
45 private UnmodifiableCollection(final @NonNull Collection<E> delegate) {
46 this.delegate = requireNonNull(delegate);
50 * Create an unmodifiable view of the target collection. If the instance is known
51 * to be unmodifiable, that instance is returned.
53 * @param collection Target collection
54 * @return An unmodifiable view of the collection
55 * @throws NullPointerException if {@code collection} is null
58 public static <T> @NonNull Collection<T> create(final @NonNull Collection<T> collection) {
59 if (collection instanceof UnmodifiableCollection || collection instanceof ImmutableCollection
60 || Collections.EMPTY_LIST == collection || Collections.EMPTY_SET == collection
61 || UNMODIFIABLE_COLLECTION_CLASS.isInstance(collection)
62 || SINGLETON_CLASSES.contains(collection.getClass())) {
66 return new UnmodifiableCollection<>(collection);
70 public @NonNull Iterator<E> iterator() {
71 return Iterators.unmodifiableIterator(delegate.iterator());
76 return delegate.size();
80 public boolean isEmpty() {
81 return delegate.isEmpty();
85 @SuppressWarnings("checkstyle:parameterName")
86 public boolean contains(final Object o) {
87 return delegate.contains(o);
91 public Object[] toArray() {
92 return delegate.toArray();
96 @SuppressWarnings("checkstyle:parameterName")
97 public <T> T[] toArray(final T[] a) {
98 return delegate.toArray(a);
102 @SuppressWarnings("checkstyle:parameterName")
103 public boolean containsAll(final Collection<?> c) {
104 return delegate.containsAll(c);
108 @SuppressWarnings("checkstyle:parameterName")
109 public boolean add(final E e) {
110 throw new UnsupportedOperationException();
114 @SuppressWarnings("checkstyle:parameterName")
115 public boolean addAll(final Collection<? extends E> c) {
116 throw new UnsupportedOperationException();
120 @SuppressWarnings("checkstyle:parameterName")
121 public boolean remove(final Object o) {
122 throw new UnsupportedOperationException();
126 @SuppressWarnings("checkstyle:parameterName")
127 public boolean removeAll(final Collection<?> c) {
128 throw new UnsupportedOperationException();
132 @SuppressWarnings("checkstyle:parameterName")
133 public boolean retainAll(final Collection<?> c) {
134 throw new UnsupportedOperationException();
138 public void clear() {
139 throw new UnsupportedOperationException();
143 public @NonNull String toString() {
144 return "UnmodifiableCollection{" + delegate + "}";