Cleanup LeafRefContext(Tree)Builder 81/75781/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Sep 2018 17:28:16 +0000 (19:28 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 5 Sep 2018 22:21:23 +0000 (22:21 +0000)
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 <robert.varga@pantheon.tech>
(cherry picked from commit d76c8ce876f2609dd9775eb58cfd2ee012b27ad9)

yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextBuilder.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java

index e29fa3a28e7e443c0a1b96e64b57720d610ea25a..47e9a6152adc7b4acc37a2577c01fb4ae7fb9d00 100644 (file)
@@ -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<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;
@@ -36,8 +38,9 @@ class LeafRefContextBuilder implements Builder<LeafRefContext> {
 
     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<LeafRefContext> {
         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;
@@ -158,21 +129,21 @@ class LeafRefContextBuilder implements Builder<LeafRefContext> {
         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;
     }
 }
index f17ce4e735fef4c8adcdf6b222dd076f84750f6d..0a23636320ff0dd078c06a2844c5d1e777f83f0c 100644 (file)
@@ -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<LeafRefContext> 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<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());
                 }
             }
         }
@@ -59,13 +53,10 @@ class LeafRefContextTreeBuilder {
         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());
                 }
             }
         }
@@ -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<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());
                 }
             }
 
@@ -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<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()) {
@@ -202,16 +165,11 @@ class LeafRefContextTreeBuilder {
         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);
             }
@@ -219,5 +177,4 @@ class LeafRefContextTreeBuilder {
 
         return foundLeafRefs;
     }
-
 }