bd2a01698f5404eb1a8419767b09879789631d23
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / spi / AbstractContainerNode.java
1 /*
2  * Copyright (c) 2014 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.api.schema.tree.spi;
9
10 import java.util.Optional;
11 import org.eclipse.jdt.annotation.Nullable;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
15
16 /**
17  * A TreeNode capable of holding child nodes. The fact that any of the children
18  * changed is tracked by the subtree version.
19  */
20 abstract class AbstractContainerNode extends AbstractTreeNode {
21     protected AbstractContainerNode(final NormalizedNode<?, ?> data, final Version version) {
22         super(data, version);
23     }
24
25     @SuppressWarnings("unchecked")
26     protected final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> castData() {
27         return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) getData();
28     }
29
30     protected final @Nullable TreeNode getChildFromData(final PathArgument childId) {
31         // We do not cache the instantiated node as it is dirt cheap
32         return getChildFromData(castData(), childId, getVersion());
33     }
34
35     static TreeNode getChildFromData(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data,
36             final PathArgument childId, final Version version) {
37         final Optional<NormalizedNode<?, ?>> child = data.getChild(childId);
38         return child.isPresent() ? TreeNodeFactory.createTreeNode(child.get(), version) : null;
39     }
40 }