Merge "Added missing copyright headers to yang-data-impl"
[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     private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
27     private transient Collection<DataContainerChild<? extends PathArgument, ?>> values;
28
29     private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate) {
30         this.delegate = Preconditions.checkNotNull(delegate);
31     }
32
33     /**
34      * Create an unmodifiable view of a particular map. Does not perform unnecessary
35      * encapsulation if the map is known to be already unmodifiable.
36      *
37      * @param map Backing map
38      * @return Unmodifiable view
39      */
40     static Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> create(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> map) {
41         if (map instanceof UnmodifiableChildrenMap) {
42             return map;
43         }
44         if (map instanceof ImmutableMap) {
45             return map;
46         }
47         if (map.isEmpty()) {
48             return Collections.emptyMap();
49         }
50
51         return new UnmodifiableChildrenMap(map);
52     }
53
54     @Override
55     public int size() {
56         return delegate.size();
57     }
58
59     @Override
60     public boolean isEmpty() {
61         return delegate.isEmpty();
62     }
63
64     @Override
65     public boolean containsKey(final Object key) {
66         return delegate.containsKey(key);
67     }
68
69     @Override
70     public boolean containsValue(final Object value) {
71         return delegate.containsValue(value);
72     }
73
74     @Override
75     public DataContainerChild<? extends PathArgument, ?> get(final Object key) {
76         return delegate.get(key);
77     }
78
79     @Override
80     public DataContainerChild<? extends PathArgument, ?> put(final PathArgument key,
81             final DataContainerChild<? extends PathArgument, ?> value) {
82         throw new UnsupportedOperationException();
83     }
84
85     @Override
86     public DataContainerChild<? extends PathArgument, ?> remove(final Object key) {
87         throw new UnsupportedOperationException();
88     }
89
90     @Override
91     public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild<? extends PathArgument, ?>> m) {
92         throw new UnsupportedOperationException();
93     }
94
95     @Override
96     public void clear() {
97         throw new UnsupportedOperationException();
98     }
99
100     @Override
101     public Set<PathArgument> keySet() {
102         return Collections.unmodifiableSet(delegate.keySet());
103     }
104
105     @Override
106     public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
107         if (values == null) {
108             values = Collections.unmodifiableCollection(delegate.values());
109         }
110         return values;
111     }
112
113     @Override
114     public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
115         /*
116          * Okay, this is not as efficient as it could be -- we could save ourselves the
117          * map instantiation. The cost of that would be re-implementation of a read-only
118          * Map.Entry to ensure our delegate is never modified.
119          *
120          * Let's skip that and use whatever the JRE gives us instead.
121          */
122         return Collections.unmodifiableMap(delegate).entrySet();
123     }
124
125     @Override
126     public boolean equals(final Object o) {
127         return this == o || delegate.equals(o);
128     }
129
130     @Override
131     public int hashCode() {
132         return delegate.hashCode();
133     }
134
135     @Override
136     public String toString() {
137         return delegate.toString();
138     }
139 }