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;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
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 implements Map<PathArgument, DataContainerChild<? extends PathArgument, ?>>, Serializable {
27 private static final Logger LOG = LoggerFactory.getLogger(UnmodifiableChildrenMap.class);
28 private static final long serialVersionUID = 1L;
29 private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
30 private transient Collection<DataContainerChild<? extends PathArgument, ?>> values;
32 private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate) {
33 this.delegate = Preconditions.checkNotNull(delegate);
37 * Create an unmodifiable view of a particular map. Does not perform unnecessary
38 * encapsulation if the map is known to be already unmodifiable.
40 * @param map Backing map
41 * @return Unmodifiable view
43 static Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> create(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> map) {
44 if (map instanceof UnmodifiableChildrenMap) {
47 if (map instanceof ImmutableMap) {
51 return Collections.emptyMap();
54 return new UnmodifiableChildrenMap(map);
59 return delegate.size();
63 public boolean isEmpty() {
64 return delegate.isEmpty();
68 public boolean containsKey(final Object key) {
69 return delegate.containsKey(key);
73 public boolean containsValue(final Object value) {
74 return delegate.containsValue(value);
78 public DataContainerChild<? extends PathArgument, ?> get(final Object key) {
79 return delegate.get(key);
83 public DataContainerChild<? extends PathArgument, ?> put(final PathArgument key,
84 final DataContainerChild<? extends PathArgument, ?> value) {
85 throw new UnsupportedOperationException();
89 public DataContainerChild<? extends PathArgument, ?> remove(final Object key) {
90 throw new UnsupportedOperationException();
94 public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild<? extends PathArgument, ?>> m) {
95 throw new UnsupportedOperationException();
100 throw new UnsupportedOperationException();
104 public Set<PathArgument> keySet() {
105 return Collections.unmodifiableSet(delegate.keySet());
109 public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
110 if (values == null) {
111 values = Collections.unmodifiableCollection(delegate.values());
117 public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
118 LOG.warn("Invocation of inefficient entrySet()", new Throwable().fillInStackTrace());
119 return Collections.unmodifiableMap(delegate).entrySet();
124 public boolean equals(final Object o) {
125 return this == o || delegate.equals(o);
129 public int hashCode() {
130 return delegate.hashCode();
134 public String toString() {
135 return delegate.toString();