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