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 implements CloneableMap<PathArgument, DataContainerChild>, Serializable {
28 private static final long serialVersionUID = 1L;
31 * Do not wrap maps which are smaller than this and instead copy them into an ImmutableMap.
33 private static final int WRAP_THRESHOLD = 9;
35 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Delegate is expected to be Serializable")
36 private final Map<PathArgument, DataContainerChild> delegate;
38 private transient Collection<DataContainerChild> values = null;
40 private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild> delegate) {
41 this.delegate = requireNonNull(delegate);
45 * Create an unmodifiable view of a particular map. Does not perform unnecessary
46 * encapsulation if the map is known to be already unmodifiable.
48 * @param map Backing map
49 * @return Unmodifiable view
51 static Map<PathArgument, DataContainerChild> create(final Map<PathArgument, DataContainerChild> map) {
52 if (map instanceof UnmodifiableChildrenMap || map instanceof ImmutableMap) {
56 return ImmutableMap.of();
58 if (map.size() < WRAP_THRESHOLD) {
59 return ImmutableMap.copyOf(map);
61 return new UnmodifiableChildrenMap(map);
66 return delegate.size();
70 public boolean isEmpty() {
71 return delegate.isEmpty();
75 public boolean containsKey(final Object key) {
76 return delegate.containsKey(key);
80 public boolean containsValue(final Object value) {
81 return delegate.containsValue(value);
85 public DataContainerChild get(final Object key) {
86 return delegate.get(key);
90 public DataContainerChild put(final PathArgument key, final DataContainerChild value) {
91 throw new UnsupportedOperationException();
95 public DataContainerChild remove(final Object key) {
96 throw new UnsupportedOperationException();
100 @SuppressWarnings("checkstyle:parameterName")
101 public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild> m) {
102 throw new UnsupportedOperationException();
106 public void clear() {
107 throw new UnsupportedOperationException();
111 public Set<PathArgument> keySet() {
112 return Collections.unmodifiableSet(delegate.keySet());
116 public Collection<DataContainerChild> values() {
117 if (values == null) {
118 values = Collections.unmodifiableCollection(delegate.values());
124 public Set<Entry<PathArgument, DataContainerChild>> entrySet() {
126 * Okay, this is not as efficient as it could be -- we could save ourselves the
127 * map instantiation. The cost of that would be re-implementation of a read-only
128 * Map.Entry to ensure our delegate is never modified.
130 * Let's skip that and use whatever the JRE gives us instead.
132 return Collections.unmodifiableMap(delegate).entrySet();
136 public boolean equals(final Object obj) {
137 return this == obj || delegate.equals(obj);
141 public int hashCode() {
142 return delegate.hashCode();
146 public String toString() {
147 return delegate.toString();
151 @SuppressWarnings("unchecked")
152 public Map<PathArgument, DataContainerChild> createMutableClone() {
153 if (delegate instanceof HashMap<?, ?> hashMap) {
154 return (Map<PathArgument, DataContainerChild>) hashMap.clone();
156 return new HashMap<>(delegate);