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;
26 private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate;
27 private transient Collection<DataContainerChild<? extends PathArgument, ?>> values;
29 private UnmodifiableChildrenMap(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> delegate) {
30 this.delegate = Preconditions.checkNotNull(delegate);
34 * Create an unmodifiable view of a particular map. Does not perform unnecessary
35 * encapsulation if the map is known to be already unmodifiable.
37 * @param map Backing map
38 * @return Unmodifiable view
40 static Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> create(final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> map) {
41 if (map instanceof UnmodifiableChildrenMap) {
44 if (map instanceof ImmutableMap) {
48 return Collections.emptyMap();
51 return new UnmodifiableChildrenMap(map);
56 return delegate.size();
60 public boolean isEmpty() {
61 return delegate.isEmpty();
65 public boolean containsKey(final Object key) {
66 return delegate.containsKey(key);
70 public boolean containsValue(final Object value) {
71 return delegate.containsValue(value);
75 public DataContainerChild<? extends PathArgument, ?> get(final Object key) {
76 return delegate.get(key);
80 public DataContainerChild<? extends PathArgument, ?> put(final PathArgument key,
81 final DataContainerChild<? extends PathArgument, ?> value) {
82 throw new UnsupportedOperationException();
86 public DataContainerChild<? extends PathArgument, ?> remove(final Object key) {
87 throw new UnsupportedOperationException();
91 public void putAll(final Map<? extends PathArgument, ? extends DataContainerChild<? extends PathArgument, ?>> m) {
92 throw new UnsupportedOperationException();
97 throw new UnsupportedOperationException();
101 public Set<PathArgument> keySet() {
102 return Collections.unmodifiableSet(delegate.keySet());
106 public Collection<DataContainerChild<? extends PathArgument, ?>> values() {
107 if (values == null) {
108 values = Collections.unmodifiableCollection(delegate.values());
114 public Set<Entry<PathArgument, DataContainerChild<? extends PathArgument, ?>>> entrySet() {
116 * Okay, this is not as efficient as it could be -- we could save ourselves the
117 * map instantiation. The cost of that would be re-implementation of a read-only
118 * Map.Entry to ensure our delegate is never modified.
120 * Let's skip that and use whatever the JRE gives us instead.
122 return Collections.unmodifiableMap(delegate).entrySet();
126 public boolean equals(final Object o) {
127 return this == o || delegate.equals(o);
131 public int hashCode() {
132 return delegate.hashCode();
136 public String toString() {
137 return delegate.toString();