Merge "Remove unused imports"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / BasicValidations.java
index 46f4f8ebf5fb3128d2da9f98da9daf55260ec920..0cdf493e75614c17666404b1059362ae66b85da1 100644 (file)
@@ -7,9 +7,13 @@
  */
 package org.opendaylight.yangtools.yang.parser.impl;
 
+import static java.lang.String.format;
+
+import com.google.common.base.Splitter;
+import com.google.common.collect.Sets;
+
 import java.text.ParseException;
 import java.util.Date;
-import java.util.List;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -20,14 +24,15 @@ import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtC
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
 
-import com.google.common.collect.Sets;
-
 /**
  * Reusable checks of basic constraints on yang statements
  */
 final class BasicValidations {
 
     static final String SUPPORTED_YANG_VERSION = "1";
+    private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
+    private static final Pattern identifierPattern = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
+    private static final Pattern prefixedIdentifierPattern = Pattern.compile("(.+):(.+)");
 
     /**
      * It isn't desirable to create instance of this class.
@@ -73,7 +78,7 @@ final class BasicValidations {
      * Check if only one module or submodule is present in session(one yang
      * file)
      */
-    static void checkOnlyOneModulePresent(final String moduleName, final String globalId) {
+    static void checkIsModuleIdNull(final String globalId) {
         if (globalId != null) {
             ValidationUtil.ex(ValidationUtil.f("Multiple (sub)modules per file"));
         }
@@ -99,8 +104,6 @@ final class BasicValidations {
         }
     }
 
-    private static final Pattern identifierPattern = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
-
     static void checkIdentifier(final ParseTree statement) {
         checkIdentifierInternal(statement, ValidationUtil.getName(statement));
     }
@@ -121,8 +124,6 @@ final class BasicValidations {
         }
     }
 
-    private static Pattern prefixedIdentifierPattern = Pattern.compile("(.+):(.+)");
-
     static void checkPrefixedIdentifier(final ParseTree statement) {
         checkPrefixedIdentifierInternal(statement, ValidationUtil.getName(statement));
     }
@@ -149,10 +150,7 @@ final class BasicValidations {
         String id = ValidationUtil.getName(statement);
 
         try {
-            for (String oneOfId : id.split("/")) {
-                if (oneOfId.isEmpty()) {
-                    continue;
-                }
+            for (String oneOfId : SLASH_SPLITTER.split(id)) {
                 checkPrefixedIdentifierInternal(statement, oneOfId);
             }
         } catch (YangValidationException e) {
@@ -165,7 +163,7 @@ final class BasicValidations {
         String getMessage();
     }
 
-    static void checkPresentChildOfTypeInternal(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildType,
+    private static void checkPresentChildOfTypeInternal(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildType,
             final MessageProvider message, final boolean atMostOne) {
         if (!checkPresentChildOfTypeSafe(parent, expectedChildType, atMostOne)) {
             String str = atMostOne ? "(Expected exactly one statement) " + message.getMessage() : message.getMessage();
@@ -177,7 +175,7 @@ final class BasicValidations {
             final boolean atMostOne) {
 
         // Construct message in checkPresentChildOfTypeInternal only if
-        // validaiton fails, not in advance
+        // validation fails, not in advance
         MessageProvider message = new MessageProvider() {
 
             @Override
@@ -187,8 +185,7 @@ final class BasicValidations {
                         ValidationUtil.getSimpleStatementName(parent.getClass()), ValidationUtil.getName(parent));
 
                 String root = ValidationUtil.getRootParentName(parent);
-                message = parent.equals(ValidationUtil.getRootParentName(parent)) ? message : ValidationUtil.f(
-                        "(In (sub)module:%s) %s", root, message);
+                message = format("(In (sub)module:%s) %s", root, message);
                 return message;
             }
         };
@@ -218,52 +215,50 @@ final class BasicValidations {
         @Override
         public String getMessage() {
             StringBuilder childTypes = new StringBuilder();
-            String orStr = " OR ";
             for (Class<? extends ParseTree> type : expectedChildTypes) {
                 childTypes.append(ValidationUtil.getSimpleStatementName(type));
-                childTypes.append(orStr);
+                childTypes.append(" OR ");
             }
             String message = ValidationUtil.f("Missing %s statement in %s:%s", childTypes.toString(),
                     ValidationUtil.getSimpleStatementName(parent.getClass()), ValidationUtil.getName(parent));
 
             String root = ValidationUtil.getRootParentName(parent);
-            message = parent.equals(ValidationUtil.getRootParentName(parent)) ? message : ValidationUtil.f(
-                    "(In (sub)module:%s) %s", root, message);
+            message = format("(In (sub)module:%s) %s", root, message);
             return message;
         }
-    };
+    }
 
     static void checkPresentChildOfTypes(final ParseTree parent,
             final Set<Class<? extends ParseTree>> expectedChildTypes, final boolean atMostOne) {
 
         // Construct message in checkPresentChildOfTypeInternal only if
-        // validaiton fails, not in advance
+        // validation fails, not in advance
         MessageProvider message = new MessageProviderForSetOfChildTypes(expectedChildTypes, parent);
         checkPresentChildOfTypeInternal(parent, expectedChildTypes, message, atMostOne);
     }
 
-    static boolean checkPresentChildOfTypeSafe(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildType,
+    private static boolean checkPresentChildOfTypeSafe(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildType,
             final boolean atMostOne) {
 
         int foundChildrenOfType = ValidationUtil.countPresentChildrenOfType(parent, expectedChildType);
 
-        return atMostOne ? foundChildrenOfType == 1 ? true : false : foundChildrenOfType != 0 ? true : false;
+        return atMostOne ? foundChildrenOfType == 1 : foundChildrenOfType != 0;
     }
 
-    static boolean checkPresentChildOfTypeSafe(final ParseTree parent, final Class<? extends ParseTree> expectedChildType,
+    private static boolean checkPresentChildOfTypeSafe(final ParseTree parent, final Class<? extends ParseTree> expectedChildType,
             final boolean atMostOne) {
 
         int foundChildrenOfType = ValidationUtil.countPresentChildrenOfType(parent, expectedChildType);
 
-        return atMostOne ? foundChildrenOfType == 1 ? true : false : foundChildrenOfType != 0 ? true : false;
+        return atMostOne ? foundChildrenOfType == 1 : foundChildrenOfType != 0;
     }
 
-    static List<String> getAndCheckUniqueKeys(final ParseTree ctx) {
+    static Iterable<String> getAndCheckUniqueKeys(final ParseTree ctx) {
         String key = ValidationUtil.getName(ctx);
         ParseTree parent = ctx.getParent();
         String rootParentName = ValidationUtil.getRootParentName(ctx);
 
-        List<String> keyList = ValidationUtil.listKeysFromId(key);
+        Iterable<String> keyList = ValidationUtil.listKeysFromId(key);
         Set<String> duplicates = ValidationUtil.getDuplicates(keyList);
 
         if (duplicates.size() != 0) {