BUG-869: kill more sonar warnings
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTreeCandidate.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.impl.schema.tree;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Verify;
14 import com.google.common.collect.Collections2;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
22
23 final class InMemoryDataTreeCandidate extends AbstractDataTreeCandidate {
24     private abstract static class AbstractNode implements DataTreeCandidateNode {
25         private final ModifiedNode mod;
26         private final TreeNode newMeta;
27         private final TreeNode oldMeta;
28
29         protected AbstractNode(final ModifiedNode mod,
30                 final TreeNode oldMeta, final TreeNode newMeta) {
31             this.newMeta = newMeta;
32             this.oldMeta = oldMeta;
33             this.mod = Preconditions.checkNotNull(mod);
34         }
35
36         protected final ModifiedNode getMod() {
37             return mod;
38         }
39
40         protected final TreeNode getNewMeta() {
41             return newMeta;
42         }
43
44         protected final TreeNode getOldMeta() {
45             return oldMeta;
46         }
47
48         private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
49             if (parent != null) {
50                 return parent.getChild(id).orNull();
51             } else {
52                 return null;
53             }
54         }
55
56         private DataTreeCandidateNode childNode(final ModifiedNode input) {
57             final PathArgument id = input.getIdentifier();
58             return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
59         }
60
61         @Override
62         public Collection<DataTreeCandidateNode> getChildNodes() {
63             return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
64                 @Override
65                 public DataTreeCandidateNode apply(final ModifiedNode input) {
66                     return childNode(input);
67                 }
68             });
69         }
70
71         @Override
72         public ModificationType getModificationType() {
73             return Verify.verifyNotNull(mod.getModificationType(), "Node %s does not have resolved modification type", mod);
74         }
75
76         private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
77             if (meta != null) {
78                 return Optional.<NormalizedNode<?,?>>of(meta.getData());
79             } else {
80                 return Optional.absent();
81             }
82         }
83
84         @Override
85         public Optional<NormalizedNode<?, ?>> getDataAfter() {
86             return optionalData(newMeta);
87         }
88
89         @Override
90         public Optional<NormalizedNode<?, ?>> getDataBefore() {
91             return optionalData(oldMeta);
92         }
93
94         @Override
95         public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
96             final Optional<ModifiedNode> childMod = mod.getChild(identifier);
97             if (childMod.isPresent()) {
98                 return childNode(childMod.get());
99             }
100             return null;
101         }
102     }
103
104     private static final class ChildNode extends AbstractNode {
105         public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
106             super(mod, oldMeta, newMeta);
107         }
108
109         @Override
110         public PathArgument getIdentifier() {
111             return getMod().getIdentifier();
112         }
113     }
114
115     private static final class RootNode extends AbstractNode {
116         public RootNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
117             super(mod, oldMeta, newMeta);
118         }
119
120         @Override
121         public PathArgument getIdentifier() {
122             throw new IllegalStateException("Attempted to get identifier of the root node");
123         }
124     }
125
126     private final RootNode root;
127
128     InMemoryDataTreeCandidate(final YangInstanceIdentifier rootPath, final ModifiedNode modificationRoot,
129             final TreeNode beforeRoot, final TreeNode afterRoot) {
130         super(rootPath);
131         this.root = new RootNode(modificationRoot, beforeRoot, afterRoot);
132     }
133
134     TreeNode getAfterRoot() {
135         return root.getNewMeta();
136     }
137
138     TreeNode getBeforeRoot() {
139         return root.getOldMeta();
140     }
141
142     @Override
143     public DataTreeCandidateNode getRootNode() {
144         return root;
145     }
146 }