3394effafb72d345ed86f517acc5373452e6f9be
[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 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
17 public abstract class AbstractImmutableDataContainerNode<K extends PathArgument, N extends DataContainerNode<K>>
18         extends AbstractImmutableNormalizedNode<K, N> implements DataContainerNode<K> {
19     private final Map<PathArgument, Object> children;
20
21     protected AbstractImmutableDataContainerNode(final Map<PathArgument, Object> children, final K nodeIdentifier) {
22         super(nodeIdentifier);
23         this.children = ImmutableOffsetMap.unorderedCopyOf(children);
24     }
25
26     @Override
27     public final DataContainerChild childByArg(final PathArgument child) {
28         return LazyLeafOperations.getChild(children, child);
29     }
30
31     @Override
32     public final Collection<DataContainerChild> body() {
33         return new LazyValues(children);
34     }
35
36     @Override
37     public final int size() {
38         return children.size();
39     }
40
41     /**
42      * DO NOT USE THIS METHOD.
43      *
44      * <p>
45      * This is an implementation-internal API and no outside users should use it. If you do, you are asking for trouble,
46      * as the returned object is not guaranteed to conform to java.util.Map interface, nor is its contents well-defined.
47      *
48      * @return An unmodifiable view if this node's children.
49      */
50     public final Map<PathArgument, Object> getChildren() {
51         return children;
52     }
53
54     @Override
55     protected int valueHashCode() {
56         return children.hashCode();
57     }
58
59     @Override
60     protected boolean valueEquals(final N other) {
61         return other instanceof AbstractImmutableDataContainerNode<?, ?> && children.equals(
62                 ((AbstractImmutableDataContainerNode<?, ?>) other).children);
63     }
64 }