2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Serializable;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 * Internal equivalent of {@link Collections}' unmodifiable Map. It does not retain
25 * keySet/entrySet references, thus lowering the memory overhead.
27 final class UnmodifiableChildrenMap
28 implements CloneableMap<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
29 private static final long serialVersionUID = 1L;
32 * Do not wrap maps which are smaller than this and instead copy them into an ImmutableMap.
34 private static final int WRAP_THRESHOLD = 9;
36 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Delegate is expected to be Serializable")
37 private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
39 private transient Collection<DataContainerChild<? extends PathArgument, ?>> values = null;
41 private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate) {
42 this.delegate = requireNonNull(delegate);
46 * Create an unmodifiable view of a particular map. Does not perform unnecessary
47 * encapsulation if the map is known to be already unmodifiable.
49 * @param map Backing map
50 * @return Unmodifiable view
52 static Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> create(
53 final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> map) {
54 if (map instanceof UnmodifiableChildrenMap) {
57 if (map instanceof ImmutableMap) {
61 return ImmutableMap.of();
63 if (map.size() < WRAP_THRESHOLD) {
64 return ImmutableMap.copyOf(map);
67 return new UnmodifiableChildrenMap(map);
72 return delegate.size();
76 public boolean isEmpty() {
77 return delegate.isEmpty();
81 public boolean containsKey(final Object key) {
82 return delegate.containsKey(key);
86 public boolean containsValue(final Object value) {
87 return delegate.containsValue(value);
91 public DataContainerChild<? extends PathArgument, ?> get(final Object key) {
92 return delegate.get(key);
96 public DataContainerChild<? extends PathArgument, ?> put(final PathArgument key,
97 final DataContainerChild<? extends PathArgument, ?> value) {
98 throw new UnsupportedOperationException();
102 public DataContainerChild<? extends PathArgument, ?> remove(final Object key) {
103 throw new UnsupportedOperationException();
107 @SuppressWarnings("checkstyle:parameterName")
108 public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild<? extends PathArgument, ?>> m) {
109 throw new UnsupportedOperationException();
113 public void clear() {
114 throw new UnsupportedOperationException();
118 public Set<PathArgument> keySet() {
119 return Collections.unmodifiableSet(delegate.keySet());
123 public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
124 if (values == null) {
125 values = Collections.unmodifiableCollection(delegate.values());
131 public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
133 * Okay, this is not as efficient as it could be -- we could save ourselves the
134 * map instantiation. The cost of that would be re-implementation of a read-only
135 * Map.Entry to ensure our delegate is never modified.
137 * Let's skip that and use whatever the JRE gives us instead.
139 return Collections.unmodifiableMap(delegate).entrySet();
143 public boolean equals(final Object obj) {
144 return this == obj || delegate.equals(obj);
148 public int hashCode() {
149 return delegate.hashCode();
153 public String toString() {
154 return delegate.toString();
158 @SuppressWarnings("unchecked")
159 public Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> createMutableClone() {
160 if (delegate instanceof HashMap) {
161 return (Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>)
162 ((HashMap<?, ?>) delegate).clone();
165 return new HashMap<>(delegate);