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