BUG-6497: Do not lose augmentation statement order
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / GroupingEffectiveStatementImpl.java
index e7cbe4ca0307f1683aa8cb7bc7e4c29dae9dcef5..7f0275d600cea71a2f2ed7477e42ceb57f63f4dc 100644 (file)
@@ -1,48 +1,57 @@
+/*
+ * 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
+ */
+
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import java.util.Collection;
 import java.util.LinkedList;
-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.GroupingStatement;
 import java.util.List;
+import java.util.Objects;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 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.GroupingStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
 
 public class GroupingEffectiveStatementImpl extends
-        AbstractEffectiveDocumentedDataNodeContainer<QName, GroupingStatement>
-        implements GroupingDefinition {
+        AbstractEffectiveDocumentedDataNodeContainer<QName, GroupingStatement> implements GroupingDefinition {
     private final QName qname;
     private final SchemaPath path;
-
-    private boolean addedByUses;
-    private List<UnknownSchemaNode> unknownNodes;
+    private final boolean addedByUses;
+    private final List<UnknownSchemaNode> unknownNodes;
 
     public GroupingEffectiveStatementImpl(
-            StmtContext<QName, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> ctx) {
+            final StmtContext<QName, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> ctx) {
         super(ctx);
 
         qname = ctx.getStatementArgument();
-        // :TODO init other fields
-        path = null;
-
-        initSubstatementCollections();
-    }
+        path = ctx.getSchemaPath().get();
+
+        // initCopyType
+        List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
+        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
+            addedByUses = true;
+        } else {
+            addedByUses = false;
+        }
 
-    private void initSubstatementCollections() {
+        // initSubstatementCollections
         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
-
-        unknownNodes = new LinkedList<UnknownSchemaNode>();
-
+        unknownNodes = new LinkedList<>();
         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
             if (effectiveStatement instanceof UnknownSchemaNode) {
                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
                 unknownNodes.add(unknownNode);
             }
         }
-
     }
 
     @Override
@@ -69,8 +78,8 @@ public class GroupingEffectiveStatementImpl 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;
     }
 
@@ -86,30 +95,15 @@ public class GroupingEffectiveStatementImpl extends
             return false;
         }
         final GroupingEffectiveStatementImpl other = (GroupingEffectiveStatementImpl) obj;
-        if (qname == null) {
-            if (other.qname != null) {
-                return false;
-            }
-        } else if (!qname.equals(other.qname)) {
-            return false;
-        }
-        if (path == null) {
-            if (other.path != null) {
-                return false;
-            }
-        } else if (!path.equals(other.path)) {
-            return false;
-        }
-        return true;
+        return Objects.equals(qname, other.qname) && Objects.equals(path, other.path);
     }
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(
-                GroupingEffectiveStatementImpl.class.getSimpleName());
+        StringBuilder sb = new StringBuilder(GroupingEffectiveStatementImpl.class.getSimpleName());
         sb.append("[");
         sb.append("qname=").append(qname);
         sb.append("]");
         return sb.toString();
     }
-}
\ No newline at end of file
+}