Populate data/ hierarchy
[yangtools.git] / data / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / AbstractRecursiveCandidateNode.java
1 /*
2  * Copyright (c) 2015 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;
9
10 import com.google.common.collect.Collections2;
11 import java.util.Collection;
12 import java.util.Optional;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16
17 abstract class AbstractRecursiveCandidateNode extends AbstractDataTreeCandidateNode {
18     AbstractRecursiveCandidateNode(final DistinctNodeContainer<PathArgument, NormalizedNode> data) {
19         super(data);
20     }
21
22     @Override
23     public final Optional<DataTreeCandidateNode> getModifiedChild(final PathArgument identifier) {
24         return data().findChildByArg(identifier).map(this::createChild);
25     }
26
27     @Override
28     public final Collection<DataTreeCandidateNode> getChildNodes() {
29         return Collections2.transform(data().body(), this::createChild);
30     }
31
32     abstract DataTreeCandidateNode createContainer(DistinctNodeContainer<PathArgument, NormalizedNode> childData);
33
34     abstract DataTreeCandidateNode createLeaf(NormalizedNode childData);
35
36     @SuppressWarnings("unchecked")
37     private DataTreeCandidateNode createChild(final NormalizedNode childData) {
38         if (childData instanceof DistinctNodeContainer) {
39             return createContainer((DistinctNodeContainer<PathArgument, NormalizedNode>) childData);
40         }
41         return createLeaf(childData);
42     }
43 }