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) {
55 if (map instanceof ImmutableMap) {
59 return ImmutableMap.of();
61 if (map.size() < WRAP_THRESHOLD) {
62 return ImmutableMap.copyOf(map);
65 return new UnmodifiableChildrenMap(map);
70 return delegate.size();
74 public boolean isEmpty() {
75 return delegate.isEmpty();
79 public boolean containsKey(final Object key) {
80 return delegate.containsKey(key);
84 public boolean containsValue(final Object value) {
85 return delegate.containsValue(value);
89 public DataContainerChild get(final Object key) {
90 return delegate.get(key);
94 public DataContainerChild put(final PathArgument key, final DataContainerChild value) {
95 throw new UnsupportedOperationException();
99 public DataContainerChild remove(final Object key) {
100 throw new UnsupportedOperationException();
104 @SuppressWarnings("checkstyle:parameterName")
105 public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild> m) {
106 throw new UnsupportedOperationException();
110 public void clear() {
111 throw new UnsupportedOperationException();
115 public Set<PathArgument> keySet() {
116 return Collections.unmodifiableSet(delegate.keySet());
120 public Collection<DataContainerChild> values() {
121 if (values == null) {
122 values = Collections.unmodifiableCollection(delegate.values());
128 public Set<Entry<PathArgument, DataContainerChild>> entrySet() {
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.
134 * Let's skip that and use whatever the JRE gives us instead.
136 return Collections.unmodifiableMap(delegate).entrySet();
140 public boolean equals(final Object obj) {
141 return this == obj || delegate.equals(obj);
145 public int hashCode() {
146 return delegate.hashCode();
150 public String toString() {
151 return delegate.toString();
155 @SuppressWarnings("unchecked")
156 public Map<PathArgument, DataContainerChild> createMutableClone() {
157 if (delegate instanceof HashMap) {
158 return (Map<PathArgument, DataContainerChild>) ((HashMap<?, ?>) delegate).clone();
160 return new HashMap<>(delegate);