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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableCollection;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.ImmutableSet.Builder;
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 javax.annotation.Nonnull;
23 * An unmodifiable view over a {@link Collection}. Unlike the view returned via
24 * {@link Collections#unmodifiableCollection(Collection)}, this class checks its
25 * argument to ensure multiple encapsulation does not occur.
28 * the argument so it prevents multiple encapsulation. Subclasses of
29 * {@link ImmutableCollection} are also recognized and not encapsulated.
30 * An attempt is also made to identi
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 Class<?> UNMODIFIABLE_COLLECTION_CLASS;
37 private static final Collection<Class<?>> SINGLETON_CLASSES;
40 UNMODIFIABLE_COLLECTION_CLASS = Collections.unmodifiableCollection(new ArrayList<Object>()).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 Collection<E> delegate;
50 private UnmodifiableCollection(final Collection<E> delegate) {
51 this.delegate = Preconditions.checkNotNull(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
61 public static <T> Collection<T> create(final @Nonnull Collection<T> collection) {
62 if (collection instanceof UnmodifiableCollection || collection instanceof ImmutableCollection ||
63 Collections.EMPTY_LIST == collection || Collections.EMPTY_SET == collection ||
64 UNMODIFIABLE_COLLECTION_CLASS.isInstance(collection) || SINGLETON_CLASSES.contains(collection.getClass())) {
68 return new UnmodifiableCollection<>(collection);
72 public Iterator<E> iterator() {
73 return Iterators.unmodifiableIterator(delegate.iterator());
78 return delegate.size();
82 public boolean isEmpty() {
83 return delegate.isEmpty();
87 public boolean contains(final Object o) {
88 return delegate.contains(o);
92 public Object[] toArray() {
93 return delegate.toArray();
97 public <T> T[] toArray(final T[] a) {
98 return delegate.toArray(a);
102 public boolean containsAll(final Collection<?> c) {
103 return delegate.containsAll(c);
107 public boolean add(final E e) {
108 throw new UnsupportedOperationException();
112 public boolean addAll(final Collection<? extends E> c) {
113 throw new UnsupportedOperationException();
117 public boolean remove(final Object o) {
118 throw new UnsupportedOperationException();
122 public boolean removeAll(final Collection<?> c) {
123 throw new UnsupportedOperationException();
127 public boolean retainAll(final Collection<?> c) {
128 throw new UnsupportedOperationException();
132 public void clear() {
133 throw new UnsupportedOperationException();
137 public String toString() {
138 final StringBuffer sb = new StringBuffer("UnmodifiableCollection{");
140 return sb.toString();