2 * Copyright (c) 2013 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.Optional;
12 import java.util.Collections;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
18 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public abstract class AbstractImmutableDataContainerNode<K extends PathArgument> extends AbstractImmutableNormalizedNode<K, Iterable<DataContainerChild<? extends PathArgument, ?>>> implements Immutable, DataContainerNode<K> {
23 private static final Logger LOG = LoggerFactory.getLogger(AbstractImmutableDataContainerNode.class);
24 private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> children;
26 public AbstractImmutableDataContainerNode(
27 final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> children, final K nodeIdentifier) {
28 super(nodeIdentifier);
31 * There is a code path where AbstractImmutableDataContainerNodeBuilder can reflect
32 * the collection acquired via getChildren() back to us. This is typically the case
33 * in the datastore where transactions cancel each other out, leaving an unmodified
34 * node. In that case we want to skip wrapping the map again (and again and again).
36 * In a perfect world, Collection.unmodifiableMap() would be doing the instanceof
37 * check which would stop the proliferation. Unfortunately this not the case and the
38 * 'unmodifiable' trait is not exposed by anything we can query. Furthermore the API
39 * contract there is sufficiently vague so an implementation may actually return a
40 * different implementation based on input map -- for example
41 * Collections.unmodifiableMap(Collections.emptyMap()) returning the same thing as
42 * Collections.emptyMap().
44 * This means that we have to perform the instantiation here (as opposed to once at
45 * class load time) and then compare the classes.
47 final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> pub = Collections.unmodifiableMap(children);
48 if (children.getClass().equals(pub.getClass())) {
49 LOG.trace("Reusing already-unmodifiable children {}", children);
50 this.children = children;
57 public final Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
58 return Optional.<DataContainerChild<? extends PathArgument, ?>> fromNullable(children.get(child));
62 public final Iterable<DataContainerChild<? extends PathArgument, ?>> getValue() {
63 return children.values();
67 protected int valueHashCode() {
68 return children.hashCode();
71 public final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> getChildren() {
76 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
77 if (!(other instanceof AbstractImmutableDataContainerNode<?>)) {
81 return children.equals(((AbstractImmutableDataContainerNode<?>)other).children);