Seal NormalizedNode hierarchy
[yangtools.git] / data / 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 || map instanceof ImmutableMap) {
53             return map;
54         }
55         if (map.isEmpty()) {
56             return ImmutableMap.of();
57         }
58         if (map.size() < WRAP_THRESHOLD) {
59             return ImmutableMap.copyOf(map);
60         }
61         return new UnmodifiableChildrenMap(map);
62     }
63
64     @Override
65     public int size() {
66         return delegate.size();
67     }
68
69     @Override
70     public boolean isEmpty() {
71         return delegate.isEmpty();
72     }
73
74     @Override
75     public boolean containsKey(final Object key) {
76         return delegate.containsKey(key);
77     }
78
79     @Override
80     public boolean containsValue(final Object value) {
81         return delegate.containsValue(value);
82     }
83
84     @Override
85     public DataContainerChild get(final Object key) {
86         return delegate.get(key);
87     }
88
89     @Override
90     public DataContainerChild put(final PathArgument key, final DataContainerChild value) {
91         throw new UnsupportedOperationException();
92     }
93
94     @Override
95     public DataContainerChild remove(final Object key) {
96         throw new UnsupportedOperationException();
97     }
98
99     @Override
100     @SuppressWarnings("checkstyle:parameterName")
101     public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild> m) {
102         throw new UnsupportedOperationException();
103     }
104
105     @Override
106     public void clear() {
107         throw new UnsupportedOperationException();
108     }
109
110     @Override
111     public Set<PathArgument> keySet() {
112         return Collections.unmodifiableSet(delegate.keySet());
113     }
114
115     @Override
116     public Collection<DataContainerChild> values() {
117         if (values == null) {
118             values = Collections.unmodifiableCollection(delegate.values());
119         }
120         return values;
121     }
122
123     @Override
124     public Set<Entry<PathArgument, DataContainerChild>> entrySet() {
125         /*
126          * Okay, this is not as efficient as it could be -- we could save ourselves the
127          * map instantiation. The cost of that would be re-implementation of a read-only
128          * Map.Entry to ensure our delegate is never modified.
129          *
130          * Let's skip that and use whatever the JRE gives us instead.
131          */
132         return Collections.unmodifiableMap(delegate).entrySet();
133     }
134
135     @Override
136     public boolean equals(final Object obj) {
137         return this == obj || delegate.equals(obj);
138     }
139
140     @Override
141     public int hashCode() {
142         return delegate.hashCode();
143     }
144
145     @Override
146     public String toString() {
147         return delegate.toString();
148     }
149
150     @Override
151     @SuppressWarnings("unchecked")
152     public Map<PathArgument, DataContainerChild> createMutableClone() {
153         if (delegate instanceof HashMap<?, ?> hashMap) {
154             return (Map<PathArgument, DataContainerChild>) hashMap.clone();
155         }
156         return new HashMap<>(delegate);
157     }
158 }