8f1baffcf686e5a9b9cadb1bd211f8222caabab4
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / 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.impl.schema.tree;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Collections2;
12 import java.util.Collection;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
17
18 abstract class AbstractRecursiveCandidateNode extends AbstractDataTreeCandidateNode {
19
20     protected AbstractRecursiveCandidateNode(final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
21         super(data);
22     }
23
24     @SuppressWarnings("unchecked")
25     static DataTreeCandidateNode deleteNode(final NormalizedNode<?, ?> data) {
26         if (data instanceof NormalizedNodeContainer) {
27             return new RecursiveDeleteCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
28         }
29         return new DeleteLeafCandidateNode(data);
30     }
31
32     @SuppressWarnings("unchecked")
33     static DataTreeCandidateNode replaceNode(final NormalizedNode<?, ?> oldData, final NormalizedNode<?, ?> newData) {
34         if (isContainer(oldData)) {
35             return new RecursiveReplaceCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) oldData,
36                 (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) newData);
37         }
38         return new ReplaceLeafCandidateNode(oldData, newData);
39     }
40
41     @SuppressWarnings("unchecked")
42     static DataTreeCandidateNode unmodifiedNode(final NormalizedNode<?, ?> data) {
43         if (data instanceof NormalizedNodeContainer) {
44             return new RecursiveUnmodifiedCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
45         }
46         return new UnmodifiedLeafCandidateNode(data);
47     }
48
49     @SuppressWarnings("unchecked")
50     static DataTreeCandidateNode writeNode(final NormalizedNode<?, ?> data) {
51         if (data instanceof NormalizedNodeContainer) {
52             return new RecursiveWriteCandidateNode((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) data);
53         }
54         return new WriteLeafCandidateNode(data);
55     }
56
57     protected static boolean isContainer(final NormalizedNode<?, ?> data) {
58         return data instanceof NormalizedNodeContainer;
59     }
60
61     @Override
62     public final DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
63         final Optional<NormalizedNode<?, ?>> potential = getData().getChild(identifier);
64         if (potential.isPresent()) {
65             return createChild(potential.get());
66         }
67         return null;
68     }
69
70     @Override
71     public final Collection<DataTreeCandidateNode> getChildNodes() {
72         return Collections2.transform(getData().getValue(), this::createChild);
73     }
74
75     @SuppressWarnings("unchecked")
76     private DataTreeCandidateNode createChild(final NormalizedNode<?, ?> childData) {
77         if (isContainer(childData)) {
78             return createContainer((NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) childData);
79         }
80         return createLeaf(childData);
81     }
82
83     protected abstract DataTreeCandidateNode createContainer(NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> childData);
84
85     protected abstract DataTreeCandidateNode createLeaf(NormalizedNode<?,?> childData);
86 }