Rework LeafRefValidation
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContextBuilder.java
index 4349bd5396bafd717f09f6467c8d36198158ecf4..b8036a474c8b157dea26901f18164f3ab1843296 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -7,19 +7,27 @@
  */
 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.concepts.Builder;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 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 {
+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;
@@ -28,151 +36,114 @@ class LeafRefContextBuilder {
     private boolean isReferencedBy = false;
     private boolean isReferencing = false;
 
-    private Map<QName, LeafRefContext> referencingChilds = new HashMap<QName, LeafRefContext>();
-    private Map<QName, LeafRefContext> referencedByChilds = new HashMap<QName, LeafRefContext>();
-    private Map<QName, LeafRefContext> referencedByLeafRefCtx = new HashMap<QName, LeafRefContext>();
-
-    public LeafRefContextBuilder(final QName currentNodeQName,
-            final SchemaPath currentNodePath, final SchemaContext schemaContext) {
-        this.currentNodeQName = currentNodeQName;
-        this.currentNodePath = currentNodePath;
+    LeafRefContextBuilder(final QName currentNodeQName, final SchemaPath currentNodePath,
+        final SchemaContext schemaContext) {
+        this.currentNodeQName = requireNonNull(currentNodeQName);
+        this.currentNodePath = requireNonNull(currentNodePath);
+        // FIXME: requireNonNull
         this.schemaContext = schemaContext;
     }
 
+    @Override
     public LeafRefContext build() {
         final LeafRefContext leafRefContext = new LeafRefContext(this);
 
-        referencingChilds = new HashMap<QName, LeafRefContext>();
-        referencedByChilds = new HashMap<QName, LeafRefContext>();
-        referencedByLeafRefCtx = new HashMap<QName, LeafRefContext>();
+        // LeafRefContext has made a copy of these
+        referencingChildren.clear();
+        referencedByChildren.clear();
+        referencedByLeafRefCtx.clear();
 
         return leafRefContext;
     }
 
-    public boolean hasLeafRefContextChild() {
-        return hasReferencedByChild() || hasReferencingChild();
-    }
-
-    public boolean hasReferencedByChild() {
-        return !referencedByChilds.isEmpty();
-    }
-
-    public boolean hasReferencingChild() {
-        return !referencingChilds.isEmpty();
-    }
-
-    public boolean isReferencedBy() {
+    boolean isReferencedBy() {
         return isReferencedBy;
     }
 
-    public void setReferencedBy(final boolean isReferencedBy) {
-        this.isReferencedBy = isReferencedBy;
+    void setReferencedBy(final boolean referencedBy) {
+        this.isReferencedBy = referencedBy;
     }
 
-    public boolean isReferencing() {
+    boolean isReferencing() {
         return isReferencing;
     }
 
-    public void setReferencing(final boolean isReferencing) {
-        this.isReferencing = isReferencing;
+    void setReferencing(final boolean referencing) {
+        this.isReferencing = referencing;
     }
 
-    public void addReferencingChild(final LeafRefContext child, final QName childQName) {
-        referencingChilds.put(childQName, child);
+    void addReferencingChild(final LeafRefContext child, final QName childQName) {
+        referencingChildren.put(childQName, child);
     }
 
-    public LeafRefContext getReferencingChildByName(final QName name) {
-        return referencingChilds.get(name);
+    Map<QName, LeafRefContext> getReferencingChilds() {
+        return referencingChildren;
     }
 
-    public Map<QName, LeafRefContext> getReferencingChilds() {
-        return referencingChilds;
+    void addReferencedByChild(final LeafRefContext child, final QName childQName) {
+        referencedByChildren.put(childQName, child);
     }
 
-    public void addReferencedByChild(final LeafRefContext child, final QName childQName) {
-        referencedByChilds.put(childQName, child);
+    Map<QName, LeafRefContext> getReferencedByChilds() {
+        return referencedByChildren;
     }
 
-    public LeafRefContext getReferencedByChildByName(final QName name) {
-        return referencedByChilds.get(name);
-    }
-
-    public Map<QName, LeafRefContext> getReferencedByChilds() {
-        return referencedByChilds;
-    }
-
-    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) {
-        this.leafRefTargetPath = leafRefPath;
+    void setLeafRefTargetPath(final LeafRefPath leafRefPath) {
+        this.leafRefTargetPath = requireNonNull(leafRefPath);
     }
 
-    public String getLeafRefTargetPathString() {
+    String getLeafRefTargetPathString() {
         return leafRefTargetPathString;
     }
 
-    public void setLeafRefTargetPathString(final String leafRefPathString) {
-        this.leafRefTargetPathString = leafRefPathString;
+    void setLeafRefTargetPathString(final String leafRefPathString) {
+        this.leafRefTargetPathString = requireNonNull(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;
             } else {
-                absoluteLeafRefTargetPath = LeafRefUtils
-                        .createAbsoluteLeafRefPath(leafRefTargetPath,
-                                currentNodePath, getLeafRefContextModule());
+                absoluteLeafRefTargetPath = LeafRefUtils.createAbsoluteLeafRefPath(leafRefTargetPath,
+                    currentNodePath, getLeafRefContextModule());
             }
         }
 
         return absoluteLeafRefTargetPath;
     }
 
-    public Module getLeafRefContextModule() {
-        final QNameModule qnameModule = currentNodeQName.getModule();
-
-        return schemaContext.findModuleByNamespaceAndRevision(
-                qnameModule.getNamespace(), qnameModule.getRevision());
+    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;
     }
-
 }