Use String concatenation instead of StringBuffer/Builder
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / UsesEffectiveStatementImpl.java
index f2c7f508aa1515840a82c837e12669e67f43e5f1..aec4f83b7b56836cd6d9925dc7a96fb5d30b9885 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * 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,
@@ -7,34 +7,78 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+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.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.GroupingStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
+import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
 
-public class UsesEffectiveStatementImpl extends EffectiveStatementBase<QName, UsesStatement>implements UsesNode {
-    private SchemaPath groupingPath;
-    ImmutableSet<AugmentationSchema> augmentations;
-    private boolean addedByUses;
-    ImmutableMap<SchemaPath, SchemaNode> refines;
-    ImmutableList<UnknownSchemaNode> unknownNodes;
+public final class UsesEffectiveStatementImpl extends DeclaredEffectiveStatementBase<QName, UsesStatement> implements UsesNode {
+    private final SchemaPath groupingPath;
+    private final boolean addedByUses;
+    private final Map<SchemaPath, SchemaNode> refines;
+    private final Set<AugmentationSchema> augmentations;
+    private final List<UnknownSchemaNode> unknownNodes;
 
-    public UsesEffectiveStatementImpl(StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
+    public UsesEffectiveStatementImpl(
+            final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
         super(ctx);
 
-        this.groupingPath = null;
+        // initGroupingPath
+        StmtContext<?, GroupingStatement, EffectiveStatement<QName, GroupingStatement>> grpCtx = ctx.getFromNamespace(
+                GroupingNamespace.class, ctx.getStatementArgument());
+        this.groupingPath = grpCtx.getSchemaPath().get();
+
+        // initCopyType
+        List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
+        if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
+            addedByUses = true;
+        } else {
+            addedByUses = false;
+        }
+
+        // initSubstatementCollections
+        Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
+        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
+        Set<AugmentationSchema> augmentationsInit = new HashSet<>();
+        Map<SchemaPath, SchemaNode> refinesInit = new HashMap<>();
+        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
+            if (effectiveStatement instanceof UnknownSchemaNode) {
+                UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
+                unknownNodesInit.add(unknownNode);
+            }
+            if (effectiveStatement instanceof AugmentationSchema) {
+                AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
+                augmentationsInit.add(augmentationSchema);
+            }
+            if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
+                RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
+                SchemaNodeIdentifier identifier = refineStmt.argument();
+                refinesInit.put(identifier.asSchemaPath(), refineStmt.getRefineTargetNode());
+            }
+        }
+        this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
+        this.augmentations = ImmutableSet.copyOf(augmentationsInit);
+        this.refines = ImmutableMap.copyOf(refinesInit);
     }
 
     @Override
@@ -57,10 +101,6 @@ public class UsesEffectiveStatementImpl extends EffectiveStatementBase<QName, Us
         return addedByUses;
     }
 
-    void setAddedByUses(final boolean addedByUses) {
-        this.addedByUses = addedByUses;
-    }
-
     @Override
     public Map<SchemaPath, SchemaNode> getRefines() {
         return refines;
@@ -74,8 +114,8 @@ public class UsesEffectiveStatementImpl extends EffectiveStatementBase<QName, Us
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
-        result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
+        result = prime * result + Objects.hashCode(groupingPath);
+        result = prime * result + Objects.hashCode(augmentations);
         return result;
     }
 
@@ -91,29 +131,13 @@ public class UsesEffectiveStatementImpl extends EffectiveStatementBase<QName, Us
             return false;
         }
         final UsesEffectiveStatementImpl other = (UsesEffectiveStatementImpl) obj;
-        if (groupingPath == null) {
-            if (other.groupingPath != null) {
-                return false;
-            }
-        } else if (!groupingPath.equals(other.groupingPath)) {
-            return false;
-        }
-        if (augmentations == null) {
-            if (other.augmentations != null) {
-                return false;
-            }
-        } else if (!augmentations.equals(other.augmentations)) {
-            return false;
-        }
-        return true;
+        return Objects.equals(groupingPath, other.groupingPath) && Objects.equals(augmentations, other.augmentations);
     }
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(UsesEffectiveStatementImpl.class.getSimpleName());
-        sb.append("[groupingPath=");
-        sb.append(groupingPath);
-        sb.append("]");
-        return sb.toString();
+        return UsesEffectiveStatementImpl.class.getSimpleName() + "[groupingPath=" +
+                groupingPath +
+                "]";
     }
-}
\ No newline at end of file
+}