Fix immutable NormalizedNode equality
[yangtools.git] / data / 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 java.util.Collection;
11 import java.util.Map;
12 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
16 import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
17
18 public abstract class AbstractImmutableDataContainerNode<K extends PathArgument, N extends DataContainerNode>
19         extends AbstractNormalizedNode<K, N> implements DataContainerNode {
20     private final Map<PathArgument, Object> children;
21
22     protected AbstractImmutableDataContainerNode(final Map<PathArgument, Object> children, final K nodeIdentifier) {
23         super(nodeIdentifier);
24         this.children = ImmutableOffsetMap.unorderedCopyOf(children);
25     }
26
27     @Override
28     public final DataContainerChild childByArg(final PathArgument child) {
29         return LazyLeafOperations.getChild(children, child);
30     }
31
32     @Override
33     public final Collection<DataContainerChild> body() {
34         return new LazyValues(children);
35     }
36
37     @Override
38     public final int size() {
39         return children.size();
40     }
41
42     /**
43      * DO NOT USE THIS METHOD.
44      *
45      * <p>
46      * This is an implementation-internal API and no outside users should use it. If you do, you are asking for trouble,
47      * as the returned object is not guaranteed to conform to java.util.Map interface, nor is its contents well-defined.
48      *
49      * @return An unmodifiable view if this node's children.
50      */
51     public final Map<PathArgument, Object> getChildren() {
52         return children;
53     }
54
55     @Override
56     protected int valueHashCode() {
57         return children.hashCode();
58     }
59
60     @Override
61     protected boolean valueEquals(final N other) {
62         if (other instanceof AbstractImmutableDataContainerNode) {
63             return children.equals(((AbstractImmutableDataContainerNode<?, ?>) other).children);
64         }
65         if (size() != other.size()) {
66             return false;
67         }
68         for (var child : body()) {
69             if (!child.equals(other.childByArg(child.getIdentifier()))) {
70                 return false;
71             }
72         }
73         return true;
74     }
75 }