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 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;
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;
23 * Internal equivalent of {@link Collections}' unmodifiable Map. It does not retain
24 * keySet/entrySet references, thus lowering the memory overhead.
26 final class UnmodifiableChildrenMap
27 implements CloneableMap<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
28 private static final long serialVersionUID = 1L;
30 * Do not wrap maps which are smaller than this and instead copy them into
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;
37 private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate) {
38 this.delegate = Preconditions.checkNotNull(delegate);
42 * Create an unmodifiable view of a particular map. Does not perform unnecessary
43 * encapsulation if the map is known to be already unmodifiable.
45 * @param map Backing map
46 * @return Unmodifiable view
48 static Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> create(
49 final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> map) {
50 if (map instanceof UnmodifiableChildrenMap) {
53 if (map instanceof ImmutableMap) {
57 return ImmutableMap.of();
59 if (map.size() < WRAP_THRESHOLD) {
60 return ImmutableMap.copyOf(map);
63 return new UnmodifiableChildrenMap(map);
68 return delegate.size();
72 public boolean isEmpty() {
73 return delegate.isEmpty();
77 public boolean containsKey(final Object key) {
78 return delegate.containsKey(key);
82 public boolean containsValue(final Object value) {
83 return delegate.containsValue(value);
87 public DataContainerChild<? extends PathArgument, ?> get(final Object key) {
88 return delegate.get(key);
92 public DataContainerChild<? extends PathArgument, ?> put(final PathArgument key,
93 final DataContainerChild<? extends PathArgument, ?> value) {
94 throw new UnsupportedOperationException();
98 public DataContainerChild<? extends PathArgument, ?> remove(final Object key) {
99 throw new UnsupportedOperationException();
103 @SuppressWarnings("checkstyle:parameterName")
104 public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild<? extends PathArgument, ?>> m) {
105 throw new UnsupportedOperationException();
109 public void clear() {
110 throw new UnsupportedOperationException();
114 public Set<PathArgument> keySet() {
115 return Collections.unmodifiableSet(delegate.keySet());
120 public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
121 if (values == null) {
122 values = Collections.unmodifiableCollection(delegate.values());
128 public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> 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<? extends PathArgument, ?>> createMutableClone() {
157 if (delegate instanceof HashMap) {
158 return (Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>)
159 ((HashMap<?, ?>) delegate).clone();
162 return new HashMap<>(delegate);