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