Cleanup checkstyle warnings and turn enforcement on in 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.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18 import javax.annotation.Nonnull;
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 = Preconditions.checkNotNull(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     @Nonnull
119     @Override
120     public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
121         if (values == null) {
122             values = Collections.unmodifiableCollection(delegate.values());
123         }
124         return values;
125     }
126
127     @Override
128     public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
129         /*
130          * Okay, this is not as efficient as it could be -- we could save ourselves the
131          * map instantiation. The cost of that would be re-implementation of a read-only
132          * Map.Entry to ensure our delegate is never modified.
133          *
134          * Let's skip that and use whatever the JRE gives us instead.
135          */
136         return Collections.unmodifiableMap(delegate).entrySet();
137     }
138
139     @Override
140     public boolean equals(final Object obj) {
141         return this == obj || delegate.equals(obj);
142     }
143
144     @Override
145     public int hashCode() {
146         return delegate.hashCode();
147     }
148
149     @Override
150     public String toString() {
151         return delegate.toString();
152     }
153
154     @Override
155     @SuppressWarnings("unchecked")
156     public Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> createMutableClone() {
157         if (delegate instanceof HashMap) {
158             return (Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>)
159                     ((HashMap<?, ?>) delegate).clone();
160         }
161
162         return new HashMap<>(delegate);
163     }
164 }