XML -> NormalizedNodes parser identity-ref/instance-id/leafref support
[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(final @Nonnull 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) || SINGLETON_CLASSES.contains(collection.getClass())) {
65             return collection;
66         }
67
68         return new UnmodifiableCollection<>(collection);
69     }
70
71     @Override
72     public Iterator<E> iterator() {
73         return Iterators.unmodifiableIterator(delegate.iterator());
74     }
75
76     @Override
77     public int size() {
78         return delegate.size();
79     }
80
81     @Override
82     public boolean isEmpty() {
83         return delegate.isEmpty();
84     }
85
86     @Override
87     public boolean contains(final Object o) {
88         return delegate.contains(o);
89     }
90
91     @Override
92     public Object[] toArray() {
93         return delegate.toArray();
94     }
95
96     @Override
97     public <T> T[] toArray(final T[] a) {
98         return delegate.toArray(a);
99     }
100
101     @Override
102     public boolean containsAll(final Collection<?> c) {
103         return delegate.containsAll(c);
104     }
105
106     @Override
107     public boolean add(final E e) {
108         throw new UnsupportedOperationException();
109     }
110
111     @Override
112     public boolean addAll(final Collection<? extends E> c) {
113         throw new UnsupportedOperationException();
114     }
115
116     @Override
117     public boolean remove(final Object o) {
118         throw new UnsupportedOperationException();
119     }
120
121     @Override
122     public boolean removeAll(final Collection<?> c) {
123         throw new UnsupportedOperationException();
124     }
125
126     @Override
127     public boolean retainAll(final Collection<?> c) {
128         throw new UnsupportedOperationException();
129     }
130
131     @Override
132     public void clear() {
133         throw new UnsupportedOperationException();
134     }
135
136     @Override
137     public String toString() {
138         final StringBuffer sb = new StringBuffer("UnmodifiableCollection{");
139         sb.append(delegate);
140         return sb.toString();
141     }
142 }