Merge "Bug 2361: Added prototype implementation using new SPI."
[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 import java.util.Collection;
12 import java.util.Map;
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
16 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
17
18 public abstract class AbstractImmutableDataContainerNode<K extends PathArgument> extends AbstractImmutableNormalizedNode<K, Collection<DataContainerChild<? extends PathArgument, ?>>> implements Immutable, DataContainerNode<K> {
19     private final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> children;
20
21     public AbstractImmutableDataContainerNode(
22             final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> children, final K nodeIdentifier) {
23         super(nodeIdentifier);
24
25         this.children = UnmodifiableChildrenMap.create(children);
26     }
27
28     @Override
29     public final Optional<DataContainerChild<? extends PathArgument, ?>> getChild(final PathArgument child) {
30         return Optional.<DataContainerChild<? extends PathArgument, ?>> fromNullable(children.get(child));
31     }
32
33     @Override
34     public final Collection<DataContainerChild<? extends PathArgument, ?>> getValue() {
35         return children.values();
36     }
37
38     @Override
39     protected int valueHashCode() {
40         return children.hashCode();
41     }
42
43     /**
44      * DO NOT USE THIS METHOD.
45      *
46      * This is an implementation-internal API and no outside users should use it. If you do,
47      * you are asking for trouble, as the returned object is not guaranteed to conform to
48      * java.util.Map interface.
49      *
50      * @return An unmodifiable view if this node's children.
51      */
52     public final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> getChildren() {
53         return children;
54     }
55
56     @Override
57     protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
58         if (!(other instanceof AbstractImmutableDataContainerNode<?>)) {
59             return false;
60         }
61
62         return children.equals(((AbstractImmutableDataContainerNode<?>)other).children);
63     }
64 }