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(@Nonnull final 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)
65 || SINGLETON_CLASSES.contains(collection.getClass())) {
69 return new UnmodifiableCollection<>(collection);
73 public Iterator<E> iterator() {
74 return Iterators.unmodifiableIterator(delegate.iterator());
79 return delegate.size();
83 public boolean isEmpty() {
84 return delegate.isEmpty();
88 public boolean contains(final Object o) {
89 return delegate.contains(o);
93 public Object[] toArray() {
94 return delegate.toArray();
98 public <T> T[] toArray(final T[] a) {
99 return delegate.toArray(a);
103 public boolean containsAll(final Collection<?> c) {
104 return delegate.containsAll(c);
108 public boolean add(final E e) {
109 throw new UnsupportedOperationException();
113 public boolean addAll(final Collection<? extends E> c) {
114 throw new UnsupportedOperationException();
118 public boolean remove(final Object o) {
119 throw new UnsupportedOperationException();
123 public boolean removeAll(final Collection<?> c) {
124 throw new UnsupportedOperationException();
128 public boolean retainAll(final Collection<?> c) {
129 throw new UnsupportedOperationException();
133 public void clear() {
134 throw new UnsupportedOperationException();
138 public String toString() {
139 final StringBuffer sb = new StringBuffer("UnmodifiableCollection{");
141 return sb.toString();