BUG-7052: Move qnameFromArgument to StmtContextUtils
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AugmentEffectiveStatementImpl.java
index 366c6acca1ce6662344c77acd61e34f8534d268c..f97300282e2c07e0303b4940fdb628b406a061dc 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -8,19 +8,23 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 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 java.util.Set;
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.NamespaceRevisionAware;
+import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
@@ -28,66 +32,54 @@ 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;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 
-public class AugmentEffectiveStatementImpl
-        extends AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
-        implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentEffectiveStatementImpl> {
+public final class AugmentEffectiveStatementImpl extends
+        AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement> implements
+        AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentEffectiveStatementImpl> {
     private final SchemaPath targetPath;
     private final URI namespace;
     private final Date revision;
     private final int order;
-    private ImmutableList<UnknownSchemaNode> unknownNodes;
-    private RevisionAwareXPath whenCondition;
-    private AugmentationSchema copyOf;
+    private final Set<ActionDefinition> actions;
+    private final Set<NotificationDefinition> notifications;
+    private final List<UnknownSchemaNode> unknownNodes;
+    private final RevisionAwareXPath whenCondition;
+    private final AugmentationSchema copyOf;
 
     public AugmentEffectiveStatementImpl(
             final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
         super(ctx);
 
         this.targetPath = ctx.getStatementArgument().asSchemaPath();
-        QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
+
+        final QNameModule rootModuleQName = StmtContextUtils.getRootModuleQName(ctx);
         this.namespace = rootModuleQName.getNamespace();
         this.revision = rootModuleQName.getRevision();
 
         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();
-
-        List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
-
-        boolean initWhen = false;
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof UnknownSchemaNode) {
-                UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
-                unknownNodesInit.add(unknownNode);
-            }
-            if(!initWhen && effectiveStatement instanceof WhenEffectiveStatementImpl) {
-                WhenEffectiveStatementImpl whenStmt = (WhenEffectiveStatementImpl) effectiveStatement;
-                whenCondition = whenStmt.argument();
-                initWhen = true;
+        this.copyOf = ctx.getOriginalCtx() == null ? null : (AugmentationSchema) ctx.getOriginalCtx().buildEffective();
+
+        final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
+        this.whenCondition = whenStmt == null ? null : whenStmt.argument();
+
+        // initSubstatementCollections
+        final ImmutableSet.Builder<ActionDefinition> actionsBuilder = ImmutableSet.builder();
+        final ImmutableSet.Builder<NotificationDefinition> notificationsBuilder = ImmutableSet.builder();
+        final ImmutableList.Builder<UnknownSchemaNode> listBuilder = new ImmutableList.Builder<>();
+        for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
+            if (effectiveStatement instanceof ActionDefinition) {
+                actionsBuilder.add((ActionDefinition) effectiveStatement);
+            } else if (effectiveStatement instanceof NotificationDefinition) {
+                notificationsBuilder.add((NotificationDefinition) effectiveStatement);
+            } else if (effectiveStatement instanceof UnknownSchemaNode) {
+                listBuilder.add((UnknownSchemaNode) effectiveStatement);
             }
         }
 
-        this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
-    }
-
-    public void setCopyOf(final AugmentationSchema build) {
-        this.copyOf = build;
+        this.actions = actionsBuilder.build();
+        this.notifications = notificationsBuilder.build();
+        this.unknownNodes = listBuilder.build();
     }
 
     @Override
@@ -105,6 +97,7 @@ public class AugmentEffectiveStatementImpl
         return whenCondition;
     }
 
+    @Nonnull
     @Override
     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
         return unknownNodes;
@@ -115,6 +108,16 @@ public class AugmentEffectiveStatementImpl
         return namespace;
     }
 
+    @Override
+    public Set<ActionDefinition> getActions() {
+        return actions;
+    }
+
+    @Override
+    public Set<NotificationDefinition> getNotifications() {
+        return notifications;
+    }
+
     @Override
     public Date getRevision() {
         return revision;
@@ -141,7 +144,7 @@ public class AugmentEffectiveStatementImpl
         if (getClass() != obj.getClass()) {
             return false;
         }
-        AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
+        final AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
         if (!Objects.equals(targetPath, other.targetPath)) {
             return false;
         }
@@ -156,22 +159,18 @@ public class AugmentEffectiveStatementImpl
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(AugmentEffectiveStatementImpl.class.getSimpleName());
-        sb.append("[");
-        sb.append("targetPath=").append(targetPath);
-        sb.append(", when=").append(whenCondition);
-        sb.append("]");
-        return sb.toString();
+        return AugmentEffectiveStatementImpl.class.getSimpleName() + "[" + "targetPath=" + targetPath + ", when="
+                + whenCondition + "]";
     }
 
     @Override
-    public int compareTo(final AugmentEffectiveStatementImpl o) {
+    public int compareTo(@Nonnull final AugmentEffectiveStatementImpl o) {
         checkNotNull(o);
-        Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
-        Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
+        final Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
+        final Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot().iterator();
         while (thisIt.hasNext()) {
             if (otherIt.hasNext()) {
-                int comp = thisIt.next().compareTo(otherIt.next());
+                final int comp = thisIt.next().compareTo(otherIt.next());
                 if (comp != 0) {
                     return comp;
                 }