Merge "BUG 1440 - incorrect inserting of augment node"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableDataContainerNode.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
9
10 import com.google.common.base.Optional;
11
12 import java.util.Collections;
13 import java.util.Map;
14
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;
21
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;
25
26     public AbstractImmutableDataContainerNode(
27             final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> children, final K nodeIdentifier) {
28         super(nodeIdentifier);
29
30         /*
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).
35          *
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().
43          *
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.
46          */
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;
51         } else {
52             this.children = pub;
53         }
54     }
55
56     @Override
57     public final Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
58         return Optional.<DataContainerChild<? extends PathArgument, ?>> fromNullable(children.get(child));
59     }
60
61     @Override
62     public final Iterable<DataContainerChild<? extends PathArgument, ?>> getValue() {
63         return children.values();
64     }
65
66     @Override
67     protected int valueHashCode() {
68         return children.hashCode();
69     }
70
71     public final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> getChildren() {
72         return children;
73     }
74
75     @Override
76     protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
77         if (!(other instanceof AbstractImmutableDataContainerNode<?>)) {
78             return false;
79         }
80
81         return children.equals(((AbstractImmutableDataContainerNode<?>)other).children);
82     }
83 }