*/
package org.opendaylight.yangtools.yang.data.impl.leafref;
+import static java.util.Objects.requireNonNull;
+
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-class LeafRefContextBuilder implements Builder<LeafRefContext> {
+final class LeafRefContextBuilder implements Builder<LeafRefContext> {
private final Map<QName, LeafRefContext> referencingChildren = new HashMap<>();
private final Map<QName, LeafRefContext> referencedByChildren = new HashMap<>();
private final Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<>();
- private QName currentNodeQName;
- private SchemaPath currentNodePath;
- private SchemaContext schemaContext;
+ private final QName currentNodeQName;
+ private final SchemaPath currentNodePath;
+ private final SchemaContext schemaContext;
private LeafRefPath leafRefTargetPath = null;
private LeafRefPath absoluteLeafRefTargetPath = null;
LeafRefContextBuilder(final QName currentNodeQName, final SchemaPath currentNodePath,
final SchemaContext schemaContext) {
- this.currentNodeQName = currentNodeQName;
- this.currentNodePath = currentNodePath;
+ this.currentNodeQName = requireNonNull(currentNodeQName);
+ this.currentNodePath = requireNonNull(currentNodePath);
+ // FIXME: requireNonNull
this.schemaContext = schemaContext;
}
return leafRefContext;
}
- public boolean hasLeafRefContextChild() {
- return hasReferencedByChild() || hasReferencingChild();
- }
-
- public boolean hasReferencedByChild() {
- return !referencedByChildren.isEmpty();
- }
-
- public boolean hasReferencingChild() {
- return !referencingChildren.isEmpty();
- }
-
- public boolean isReferencedBy() {
+ boolean isReferencedBy() {
return isReferencedBy;
}
- public void setReferencedBy(final boolean referencedBy) {
+ void setReferencedBy(final boolean referencedBy) {
this.isReferencedBy = referencedBy;
}
- public boolean isReferencing() {
+ boolean isReferencing() {
return isReferencing;
}
- public void setReferencing(final boolean referencing) {
+ void setReferencing(final boolean referencing) {
this.isReferencing = referencing;
}
- public void addReferencingChild(final LeafRefContext child, final QName childQName) {
+ void addReferencingChild(final LeafRefContext child, final QName childQName) {
referencingChildren.put(childQName, child);
}
- public LeafRefContext getReferencingChildByName(final QName name) {
- return referencingChildren.get(name);
- }
-
- public Map<QName, LeafRefContext> getReferencingChilds() {
+ Map<QName, LeafRefContext> getReferencingChilds() {
return referencingChildren;
}
- public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
+ void addReferencedByChild(final LeafRefContext child, final QName childQName) {
referencedByChildren.put(childQName, child);
}
- public LeafRefContext getReferencedByChildByName(final QName name) {
- return referencedByChildren.get(name);
- }
-
- public Map<QName, LeafRefContext> getReferencedByChilds() {
+ Map<QName, LeafRefContext> getReferencedByChilds() {
return referencedByChildren;
}
- public SchemaPath getCurrentNodePath() {
+ SchemaPath getCurrentNodePath() {
return currentNodePath;
}
- public void setCurrentNodePath(final SchemaPath currentNodePath) {
- this.currentNodePath = currentNodePath;
- }
-
- public LeafRefPath getLeafRefTargetPath() {
+ LeafRefPath getLeafRefTargetPath() {
return leafRefTargetPath;
}
- public void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
+ void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
this.leafRefTargetPath = leafRefPath;
}
- public String getLeafRefTargetPathString() {
+ String getLeafRefTargetPathString() {
return leafRefTargetPathString;
}
- public void setLeafRefTargetPathString(final String leafRefPathString) {
+ void setLeafRefTargetPathString(final String leafRefPathString) {
this.leafRefTargetPathString = leafRefPathString;
}
- public QName getCurrentNodeQName() {
+ QName getCurrentNodeQName() {
return currentNodeQName;
}
- public void setCurrentNodeQName(final QName currentNodeQName) {
- this.currentNodeQName = currentNodeQName;
- }
-
- public SchemaContext getSchemaContext() {
+ SchemaContext getSchemaContext() {
return schemaContext;
}
- public void setSchemaContext(final SchemaContext schemaContext) {
- this.schemaContext = schemaContext;
- }
-
- public LeafRefPath getAbsoluteLeafRefTargetPath() {
+ LeafRefPath getAbsoluteLeafRefTargetPath() {
if (isReferencing && absoluteLeafRefTargetPath == null) {
if (leafRefTargetPath.isAbsolute()) {
absoluteLeafRefTargetPath = leafRefTargetPath;
return absoluteLeafRefTargetPath;
}
- public Module getLeafRefContextModule() {
+ Module getLeafRefContextModule() {
final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
return schemaContext.findModule(qnameModule).orElse(null);
}
- public void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
+ void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
referencedByLeafRefCtx.put(qname, leafRef);
}
- public LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
+ LeafRefContext getReferencedByLeafRefCtxByName(final QName qname) {
return referencedByLeafRefCtx.get(qname);
}
- public Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
+ Map<QName, LeafRefContext> getAllReferencedByLeafRefCtxs() {
return referencedByLeafRefCtx;
}
}
import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
-class LeafRefContextTreeBuilder {
+final class LeafRefContextTreeBuilder {
private final List<LeafRefContext> leafRefs = new LinkedList<>();
private final SchemaContext schemaContext;
this.schemaContext = schemaContext;
}
- public LeafRefContext buildLeafRefContextTree() throws IOException,
- LeafRefYangSyntaxErrorException {
+ LeafRefContext buildLeafRefContextTree() throws IOException, LeafRefYangSyntaxErrorException {
final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(schemaContext.getQName(),
schemaContext.getPath(), schemaContext);
final Set<Module> modules = schemaContext.getModules();
for (final Module module : modules) {
- final Collection<DataSchemaNode> childNodes = module.getChildNodes();
- for (final DataSchemaNode childNode : childNodes) {
- final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
- childNode, module);
-
- if (childLeafRefContext.hasReferencingChild()
- || childLeafRefContext.isReferencing()) {
- rootBuilder.addReferencingChild(childLeafRefContext,
- childLeafRefContext.getNodeName());
+ for (final DataSchemaNode childNode : module.getChildNodes()) {
+ final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(childNode, module);
+ if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
+ rootBuilder.addReferencingChild(childLeafRefContext, childLeafRefContext.getNodeName());
}
}
}
for (final Module module : modules) {
final Collection<DataSchemaNode> childNodes = module.getChildNodes();
for (final DataSchemaNode childNode : childNodes) {
- final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
- childNode, module);
+ final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(childNode, module);
- if (childLeafRefContext.hasReferencedChild()
- || childLeafRefContext.isReferenced()) {
- rootBuilder.addReferencedByChild(childLeafRefContext,
- childLeafRefContext.getNodeName());
+ if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
+ rootBuilder.addReferencedByChild(childLeafRefContext, childLeafRefContext.getNodeName());
}
}
}
return rootBuilder.build();
}
- private LeafRefContext buildLeafRefContextReferencingTree(
- final DataSchemaNode node, final Module currentModule) throws IOException,
- LeafRefYangSyntaxErrorException {
-
- final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(
- node.getQName(), node.getPath(), schemaContext);
+ private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node, final Module currentModule)
+ throws IOException, LeafRefYangSyntaxErrorException {
+ final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
+ node.getPath(), schemaContext);
if (node instanceof DataNodeContainer) {
- final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
- final Collection<DataSchemaNode> childNodes = dataNodeContainer
- .getChildNodes();
-
- for (final DataSchemaNode childNode : childNodes) {
- final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
- childNode, currentModule);
-
- if (childLeafRefContext.hasReferencingChild()
- || childLeafRefContext.isReferencing()) {
- currentLeafRefContextBuilder.addReferencingChild(
- childLeafRefContext,
- childLeafRefContext.getNodeName());
+ for (final DataSchemaNode childNode : ((DataNodeContainer) node).getChildNodes()) {
+ final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(childNode, currentModule);
+ if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
+ currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext,
+ childLeafRefContext.getNodeName());
}
}
} else if (node instanceof ChoiceSchemaNode) {
-
- final ChoiceSchemaNode choice = (ChoiceSchemaNode) node;
// :FIXME choice without case
- for (final CaseSchemaNode caseNode : choice.getCases().values()) {
- final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(
- caseNode, currentModule);
-
- if (childLeafRefContext.hasReferencingChild()
- || childLeafRefContext.isReferencing()) {
- currentLeafRefContextBuilder.addReferencingChild(
- childLeafRefContext,
- childLeafRefContext.getNodeName());
+ for (final CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
+ final LeafRefContext childLeafRefContext = buildLeafRefContextReferencingTree(caseNode, currentModule);
+ if (childLeafRefContext.hasReferencingChild() || childLeafRefContext.isReferencing()) {
+ currentLeafRefContextBuilder.addReferencingChild(childLeafRefContext,
+ childLeafRefContext.getNodeName());
}
}
return schemaContext.findModule(baseLeafRefType.getQName().getModule()).orElse(null);
}
- private LeafRefContext buildLeafRefContextReferencedByTree(
- final DataSchemaNode node, final Module currentModule) throws IOException,
- LeafRefYangSyntaxErrorException {
-
- final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(
- node.getQName(), node.getPath(), schemaContext);
-
+ private LeafRefContext buildLeafRefContextReferencedByTree(final DataSchemaNode node, final Module currentModule)
+ throws IOException,LeafRefYangSyntaxErrorException {
+ final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(),
+ node.getPath(), schemaContext);
if (node instanceof DataNodeContainer) {
- final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
- final Collection<DataSchemaNode> childNodes = dataNodeContainer
- .getChildNodes();
-
- for (final DataSchemaNode childNode : childNodes) {
- final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(
- childNode, currentModule);
-
- if (childLeafRefContext.hasReferencedChild()
- || childLeafRefContext.isReferenced()) {
- currentLeafRefContextBuilder.addReferencedByChild(
- childLeafRefContext,
- childLeafRefContext.getNodeName());
+ for (final DataSchemaNode childNode : ((DataNodeContainer) node).getChildNodes()) {
+ final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(childNode,
+ currentModule);
+ if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
+ currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
+ childLeafRefContext.getNodeName());
}
}
} else if (node instanceof ChoiceSchemaNode) {
for (final CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
final LeafRefContext childLeafRefContext = buildLeafRefContextReferencedByTree(caseNode, currentModule);
-
if (childLeafRefContext.hasReferencedChild() || childLeafRefContext.isReferenced()) {
currentLeafRefContextBuilder.addReferencedByChild(childLeafRefContext,
childLeafRefContext.getNodeName());
}
}
-
} else if (node instanceof LeafSchemaNode || node instanceof LeafListSchemaNode) {
final List<LeafRefContext> foundLeafRefs = getLeafRefsFor(node, currentModule);
if (!foundLeafRefs.isEmpty()) {
return currentLeafRefContextBuilder.build();
}
- private List<LeafRefContext> getLeafRefsFor(final DataSchemaNode node,
- final Module module) {
- final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(
- node.getPath(), module);
-
+ private List<LeafRefContext> getLeafRefsFor(final DataSchemaNode node, final Module module) {
+ final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(node.getPath(), module);
final List<LeafRefContext> foundLeafRefs = new LinkedList<>();
-
for (final LeafRefContext leafref : leafRefs) {
- final LeafRefPath leafRefTargetPath = leafref
- .getAbsoluteLeafRefTargetPath();
+ final LeafRefPath leafRefTargetPath = leafref.getAbsoluteLeafRefTargetPath();
if (leafRefTargetPath.equals(nodeXPath)) {
foundLeafRefs.add(leafref);
}
return foundLeafRefs;
}
-
}