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