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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
22 * Internal equivalent of {@link Collections}' unmodifiable Map. It does not retain
23 * keySet/entrySet references, thus lowering the memory overhead.
25 final class UnmodifiableChildrenMap implements CloneableMap<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
26 private static final long serialVersionUID = 1L;
28 * Do not wrap maps which are smaller than this and instead copy them into
31 private static final int WRAP_THRESHOLD = 9;
32 private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
33 private transient Collection<DataContainerChild<? extends PathArgument, ?>> values;
35 private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate) {
36 this.delegate = Preconditions.checkNotNull(delegate);
40 * Create an unmodifiable view of a particular map. Does not perform unnecessary
41 * encapsulation if the map is known to be already unmodifiable.
43 * @param map Backing map
44 * @return Unmodifiable view
46 static Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> create(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> map) {
47 if (map instanceof UnmodifiableChildrenMap) {
50 if (map instanceof ImmutableMap) {
54 return ImmutableMap.of();
56 if (map.size() < WRAP_THRESHOLD) {
57 return ImmutableMap.copyOf(map);
60 return new UnmodifiableChildrenMap(map);
65 return delegate.size();
69 public boolean isEmpty() {
70 return delegate.isEmpty();
74 public boolean containsKey(final Object key) {
75 return delegate.containsKey(key);
79 public boolean containsValue(final Object value) {
80 return delegate.containsValue(value);
84 public DataContainerChild<? extends PathArgument, ?> get(final Object key) {
85 return delegate.get(key);
89 public DataContainerChild<? extends PathArgument, ?> put(final PathArgument key,
90 final DataContainerChild<? extends PathArgument, ?> value) {
91 throw new UnsupportedOperationException();
95 public DataContainerChild<? extends PathArgument, ?> remove(final Object key) {
96 throw new UnsupportedOperationException();
100 public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild<? extends PathArgument, ?>> m) {
101 throw new UnsupportedOperationException();
105 public void clear() {
106 throw new UnsupportedOperationException();
110 public Set<PathArgument> keySet() {
111 return Collections.unmodifiableSet(delegate.keySet());
115 public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
116 if (values == null) {
117 values = Collections.unmodifiableCollection(delegate.values());
123 public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
125 * Okay, this is not as efficient as it could be -- we could save ourselves the
126 * map instantiation. The cost of that would be re-implementation of a read-only
127 * Map.Entry to ensure our delegate is never modified.
129 * Let's skip that and use whatever the JRE gives us instead.
131 return Collections.unmodifiableMap(delegate).entrySet();
135 public boolean equals(final Object o) {
136 return this == o || delegate.equals(o);
140 public int hashCode() {
141 return delegate.hashCode();
145 public String toString() {
146 return delegate.toString();
150 @SuppressWarnings("unchecked")
151 public Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> createMutableClone() {
152 if (delegate instanceof HashMap) {
153 return (Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>) ((HashMap<?, ?>) delegate).clone();
156 return new HashMap<>(delegate);