Introduce WhenConditionAware
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AugmentEffectiveStatementImpl.java
index 0d3f3741f768db64de227e2e8b2dc5b924086a69..62edcab92a553d2b2d1244e4d4a4be6f4252cc71 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
@@ -7,78 +7,78 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-import java.util.Collection;
-import java.util.LinkedList;
-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 com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import java.net.URI;
-import java.util.Date;
-import java.util.Iterator;
 import java.util.List;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 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;
+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.spi.meta.StmtContextUtils;
 
-public class AugmentEffectiveStatementImpl
-        extends
-        AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
-        implements AugmentationSchema, NamespaceRevisionAware,
-        Comparable<AugmentEffectiveStatementImpl> {
-    private final int order;
+public final class AugmentEffectiveStatementImpl
+        extends AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
+        implements AugmentationSchemaNode, NamespaceRevisionAware {
     private final SchemaPath targetPath;
-    RevisionAwareXPath whenCondition;
-
-    URI namespace;
-    Date revision;
-    ImmutableList<UnknownSchemaNode> unknownNodes;
-    private AugmentationSchema copyOf;
-
-    public AugmentEffectiveStatementImpl(
-            StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
+    private final URI namespace;
+    private final Revision revision;
+    private final Set<ActionDefinition> actions;
+    private final Set<NotificationDefinition> notifications;
+    private final List<UnknownSchemaNode> unknownNodes;
+    private final RevisionAwareXPath whenCondition;
+    private final AugmentationSchemaNode copyOf;
+
+    public AugmentEffectiveStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement,
+            EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
         super(ctx);
 
-        SchemaNodeIdentifier schemaNodeIdentifier = ctx.getStatementArgument();
-        this.targetPath = SchemaPath.create(
-                schemaNodeIdentifier.getPathFromRoot(),
-                schemaNodeIdentifier.isAbsolute());
+        this.targetPath = ctx.getStatementArgument().asSchemaPath();
 
-        // :TODO init other fields
-        this.order = 1;
-        // firstEffective(WhenEffectiveStatementImpl.class);
+        final QNameModule rootModuleQName = StmtContextUtils.getRootModuleQName(ctx);
+        this.namespace = rootModuleQName.getNamespace();
+        this.revision = rootModuleQName.getRevision().orElse(null);
 
-        initSubstatementCollections();
-    }
+        this.copyOf = (AugmentationSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
 
-    private void initSubstatementCollections() {
-        Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
+        final WhenEffectiveStatementImpl whenStmt = firstEffective(WhenEffectiveStatementImpl.class);
+        this.whenCondition = whenStmt == null ? null : whenStmt.argument();
 
-        LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
-
-        for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
-            if (effectiveStatement instanceof UnknownSchemaNode) {
-                UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
-                unknownNodes.add(unknownNode);
+        // 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(unknownNodes);
-    }
-
-    public void setCopyOf(final AugmentationSchema build) {
-        this.copyOf = build;
+        this.actions = actionsBuilder.build();
+        this.notifications = notificationsBuilder.build();
+        this.unknownNodes = listBuilder.build();
     }
 
     @Override
-    public Optional<AugmentationSchema> getOriginalDefinition() {
-        return Optional.fromNullable(this.copyOf);
+    public Optional<AugmentationSchemaNode> getOriginalDefinition() {
+        return Optional.ofNullable(this.copyOf);
     }
 
     @Override
@@ -87,10 +87,11 @@ public class AugmentEffectiveStatementImpl
     }
 
     @Override
-    public RevisionAwareXPath getWhenCondition() {
-        return whenCondition;
+    public Optional<RevisionAwareXPath> getWhenCondition() {
+        return Optional.ofNullable(whenCondition);
     }
 
+    @Nonnull
     @Override
     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
         return unknownNodes;
@@ -102,18 +103,26 @@ public class AugmentEffectiveStatementImpl
     }
 
     @Override
-    public Date getRevision() {
-        return revision;
+    public Set<ActionDefinition> getActions() {
+        return actions;
+    }
+
+    @Override
+    public Set<NotificationDefinition> getNotifications() {
+        return notifications;
+    }
+
+    @Override
+    public Optional<Revision> getRevision() {
+        return Optional.ofNullable(revision);
     }
 
     @Override
     public int hashCode() {
         final int prime = 17;
         int result = 1;
-        result = prime * result
-                + ((targetPath == null) ? 0 : targetPath.hashCode());
-        result = prime * result
-                + ((whenCondition == null) ? 0 : whenCondition.hashCode());
+        result = prime * result + Objects.hashCode(targetPath);
+        result = prime * result + Objects.hashCode(whenCondition);
         result = prime * result + getChildNodes().hashCode();
         return result;
     }
@@ -129,19 +138,11 @@ public class AugmentEffectiveStatementImpl
         if (getClass() != obj.getClass()) {
             return false;
         }
-        AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
-        if (targetPath == null) {
-            if (other.targetPath != null) {
-                return false;
-            }
-        } else if (!targetPath.equals(other.targetPath)) {
+        final AugmentEffectiveStatementImpl other = (AugmentEffectiveStatementImpl) obj;
+        if (!Objects.equals(targetPath, other.targetPath)) {
             return false;
         }
-        if (whenCondition == null) {
-            if (other.whenCondition != null) {
-                return false;
-            }
-        } else if (!whenCondition.equals(other.whenCondition)) {
+        if (!Objects.equals(whenCondition, other.whenCondition)) {
             return false;
         }
         if (!getChildNodes().equals(other.getChildNodes())) {
@@ -152,34 +153,7 @@ 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();
-    }
-
-    @Override
-    public int compareTo(final AugmentEffectiveStatementImpl o) {
-        checkNotNull(o);
-        Iterator<QName> thisIt = this.targetPath.getPathFromRoot().iterator();
-        Iterator<QName> otherIt = o.getTargetPath().getPathFromRoot()
-                .iterator();
-        while (thisIt.hasNext()) {
-            if (otherIt.hasNext()) {
-                int comp = thisIt.next().compareTo(otherIt.next());
-                if (comp != 0) {
-                    return comp;
-                }
-            } else {
-                return 1;
-            }
-        }
-        if (otherIt.hasNext()) {
-            return -1;
-        }
-        return this.order - o.order;
+        return AugmentEffectiveStatementImpl.class.getSimpleName() + "[" + "targetPath=" + targetPath + ", when="
+                + whenCondition + "]";
     }
-}
\ No newline at end of file
+}