Use Objects.hashCode()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ContainerEffectiveStatementImpl.java
index 7cb1b79a546473e738bb59014bcc8a61e00f502b..f91c1ce5775109d7520cfe295b84da34c3bdf471 100644 (file)
@@ -7,27 +7,27 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
-
-import com.google.common.collect.ImmutableSet;
+import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
+import com.google.common.collect.ImmutableSet;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.LinkedList;
-import java.util.Collection;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import com.google.common.base.Optional;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.stmt.ContainerStatement;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
 public class ContainerEffectiveStatementImpl extends
         AbstractEffectiveDocumentedDataNodeContainer<QName, ContainerStatement>
@@ -37,9 +37,9 @@ public class ContainerEffectiveStatementImpl extends
     private final SchemaPath path;
 
     private boolean presence;
-    private boolean augmenting;
+    boolean augmenting;
     private boolean addedByUses;
-    private boolean configuration;
+    private boolean configuration = true;
     private ContainerSchemaNode original;
     private ConstraintDefinition constraints;
 
@@ -52,36 +52,39 @@ public class ContainerEffectiveStatementImpl extends
 
         qname = ctx.getStatementArgument();
         path = Utils.getSchemaPath(ctx);
+        this.constraints = new EffectiveConstraintDefinitionImpl(this);
 
         initCopyType(ctx);
-        initFields();
-        // :TODO init other fields
+        initSubstatementCollectionsAndFields();
     }
 
     private void initCopyType(
             StmtContext<QName, ContainerStatement, EffectiveStatement<QName, ContainerStatement>> ctx) {
 
-        TypeOfCopy typeOfCopy = ctx.getTypeOfCopy();
-        switch (typeOfCopy) {
-        case ADDED_BY_AUGMENTATION:
+        List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
+
+        if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
             augmenting = true;
-            original = (ContainerSchemaNode) ctx.getOriginalCtx().buildEffective();
-            break;
-        case ADDED_BY_USES:
+        }
+        if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
             addedByUses = true;
+        }
+        if(copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
+            addedByUses = augmenting = true;
+        }
+
+        if (ctx.getOriginalCtx() != null) {
             original = (ContainerSchemaNode) ctx.getOriginalCtx().buildEffective();
-            break;
-        default:
-            break;
         }
     }
 
-    private void initFields() {
+    private void initSubstatementCollectionsAndFields() {
         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
 
         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
 
+        boolean configurationInit = false;
         for (EffectiveStatement<?, ?> effectiveSubstatement : effectiveSubstatements) {
             if (effectiveSubstatement instanceof UnknownSchemaNode) {
                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveSubstatement;
@@ -94,9 +97,11 @@ public class ContainerEffectiveStatementImpl extends
             if (effectiveSubstatement instanceof PresenceEffectiveStatementImpl) {
                 presence = true;
             }
-            if (effectiveSubstatement instanceof ConfigEffectiveStatementImpl) {
-                ConfigEffectiveStatementImpl config = (ConfigEffectiveStatementImpl) effectiveSubstatement;
-                this.configuration = config.argument();
+            if (!configurationInit
+                    && effectiveSubstatement instanceof ConfigEffectiveStatementImpl) {
+                ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveSubstatement;
+                this.configuration = configStmt.argument();
+                configurationInit = true;
             }
         }
 
@@ -158,8 +163,8 @@ public class ContainerEffectiveStatementImpl extends
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((qname == null) ? 0 : qname.hashCode());
-        result = prime * result + ((path == null) ? 0 : path.hashCode());
+        result = prime * result + Objects.hashCode(qname);
+        result = prime * result + Objects.hashCode(path);
         return result;
     }
 
@@ -196,4 +201,4 @@ public class ContainerEffectiveStatementImpl extends
     public String toString() {
         return "container " + qname.getLocalName();
     }
-}
\ No newline at end of file
+}