Update DataObjectStep class hierarchy
[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.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 org.eclipse.jdt.annotation.NonNull;
21 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
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 @NonNull Class<?> UNMODIFIABLE_COLLECTION_CLASS =
37         Collections.unmodifiableCollection(new ArrayList<>()).getClass();
38     private static final @NonNull ImmutableSet<Class<?>> SINGLETON_CLASSES = ImmutableSet.<Class<?>>builder()
39         .add(Collections.singleton(null).getClass())
40         .add(Collections.singletonList(null).getClass())
41         .build();
42
43     private final @NonNull Collection<E> delegate;
44
45     private UnmodifiableCollection(final @NonNull Collection<E> delegate) {
46         this.delegate = requireNonNull(delegate);
47     }
48
49     /**
50      * Create an unmodifiable view of the target collection. If the instance is known
51      * to be unmodifiable, that instance is returned.
52      *
53      * @param collection Target collection
54      * @return An unmodifiable view of the collection
55      * @throws NullPointerException if {@code collection} is null
56      */
57     @SuppressModernizer
58     public static <T> @NonNull Collection<T> create(final @NonNull Collection<T> collection) {
59         if (collection instanceof UnmodifiableCollection || collection instanceof ImmutableCollection
60                 || Collections.EMPTY_LIST == collection || Collections.EMPTY_SET == collection
61                 || UNMODIFIABLE_COLLECTION_CLASS.isInstance(collection)
62                 || SINGLETON_CLASSES.contains(collection.getClass())) {
63             return collection;
64         }
65
66         return new UnmodifiableCollection<>(collection);
67     }
68
69     @Override
70     public @NonNull Iterator<E> iterator() {
71         return Iterators.unmodifiableIterator(delegate.iterator());
72     }
73
74     @Override
75     public int size() {
76         return delegate.size();
77     }
78
79     @Override
80     public boolean isEmpty() {
81         return delegate.isEmpty();
82     }
83
84     @Override
85     @SuppressWarnings("checkstyle:parameterName")
86     public boolean contains(final Object o) {
87         return delegate.contains(o);
88     }
89
90     @Override
91     public Object[] toArray() {
92         return delegate.toArray();
93     }
94
95     @Override
96     @SuppressWarnings("checkstyle:parameterName")
97     public <T> T[] toArray(final T[] a) {
98         return delegate.toArray(a);
99     }
100
101     @Override
102     @SuppressWarnings("checkstyle:parameterName")
103     public boolean containsAll(final Collection<?> c) {
104         return delegate.containsAll(c);
105     }
106
107     @Override
108     @SuppressWarnings("checkstyle:parameterName")
109     public boolean add(final E e) {
110         throw new UnsupportedOperationException();
111     }
112
113     @Override
114     @SuppressWarnings("checkstyle:parameterName")
115     public boolean addAll(final Collection<? extends E> c) {
116         throw new UnsupportedOperationException();
117     }
118
119     @Override
120     @SuppressWarnings("checkstyle:parameterName")
121     public boolean remove(final Object o) {
122         throw new UnsupportedOperationException();
123     }
124
125     @Override
126     @SuppressWarnings("checkstyle:parameterName")
127     public boolean removeAll(final Collection<?> c) {
128         throw new UnsupportedOperationException();
129     }
130
131     @Override
132     @SuppressWarnings("checkstyle:parameterName")
133     public boolean retainAll(final Collection<?> c) {
134         throw new UnsupportedOperationException();
135     }
136
137     @Override
138     public void clear() {
139         throw new UnsupportedOperationException();
140     }
141
142     @Override
143     public @NonNull String toString() {
144         return "UnmodifiableCollection{" + delegate + "}";
145     }
146 }