Use Objects.hashCode()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ListEffectiveStatementImpl.java
index 4e205bde1a1ab1b36f39cf5dfd02ded1eee51311..303604f5d2f7df1637732e1790af68fb5f337b50 100644 (file)
@@ -1,38 +1,46 @@
-package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
+package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedList;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedList;
 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.DerivableSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+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.ListStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 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 ListEffectiveStatementImpl extends
-        AbstractEffectiveDocumentedDataNodeContainer<QName, ListStatement>
+public class ListEffectiveStatementImpl extends AbstractEffectiveDocumentedDataNodeContainer<QName, ListStatement>
         implements ListSchemaNode, DerivableSchemaNode {
     private final QName qname;
     private final SchemaPath path;
 
     boolean augmenting;
-    boolean addedByUses;
+    private boolean addedByUses;
     ListSchemaNode original;
-    boolean configuration;
+    boolean configuration = true;
     ConstraintDefinition constraints;
     boolean userOrdered;
 
@@ -40,59 +48,77 @@ public class ListEffectiveStatementImpl extends
     ImmutableSet<AugmentationSchema> augmentations;
     ImmutableList<UnknownSchemaNode> unknownNodes;
 
-    public ListEffectiveStatementImpl(
-            StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
+    public ListEffectiveStatementImpl(StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
         super(ctx);
         this.qname = ctx.getStatementArgument();
         this.path = Utils.getSchemaPath(ctx);
-        // :TODO init other fields
+        this.constraints = new EffectiveConstraintDefinitionImpl(this);
 
-        initKeyDefinition();
-        initSubstatementCollections();
+        initSubstatementCollectionsAndFields();
         initCopyType(ctx);
+
+        // should be after initSubstatementCollectionsAndFields()
+        initKeyDefinition(ctx);
     }
 
     private void initCopyType(
             StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> 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 = (ListSchemaNode) 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 = (ListSchemaNode) ctx.getOriginalCtx().buildEffective();
-            break;
-        default:
-            break;
         }
     }
 
-    /**
-     *
-     */
-    private void initKeyDefinition() {
-        List<QName> keyDefinitionInit = new LinkedList<QName>();
-        KeyEffectiveStatementImpl key = firstEffective(KeyEffectiveStatementImpl.class);
+    private void initKeyDefinition(StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
+        List<QName> keyDefinitionInit = new LinkedList<>();
+        KeyEffectiveStatementImpl keyEffectiveSubstatement = firstEffective(KeyEffectiveStatementImpl.class);
+
+        if (keyEffectiveSubstatement != null) {
+            Set<QName> possibleLeafQNamesForKey = new HashSet<>();
+
+            for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
+                if (effectiveStatement instanceof LeafSchemaNode) {
+                    possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
+                }
+            }
+
+            Collection<SchemaNodeIdentifier> keys = keyEffectiveSubstatement.argument();
+            for (SchemaNodeIdentifier key : keys) {
+                final QName keyQName = key.getLastComponent();
+
+                if (!possibleLeafQNamesForKey.contains(keyQName)) {
+                    throw new IllegalArgumentException(String.format("Key '%s' misses node '%s' in list '%s', file %s",
+                            keyEffectiveSubstatement.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument(),
+                            ctx.getStatementSourceReference()));
+                }
 
-        if (key != null) {
-            Collection<SchemaNodeIdentifier> keyParts = key.argument();
-            for (SchemaNodeIdentifier keyPart : keyParts) {
-                keyDefinitionInit.add(keyPart.getLastComponent());
+                keyDefinitionInit.add(keyQName);
             }
         }
 
         this.keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
     }
 
-    private void initSubstatementCollections() {
+    private void initSubstatementCollectionsAndFields() {
         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
 
-        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<UnknownSchemaNode>();
-        Set<AugmentationSchema> augmentationsInit = new HashSet<AugmentationSchema>();
+        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
+        Set<AugmentationSchema> augmentationsInit = new HashSet<>();
 
+        boolean configurationInit = false;
+        boolean userOrderedInit = false;
         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
             if (effectiveStatement instanceof UnknownSchemaNode) {
                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
@@ -102,6 +128,16 @@ public class ListEffectiveStatementImpl extends
                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
                 augmentationsInit.add(augmentationSchema);
             }
+            if (!configurationInit && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
+                ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
+                this.configuration = configStmt.argument();
+                configurationInit = true;
+            }
+            if (!userOrderedInit && effectiveStatement instanceof OrderedByEffectiveStatementImpl) {
+                OrderedByEffectiveStatementImpl orderedByStmt = (OrderedByEffectiveStatementImpl) effectiveStatement;
+                this.userOrdered = orderedByStmt.argument().equals("user") ? true : false;
+                userOrderedInit = true;
+            }
         }
 
         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
@@ -167,8 +203,8 @@ public class ListEffectiveStatementImpl 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;
     }
 
@@ -205,4 +241,4 @@ public class ListEffectiveStatementImpl extends
     public String toString() {
         return "list " + qname.getLocalName();
     }
-}
\ No newline at end of file
+}