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