Enable SpotBugs enforcement in yang-data-impl
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / UnmodifiableChildrenMap.java
1 /*
2  * Copyright (c) 2014 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.yang.data.impl.schema.nodes;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Serializable;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
22
23 /**
24  * Internal equivalent of {@link Collections}' unmodifiable Map. It does not retain
25  * keySet/entrySet references, thus lowering the memory overhead.
26  */
27 final class UnmodifiableChildrenMap
28         implements CloneableMap<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
29     private static final long serialVersionUID = 1L;
30
31     /*
32      * Do not wrap maps which are smaller than this and instead copy them into an ImmutableMap.
33      */
34     private static final int WRAP_THRESHOLD = 9;
35
36     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Delegate is expected to be Serializable")
37     private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
38
39     private transient Collection<DataContainerChild<? extends PathArgument, ?>> values = null;
40
41     private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate) {
42         this.delegate = requireNonNull(delegate);
43     }
44
45     /**
46      * Create an unmodifiable view of a particular map. Does not perform unnecessary
47      * encapsulation if the map is known to be already unmodifiable.
48      *
49      * @param map Backing map
50      * @return Unmodifiable view
51      */
52     static Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> create(
53             final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> map) {
54         if (map instanceof UnmodifiableChildrenMap) {
55             return map;
56         }
57         if (map instanceof ImmutableMap) {
58             return map;
59         }
60         if (map.isEmpty()) {
61             return ImmutableMap.of();
62         }
63         if (map.size() < WRAP_THRESHOLD) {
64             return ImmutableMap.copyOf(map);
65         }
66
67         return new UnmodifiableChildrenMap(map);
68     }
69
70     @Override
71     public int size() {
72         return delegate.size();
73     }
74
75     @Override
76     public boolean isEmpty() {
77         return delegate.isEmpty();
78     }
79
80     @Override
81     public boolean containsKey(final Object key) {
82         return delegate.containsKey(key);
83     }
84
85     @Override
86     public boolean containsValue(final Object value) {
87         return delegate.containsValue(value);
88     }
89
90     @Override
91     public DataContainerChild<? extends PathArgument, ?> get(final Object key) {
92         return delegate.get(key);
93     }
94
95     @Override
96     public DataContainerChild<? extends PathArgument, ?> put(final PathArgument key,
97             final DataContainerChild<? extends PathArgument, ?> value) {
98         throw new UnsupportedOperationException();
99     }
100
101     @Override
102     public DataContainerChild<? extends PathArgument, ?> remove(final Object key) {
103         throw new UnsupportedOperationException();
104     }
105
106     @Override
107     @SuppressWarnings("checkstyle:parameterName")
108     public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild<? extends PathArgument, ?>> m) {
109         throw new UnsupportedOperationException();
110     }
111
112     @Override
113     public void clear() {
114         throw new UnsupportedOperationException();
115     }
116
117     @Override
118     public Set<PathArgument> keySet() {
119         return Collections.unmodifiableSet(delegate.keySet());
120     }
121
122     @Override
123     public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
124         if (values == null) {
125             values = Collections.unmodifiableCollection(delegate.values());
126         }
127         return values;
128     }
129
130     @Override
131     public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
132         /*
133          * Okay, this is not as efficient as it could be -- we could save ourselves the
134          * map instantiation. The cost of that would be re-implementation of a read-only
135          * Map.Entry to ensure our delegate is never modified.
136          *
137          * Let's skip that and use whatever the JRE gives us instead.
138          */
139         return Collections.unmodifiableMap(delegate).entrySet();
140     }
141
142     @Override
143     public boolean equals(final Object obj) {
144         return this == obj || delegate.equals(obj);
145     }
146
147     @Override
148     public int hashCode() {
149         return delegate.hashCode();
150     }
151
152     @Override
153     public String toString() {
154         return delegate.toString();
155     }
156
157     @Override
158     @SuppressWarnings("unchecked")
159     public Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> createMutableClone() {
160         if (delegate instanceof HashMap) {
161             return (Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>)
162                     ((HashMap<?, ?>) delegate).clone();
163         }
164
165         return new HashMap<>(delegate);
166     }
167 }