Merge branch 'master' of ../controller
[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 static java.util.Objects.requireNonNull;
11
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;
23
24 /**
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.
28  *
29  * <p>This class checks
30  * the argument so it prevents multiple encapsulation. Subclasses of
31  * {@link ImmutableCollection} are also recognized and not encapsulated.
32  *
33  * @param <E> the type of elements in this collection
34  */
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;
39
40     static {
41         UNMODIFIABLE_COLLECTION_CLASS = Collections.unmodifiableCollection(new ArrayList<>()).getClass();
42
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();
47     }
48
49     private final @NonNull Collection<E> delegate;
50
51     private UnmodifiableCollection(final @NonNull Collection<E> delegate) {
52         this.delegate = requireNonNull(delegate);
53     }
54
55     /**
56      * Create an unmodifiable view of the target collection. If the instance is known
57      * to be unmodifiable, that instance is returned.
58      *
59      * @param collection Target collection
60      * @return An unmodifiable view of the collection
61      * @throws NullPointerException if {@code collection} is null
62      */
63     @SuppressModernizer
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())) {
69             return collection;
70         }
71
72         return new UnmodifiableCollection<>(collection);
73     }
74
75     @Override
76     public @NonNull Iterator<E> iterator() {
77         return Iterators.unmodifiableIterator(delegate.iterator());
78     }
79
80     @Override
81     public int size() {
82         return delegate.size();
83     }
84
85     @Override
86     public boolean isEmpty() {
87         return delegate.isEmpty();
88     }
89
90     @Override
91     @SuppressWarnings("checkstyle:parameterName")
92     public boolean contains(final Object o) {
93         return delegate.contains(o);
94     }
95
96     @Override
97     public Object[] toArray() {
98         return delegate.toArray();
99     }
100
101     @Override
102     @SuppressWarnings("checkstyle:parameterName")
103     public <T> T[] toArray(final T[] a) {
104         return delegate.toArray(a);
105     }
106
107     @Override
108     @SuppressWarnings("checkstyle:parameterName")
109     public boolean containsAll(final Collection<?> c) {
110         return delegate.containsAll(c);
111     }
112
113     @Override
114     @SuppressWarnings("checkstyle:parameterName")
115     public boolean add(final E e) {
116         throw new UnsupportedOperationException();
117     }
118
119     @Override
120     @SuppressWarnings("checkstyle:parameterName")
121     public boolean addAll(final Collection<? extends E> c) {
122         throw new UnsupportedOperationException();
123     }
124
125     @Override
126     @SuppressWarnings("checkstyle:parameterName")
127     public boolean remove(final Object o) {
128         throw new UnsupportedOperationException();
129     }
130
131     @Override
132     @SuppressWarnings("checkstyle:parameterName")
133     public boolean removeAll(final Collection<?> c) {
134         throw new UnsupportedOperationException();
135     }
136
137     @Override
138     @SuppressWarnings("checkstyle:parameterName")
139     public boolean retainAll(final Collection<?> c) {
140         throw new UnsupportedOperationException();
141     }
142
143     @Override
144     public void clear() {
145         throw new UnsupportedOperationException();
146     }
147
148     @Override
149     public @NonNull String toString() {
150         return "UnmodifiableCollection{" + delegate + "}";
151     }
152 }