Introduce formatting methods for SourceException
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / SubstatementValidator.java
index 362fd04547b535ee2533c3ebc7fc51ea169e73c5..761b60c1f2ff2477bf3642f242e47dce9e229f6c 100644 (file)
@@ -8,34 +8,34 @@
 
 package org.opendaylight.yangtools.yang.parser.spi;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.MapDifference;
 import com.google.common.collect.Maps;
-import java.util.ArrayList;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
-import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
 public final class SubstatementValidator {
+    public final static int MAX = Integer.MAX_VALUE;
     private final Map<StatementDefinition, Cardinality> cardinalityMap;
     private final StatementDefinition currentStatement;
     private final SpecialCase specialCase;
-    public final static int MAX = Integer.MAX_VALUE;
 
-    private SubstatementValidator(Builder builder, SpecialCase specialCase) {
+    private SubstatementValidator(final Builder builder, final SpecialCase specialCase) {
         this.cardinalityMap = builder.cardinalityMap.build();
         this.currentStatement = builder.currentStatement;
         this.specialCase = specialCase;
     }
 
-    public static Builder builder(StatementDefinition currentStatement) {
+    public static Builder builder(final StatementDefinition currentStatement) {
         return new Builder(currentStatement);
     }
 
@@ -43,11 +43,11 @@ public final class SubstatementValidator {
         private final ImmutableMap.Builder<StatementDefinition, Cardinality> cardinalityMap = ImmutableMap.builder();
         private final StatementDefinition currentStatement;
 
-        private Builder(StatementDefinition currentStatement) {
+        private Builder(final StatementDefinition currentStatement) {
             this.currentStatement = currentStatement;
         }
 
-        public Builder add(StatementDefinition d, int min, int max) {
+        public Builder add(final StatementDefinition d, final int min, final int max) {
             this.cardinalityMap.put(d, new Cardinality(min, max));
             return this;
         }
@@ -56,19 +56,17 @@ public final class SubstatementValidator {
             return new SubstatementValidator(this, SpecialCase.NULL);
         }
 
-        public SubstatementValidator build(SpecialCase specialCase) {
+        public SubstatementValidator build(final SpecialCase specialCase) {
             return new SubstatementValidator(this, specialCase);
         }
     }
 
     public void validate(final StmtContext<?, ?, ?> ctx) throws InvalidSubstatementException,
             MissingSubstatementException {
-        final Map<StatementDefinition, Integer> stmtDefMap = new HashMap<>();
-        final Map<StatementDefinition, Integer> validatedMap = new HashMap<>();
-        final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
-        substatementsInit.addAll(ctx.declaredSubstatements());
-        substatementsInit.addAll(ctx.effectiveSubstatements());
+        final Iterable<StatementContextBase<?, ?, ?>> substatementsInit = Iterables.concat(
+            ctx.declaredSubstatements(), ctx.effectiveSubstatements());
 
+        final Map<StatementDefinition, Integer> stmtDefMap = new HashMap<>();
         for (StatementContextBase<?, ?, ?> stmtCtx : substatementsInit) {
             final StatementDefinition definition = stmtCtx.getPublicDefinition();
             if (!stmtDefMap.containsKey(definition)) {
@@ -79,49 +77,47 @@ public final class SubstatementValidator {
         }
 
         if (stmtDefMap.isEmpty() && specialCase == SpecialCase.NOTNULL) {
-            throw new InvalidSubstatementException(String.format("%s must contain atleast 1 element. Error in module "
-                    + "%s (%s)", currentStatement, ctx.getRoot().getStatementArgument(),
-                    ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
-                    ctx.getStatementSourceReference());
+            throw new InvalidSubstatementException(ctx.getStatementSourceReference(),
+                "%s must contain atleast 1 element. Error in module %s (%s)", currentStatement,
+                ctx.getRoot().getStatementArgument(),
+                ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot()));
         }
 
-        for (Map.Entry entry : stmtDefMap.entrySet()) {
+        final Map<StatementDefinition, Integer> validatedMap = new HashMap<>();
+        for (Entry<?, ?> entry : stmtDefMap.entrySet()) {
             final StatementDefinition key = (StatementDefinition) entry.getKey();
             if (!cardinalityMap.containsKey(key)) {
                 if (ctx.getFromNamespace(ExtensionNamespace.class, key.getStatementName()) != null) {
                     continue;
                 }
-                throw new InvalidSubstatementException(String.format("%s is not valid for %s. Error in module %s (%s)",
-                        key, currentStatement, ctx.getRoot().getStatementArgument(),
-                        ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
-                        ctx.getStatementSourceReference());
+                throw new InvalidSubstatementException(ctx.getStatementSourceReference(),
+                    "%s is not valid for %s. Error in module %s (%s)", key, currentStatement,
+                    ctx.getRoot().getStatementArgument(),
+                    ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot()));
             }
             if (cardinalityMap.get(key).getMin() > (Integer) entry.getValue()) {
-                throw new InvalidSubstatementException(String.format("Minimal count of %s for %s is %s, detected %s. "
-                        + "Error in module %s (%s)", key, currentStatement, cardinalityMap.get(key).getMin(),
-                        entry.getValue(), ctx.getRoot().getStatementArgument(),
-                        ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
-                        ctx.getStatementSourceReference());
+                throw new InvalidSubstatementException(ctx.getStatementSourceReference(),
+                    "Minimal count of %s for %s is %s, detected %s. Error in module %s (%s)", key, currentStatement,
+                    cardinalityMap.get(key).getMin(), entry.getValue(), ctx.getRoot().getStatementArgument(),
+                    ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot()));
             }
             if (cardinalityMap.get(key).getMax() < (Integer) entry.getValue()) {
-                throw new InvalidSubstatementException(String.format("Maximal count of %s for %s is %s, detected %s. "
-                        + "Error in module %s (%s)", key, currentStatement, cardinalityMap.get(key).getMax(),
-                        entry.getValue(), ctx.getRoot().getStatementArgument(),
-                        ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
-                        ctx.getStatementSourceReference());
+                throw new InvalidSubstatementException(ctx.getStatementSourceReference(),
+                    "Maximal count of %s for %s is %s, detected %s. Error in module %s (%s)", key, currentStatement,
+                    cardinalityMap.get(key).getMax(), entry.getValue(), ctx.getRoot().getStatementArgument(),
+                    ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot()));
             }
             validatedMap.put(key, 1);
         }
 
         final MapDifference<StatementDefinition, Object> diff = Maps.difference(validatedMap, cardinalityMap);
-
-        for (Map.Entry entry : diff.entriesOnlyOnRight().entrySet()) {
+        for (Entry<?, ?> entry : diff.entriesOnlyOnRight().entrySet()) {
             final int min = ((Cardinality) entry.getValue()).getMin();
             if (min > 0) {
-                throw new MissingSubstatementException(String.format("%s is missing %s. Minimal count is %s. Error in"
-                        + "  module %s (%s)", currentStatement, entry.getKey(), min, ctx.getRoot()
-                        .getStatementArgument(), ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot())),
-                        ctx.getStatementSourceReference());
+                throw new MissingSubstatementException(ctx.getStatementSourceReference(),
+                    "%s is missing %s. Minimal count is %s. Error in module %s (%s)", currentStatement, entry.getKey(),
+                    min, ctx.getRoot().getStatementArgument(),
+                    ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.getRoot()));
             }
         }
     }
@@ -130,13 +126,9 @@ public final class SubstatementValidator {
         private final int min;
         private final int max;
 
-        private Cardinality(int min, int max) throws YangParseException {
-            if (min > max) {
-                throw new IllegalArgumentException("Min can not be greater than max!");
-            }
-            if (min < 0) {
-                throw new IllegalArgumentException("Min can not be les than 0!");
-            }
+        private Cardinality(final int min, final int max) {
+            Preconditions.checkArgument(min >= 0, "Min %s cannot be less than 0!");
+            Preconditions.checkArgument(min <= max, "Min %s can not be greater than max %s!", min, max);
             this.min = min;
             this.max = max;
         }