Propagate @Nonnull and @Nullable annotations
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / UnmodifiableCollection.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.util;
9
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;
21
22 /**
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.
26  *
27  * <p>This class checks
28  * the argument so it prevents multiple encapsulation. Subclasses of
29  * {@link ImmutableCollection} are also recognized and not encapsulated.
30  *
31  * @param <E> the type of elements in this collection
32  */
33 public final class UnmodifiableCollection<E> implements Collection<E>, Serializable {
34     private static final long serialVersionUID = 1L;
35     private static final Class<?> UNMODIFIABLE_COLLECTION_CLASS;
36     private static final Collection<Class<?>> SINGLETON_CLASSES;
37
38     static {
39         UNMODIFIABLE_COLLECTION_CLASS = Collections.unmodifiableCollection(new ArrayList<>()).getClass();
40
41         final Builder<Class<?>> b = ImmutableSet.builder();
42         b.add(Collections.singleton(null).getClass());
43         b.add(Collections.singletonList(null).getClass());
44         SINGLETON_CLASSES = b.build();
45     }
46
47     private final Collection<E> delegate;
48
49     private UnmodifiableCollection(final Collection<E> delegate) {
50         this.delegate = Preconditions.checkNotNull(delegate);
51     }
52
53     /**
54      * Create an unmodifiable view of the target collection. If the instance is known
55      * to be unmodifiable, that instance is returned.
56      *
57      * @param collection Target collection
58      * @return An unmodifiable view of the collection
59      */
60     public static <T> Collection<T> create(@Nonnull final Collection<T> collection) {
61         if (collection instanceof UnmodifiableCollection || collection instanceof ImmutableCollection
62                 || Collections.EMPTY_LIST == collection || Collections.EMPTY_SET == collection
63                 || UNMODIFIABLE_COLLECTION_CLASS.isInstance(collection)
64                 || SINGLETON_CLASSES.contains(collection.getClass())) {
65             return collection;
66         }
67
68         return new UnmodifiableCollection<>(collection);
69     }
70
71     @Nonnull
72     @Override
73     public Iterator<E> iterator() {
74         return Iterators.unmodifiableIterator(delegate.iterator());
75     }
76
77     @Override
78     public int size() {
79         return delegate.size();
80     }
81
82     @Override
83     public boolean isEmpty() {
84         return delegate.isEmpty();
85     }
86
87     @Override
88     public boolean contains(final Object o) {
89         return delegate.contains(o);
90     }
91
92     @Override
93     public Object[] toArray() {
94         return delegate.toArray();
95     }
96
97     @Override
98     public <T> T[] toArray(@Nonnull final T[] a) {
99         return delegate.toArray(a);
100     }
101
102     @Override
103     public boolean containsAll(@Nonnull final Collection<?> c) {
104         return delegate.containsAll(c);
105     }
106
107     @Override
108     public boolean add(final E e) {
109         throw new UnsupportedOperationException();
110     }
111
112     @Override
113     public boolean addAll(@Nonnull final Collection<? extends E> c) {
114         throw new UnsupportedOperationException();
115     }
116
117     @Override
118     public boolean remove(final Object o) {
119         throw new UnsupportedOperationException();
120     }
121
122     @Override
123     public boolean removeAll(@Nonnull final Collection<?> c) {
124         throw new UnsupportedOperationException();
125     }
126
127     @Override
128     public boolean retainAll(@Nonnull final Collection<?> c) {
129         throw new UnsupportedOperationException();
130     }
131
132     @Override
133     public void clear() {
134         throw new UnsupportedOperationException();
135     }
136
137     @Override
138     public String toString() {
139         return "UnmodifiableCollection{" + delegate + "}";
140     }
141 }