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