From 224f1af8a965a8653f80d630a2c93792d3242899 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 5 Sep 2018 19:28:16 +0200 Subject: [PATCH] Cleanup LeafRefContext(Tree)Builder These are package-private classes, make them final, eliminate unneeded methods and improve code formatting. JIRA: YANGTOOLS-892 Change-Id: I8a4925a665b1bcac7abd8088719e80f53229bafe Signed-off-by: Robert Varga (cherry picked from commit d76c8ce876f2609dd9775eb58cfd2ee012b27ad9) --- .../impl/leafref/LeafRefContextBuilder.java | 87 +++++-------- .../leafref/LeafRefContextTreeBuilder.java | 115 ++++++------------ 2 files changed, 65 insertions(+), 137 deletions(-) diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextBuilder.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextBuilder.java index e29fa3a28e..47e9a6152a 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextBuilder.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextBuilder.java @@ -7,6 +7,8 @@ */ 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; @@ -17,15 +19,15 @@ import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaPath; -class LeafRefContextBuilder implements Builder { +final class LeafRefContextBuilder implements Builder { private final Map referencingChildren = new HashMap<>(); private final Map referencedByChildren = new HashMap<>(); private final Map 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; @@ -36,8 +38,9 @@ class LeafRefContextBuilder implements Builder { 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; } @@ -53,99 +56,67 @@ class LeafRefContextBuilder implements Builder { 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 getReferencingChilds() { + Map 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 getReferencedByChilds() { + Map 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; @@ -158,21 +129,21 @@ class LeafRefContextBuilder implements Builder { return absoluteLeafRefTargetPath; } - public Module getLeafRefContextModule() { + Module getLeafRefContextModule() { final Iterator 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 getAllReferencedByLeafRefCtxs() { + Map getAllReferencedByLeafRefCtxs() { return referencedByLeafRefCtx; } } diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java index f17ce4e735..0a23636320 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java @@ -28,7 +28,7 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition; 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 leafRefs = new LinkedList<>(); private final SchemaContext schemaContext; @@ -36,22 +36,16 @@ class LeafRefContextTreeBuilder { 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 modules = schemaContext.getModules(); for (final Module module : modules) { - final Collection 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()); } } } @@ -59,13 +53,10 @@ class LeafRefContextTreeBuilder { for (final Module module : modules) { final Collection 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()); } } } @@ -76,42 +67,26 @@ class LeafRefContextTreeBuilder { 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 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()); } } @@ -156,39 +131,27 @@ class LeafRefContextTreeBuilder { 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 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 foundLeafRefs = getLeafRefsFor(node, currentModule); if (!foundLeafRefs.isEmpty()) { @@ -202,16 +165,11 @@ class LeafRefContextTreeBuilder { return currentLeafRefContextBuilder.build(); } - private List getLeafRefsFor(final DataSchemaNode node, - final Module module) { - final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath( - node.getPath(), module); - + private List getLeafRefsFor(final DataSchemaNode node, final Module module) { + final LeafRefPath nodeXPath = LeafRefUtils.schemaPathToLeafRefPath(node.getPath(), module); final List 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); } @@ -219,5 +177,4 @@ class LeafRefContextTreeBuilder { return foundLeafRefs; } - } -- 2.36.6