Introduce SchemaNodeIdentifier.asSchemaPath()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AugmentEffectiveStatementImpl.java
index 0d3f3741f768db64de227e2e8b2dc5b924086a69..366c6acca1ce6662344c77acd61e34f8534d268c 100644 (file)
@@ -8,68 +8,82 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import static com.google.common.base.Preconditions.checkNotNull;
-import java.util.Collection;
-import java.util.LinkedList;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import java.net.URI;
+import java.util.Collection;
 import java.util.Date;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Objects;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
 public class AugmentEffectiveStatementImpl
-        extends
-        AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
-        implements AugmentationSchema, NamespaceRevisionAware,
-        Comparable<AugmentEffectiveStatementImpl> {
-    private final int order;
+        extends AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
+        implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentEffectiveStatementImpl> {
     private final SchemaPath targetPath;
-    RevisionAwareXPath whenCondition;
-
-    URI namespace;
-    Date revision;
-    ImmutableList<UnknownSchemaNode> unknownNodes;
+    private final URI namespace;
+    private final Date revision;
+    private final int order;
+    private ImmutableList<UnknownSchemaNode> unknownNodes;
+    private RevisionAwareXPath whenCondition;
     private AugmentationSchema copyOf;
 
     public AugmentEffectiveStatementImpl(
-            StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
+            final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
         super(ctx);
 
-        SchemaNodeIdentifier schemaNodeIdentifier = ctx.getStatementArgument();
-        this.targetPath = SchemaPath.create(
-                schemaNodeIdentifier.getPathFromRoot(),
-                schemaNodeIdentifier.isAbsolute());
+        this.targetPath = ctx.getStatementArgument().asSchemaPath();
+        QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
+        this.namespace = rootModuleQName.getNamespace();
+        this.revision = rootModuleQName.getRevision();
 
-        // :TODO init other fields
-        this.order = 1;
-        // firstEffective(WhenEffectiveStatementImpl.class);
+        this.order = ctx.getOrder();
 
+        initCopyOf(ctx);
         initSubstatementCollections();
     }
 
+    private void initCopyOf(
+            final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
+        StatementContextBase<?, ?, ?> originalCtx = ctx.getOriginalCtx();
+        if (originalCtx != null) {
+            this.copyOf = (AugmentationSchema) originalCtx.buildEffective();
+        }
+    }
+
     private void initSubstatementCollections() {
         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
 
-        LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
+        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
 
+        boolean initWhen = false;
         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
             if (effectiveStatement instanceof UnknownSchemaNode) {
                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
-                unknownNodes.add(unknownNode);
+                unknownNodesInit.add(unknownNode);
+            }
+            if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
+                WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
+                whenCondition = whenStmt.argument();
+                initWhen = true;
             }
         }
 
-        this.unknownNodes = ImmutableList.copyOf(unknownNodes);
+        this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
     }
 
     public void setCopyOf(final AugmentationSchema build) {
@@ -110,10 +124,8 @@ public class AugmentEffectiveStatementImpl
     public int hashCode() {
         final int prime = 17;
         int result = 1;
-        result = prime * result
-                + ((targetPath == null) ? 0 : targetPath.hashCode());
-        result = prime * result
-                + ((whenCondition == null) ? 0 : whenCondition.hashCode());
+        result = prime * result + Objects.hashCode(targetPath);
+        result = prime * result + Objects.hashCode(whenCondition);
         result = prime * result + getChildNodes().hashCode();
         return result;
     }
@@ -130,18 +142,10 @@ public class AugmentEffectiveStatementImpl
             return false;
         }
         AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
-        if (targetPath == null) {
-            if (other.targetPath != null) {
-                return false;
-            }
-        } else if (!targetPath.equals(other.targetPath)) {
+        if (!Objects.equals(targetPath, other.targetPath)) {
             return false;
         }
-        if (whenCondition == null) {
-            if (other.whenCondition != null) {
-                return false;
-            }
-        } else if (!whenCondition.equals(other.whenCondition)) {
+        if (!Objects.equals(whenCondition, other.whenCondition)) {
             return false;
         }
         if (!getChildNodes().equals(other.getChildNodes())) {
@@ -152,8 +156,7 @@ public class AugmentEffectiveStatementImpl
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(
-                AugmentEffectiveStatementImpl.class.getSimpleName());
+        StringBuilder sb = new StringBuilder(AugmentEffectiveStatementImpl.class.getSimpleName());
         sb.append("[");
         sb.append("targetPath=").append(targetPath);
         sb.append(", when=").append(whenCondition);
@@ -165,8 +168,7 @@ public class AugmentEffectiveStatementImpl
     public int compareTo(final AugmentEffectiveStatementImpl o) {
         checkNotNull(o);
         Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
-        Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot()
-                .iterator();
+        Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
         while (thisIt.hasNext()) {
             if (otherIt.hasNext()) {
                 int comp = thisIt.next().compareTo(otherIt.next());
@@ -182,4 +184,4 @@ public class AugmentEffectiveStatementImpl
         }
         return this.order - o.order;
     }
-}
\ No newline at end of file
+}