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;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
21 * Internal equivalent of {@link Collections}' unmodifiable Map. It does not retain
22 * keySet/entrySet references, thus lowering the memory overhead.
24 final class UnmodifiableChildrenMap implements Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
25 private static final long serialVersionUID = 1L;
27 * Do not wrap maps which are smaller than this and instead copy them into
30 private static final int WRAP_THRESHOLD = 9;
31 private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
32 private transient Collection<DataContainerChild<? extends PathArgument, ?>> values;
34 private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate) {
35 this.delegate = Preconditions.checkNotNull(delegate);
39 * Create an unmodifiable view of a particular map. Does not perform unnecessary
40 * encapsulation if the map is known to be already unmodifiable.
42 * @param map Backing map
43 * @return Unmodifiable view
45 static Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> create(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> map) {
46 if (map instanceof UnmodifiableChildrenMap) {
49 if (map instanceof ImmutableMap) {
53 return Collections.emptyMap();
55 if (map.size() < WRAP_THRESHOLD) {
56 return ImmutableMap.copyOf(map);
59 return new UnmodifiableChildrenMap(map);
64 return delegate.size();
68 public boolean isEmpty() {
69 return delegate.isEmpty();
73 public boolean containsKey(final Object key) {
74 return delegate.containsKey(key);
78 public boolean containsValue(final Object value) {
79 return delegate.containsValue(value);
83 public DataContainerChild<? extends PathArgument, ?> get(final Object key) {
84 return delegate.get(key);
88 public DataContainerChild<? extends PathArgument, ?> put(final PathArgument key,
89 final DataContainerChild<? extends PathArgument, ?> value) {
90 throw new UnsupportedOperationException();
94 public DataContainerChild<? extends PathArgument, ?> remove(final Object key) {
95 throw new UnsupportedOperationException();
99 public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild<? extends PathArgument, ?>> m) {
100 throw new UnsupportedOperationException();
104 public void clear() {
105 throw new UnsupportedOperationException();
109 public Set<PathArgument> keySet() {
110 return Collections.unmodifiableSet(delegate.keySet());
114 public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
115 if (values == null) {
116 values = Collections.unmodifiableCollection(delegate.values());
122 public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
124 * Okay, this is not as efficient as it could be -- we could save ourselves the
125 * map instantiation. The cost of that would be re-implementation of a read-only
126 * Map.Entry to ensure our delegate is never modified.
128 * Let's skip that and use whatever the JRE gives us instead.
130 return Collections.unmodifiableMap(delegate).entrySet();
134 public boolean equals(final Object o) {
135 return this == o || delegate.equals(o);
139 public int hashCode() {
140 return delegate.hashCode();
144 public String toString() {
145 return delegate.toString();