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