Merge "Bug 2894 - Yang Data Codec Gson: null pointer exception when trying to deseria...
[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  * this class checks
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
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<Object>()).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 = Preconditions.checkNotNull(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     @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(final T[] a) {
99         return delegate.toArray(a);
100     }
101
102     @Override
103     public boolean containsAll(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(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(final Collection<?> c) {
124         throw new UnsupportedOperationException();
125     }
126
127     @Override
128     public boolean retainAll(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         final StringBuffer sb = new StringBuffer("UnmodifiableCollection{");
140         sb.append(delegate);
141         return sb.toString();
142     }
143 }