Fixing sonar issue 59/98359/2
authorDmitry Puzikov <d.puzikov2@partner.samsung.com>
Wed, 13 Nov 2019 14:14:52 +0000 (15:14 +0100)
committerOfir Sonsino <ofir.sonsino@intl.att.com>
Sun, 17 Nov 2019 14:17:26 +0000 (14:17 +0000)
Constant is used instead of repeated literal.

Change-Id: I540c09f8ba596398257b34ff3189b1a3ce5ab416
Issue-ID: SDC-2654
Signed-off-by: Dmitry Puzikov <d.puzikov2@partner.samsung.com>
common/onap-common-configuration-management/onap-configuration-management-core/src/main/java/org/onap/config/ConfigurationUtils.java

index 6adeb2d..32adf55 100644 (file)
@@ -89,6 +89,10 @@ public class ConfigurationUtils {
 
     private static final Map<Class, Class> ARRAY_CLASS_MAP;
 
+    private static final String CONFIG_REGEX_TPL_OPT_1 = "CONFIG(-\\w*){0,1}(-(%s|%s|%s)){0,1}\\.(%s|%s|%s|%s)$";
+
+    private static final String CONFIG_REGEX_TPL_OPT_2 = "CONFIG(.)*\\.(%s|%s|%s|%s)$";
+
     static {
         Map<Class, Class> arrayTypes = new HashMap<>();
         arrayTypes.put(Byte.class, Byte[].class);
@@ -148,12 +152,25 @@ public class ConfigurationUtils {
         file = file.toUpperCase().substring(file.lastIndexOf('!') + 1);
         file = file.substring(file.lastIndexOf('/') + 1);
         return file.matches(
-                "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE + "|"
-                        + ConfigurationMode.UNION + ")){0,1}" + "\\.(" + ConfigurationType.PROPERTIES.name() + "|"
-                        + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|"
-                        + ConfigurationType.YAML.name() + ")$") || file.matches(
-                "CONFIG(.)*\\.(" + ConfigurationType.PROPERTIES.name() + "|" + ConfigurationType.XML.name() + "|"
-                        + ConfigurationType.JSON.name() + "|" + ConfigurationType.YAML.name() + ")$");
+                String.format(
+                        CONFIG_REGEX_TPL_OPT_1,
+                        ConfigurationMode.OVERRIDE,
+                        ConfigurationMode.MERGE,
+                        ConfigurationMode.UNION,
+                        ConfigurationType.PROPERTIES.name(),
+                        ConfigurationType.XML.name(),
+                        ConfigurationType.JSON.name(),
+                        ConfigurationType.YAML.name()
+                )
+        ) || file.matches(
+                String.format(
+                        CONFIG_REGEX_TPL_OPT_2,
+                        ConfigurationType.PROPERTIES.name(),
+                        ConfigurationType.XML.name(),
+                        ConfigurationType.JSON.name(),
+                        ConfigurationType.YAML.name()
+                )
+        );
     }
 
     public static boolean isConfig(File file) {
@@ -188,12 +205,18 @@ public class ConfigurationUtils {
 
         file = file.toUpperCase().substring(file.lastIndexOf('!') + 1);
         file = file.substring(file.lastIndexOf('/') + 1);
-
         Pattern pattern = Pattern.compile(
-                "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE + "|"
-                        + ConfigurationMode.UNION + ")){0,1}" + "\\.(" + ConfigurationType.PROPERTIES.name() + "|"
-                        + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|"
-                        + ConfigurationType.YAML.name() + ")$");
+                String.format(
+                        CONFIG_REGEX_TPL_OPT_1,
+                        ConfigurationMode.OVERRIDE,
+                        ConfigurationMode.MERGE,
+                        ConfigurationMode.UNION,
+                        ConfigurationType.PROPERTIES.name(),
+                        ConfigurationType.XML.name(),
+                        ConfigurationType.JSON.name(),
+                        ConfigurationType.YAML.name()
+                )
+        );
         Matcher matcher = pattern.matcher(file);
         boolean b1 = matcher.matches();
         if (b1) {
@@ -571,10 +594,17 @@ public class ConfigurationUtils {
         file = file.toUpperCase().substring(file.lastIndexOf('!') + 1);
         file = file.substring(file.lastIndexOf('/') + 1);
         Pattern pattern = Pattern.compile(
-                "CONFIG(-\\w*){0,1}(-" + "(" + ConfigurationMode.OVERRIDE + "|" + ConfigurationMode.MERGE + "|"
-                        + ConfigurationMode.UNION + ")){0,1}" + "\\.(" + ConfigurationType.PROPERTIES.name() + "|"
-                        + ConfigurationType.XML.name() + "|" + ConfigurationType.JSON.name() + "|"
-                        + ConfigurationType.YAML.name() + ")$");
+                String.format(
+                        CONFIG_REGEX_TPL_OPT_1,
+                        ConfigurationMode.OVERRIDE,
+                        ConfigurationMode.MERGE,
+                        ConfigurationMode.UNION,
+                        ConfigurationType.PROPERTIES.name(),
+                        ConfigurationType.XML.name(),
+                        ConfigurationType.JSON.name(),
+                        ConfigurationType.YAML.name()
+                )
+        );
         Matcher matcher = pattern.matcher(file);
         boolean b1 = matcher.matches();
         if (b1) {