Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ImportEffectiveStatementImpl.java
index 7eb4e6c03bcaaad125abac5ad2454ac74842c9c4..03fde57ee243baae7c91f5b6d64f0483fbb23fdf 100644 (file)
@@ -7,34 +7,35 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
-
 import java.util.Date;
+import java.util.Objects;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-public class ImportEffectiveStatementImpl extends EffectiveStatementBase<String, ImportStatement> implements
+public class ImportEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, ImportStatement> implements
         ModuleImport {
 
-    private String moduleName;
-    private Date revision;
-    private String prefix;
+    private final String moduleName;
+    private final Date revision;
+    private final String prefix;
 
-    public ImportEffectiveStatementImpl(StmtContext<String, ImportStatement, ?> ctx) {
+    public ImportEffectiveStatementImpl(final StmtContext<String, ImportStatement, ?> ctx) {
         super(ctx);
 
         moduleName = ctx.getStatementArgument();
-        revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
 
-        for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
-            if (effectiveStatement instanceof RevisionDateEffectiveStatementImpl) {
-                revision = ((RevisionDateEffectiveStatementImpl) effectiveStatement).argument();
-            }
-            if (effectiveStatement instanceof PrefixEffectiveStatementImpl) {
-                prefix = ((PrefixEffectiveStatementImpl) effectiveStatement).argument();
-            }
+        RevisionDateEffectiveStatementImpl revisionDateStmt = firstEffective(RevisionDateEffectiveStatementImpl.class);
+        this.revision = (revisionDateStmt == null) ? SimpleDateFormatUtil.DEFAULT_DATE_IMP : revisionDateStmt.argument();
+
+        PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
+        if (prefixStmt != null ) {
+            this.prefix = prefixStmt.argument();
+        } else {
+            throw new MissingSubstatementException("Prefix is mandatory substatement of import statement",
+                    ctx.getStatementSourceReference());
         }
     }
 
@@ -57,14 +58,14 @@ public class ImportEffectiveStatementImpl extends EffectiveStatementBase<String,
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
-        result = prime * result + ((revision == null) ? 0 : revision.hashCode());
-        result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
+        result = prime * result + Objects.hashCode(moduleName);
+        result = prime * result + Objects.hashCode(revision);
+        result = prime * result + Objects.hashCode(prefix);
         return result;
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -75,25 +76,13 @@ public class ImportEffectiveStatementImpl extends EffectiveStatementBase<String,
             return false;
         }
         ImportEffectiveStatementImpl other = (ImportEffectiveStatementImpl) obj;
-        if (getModuleName() == null) {
-            if (other.getModuleName() != null) {
-                return false;
-            }
-        } else if (!getModuleName().equals(other.getModuleName())) {
+        if (!Objects.equals(getModuleName(), other.getModuleName())) {
             return false;
         }
-        if (getRevision() == null) {
-            if (other.getRevision() != null) {
-                return false;
-            }
-        } else if (!getRevision().equals(other.getRevision())) {
+        if (!Objects.equals(getRevision(), other.getRevision())) {
             return false;
         }
-        if (getPrefix() == null) {
-            if (other.getPrefix() != null) {
-                return false;
-            }
-        } else if (!getPrefix().equals(other.getPrefix())) {
+        if (!Objects.equals(getPrefix(), other.getPrefix())) {
             return false;
         }
         return true;