From 98799b7c586dee5236d885078f6b0a9990a80713 Mon Sep 17 00:00:00 2001 From: sharath reddy Date: Mon, 28 Mar 2022 20:03:30 +0530 Subject: Reduced code complexity Issue-ID: CLI-439 Signed-off-by: sharath reddy Change-Id: I9b7e6838d4f856cfeeafd3f71825ceb534bed43e Signed-off-by: sharath reddy --- .../cli/fw/schema/OnapCommandSchemaMerger.java | 91 ++++++++++++---------- 1 file changed, 49 insertions(+), 42 deletions(-) (limited to 'framework/src') diff --git a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java index 0712603a..7e0d40ee 100644 --- a/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java +++ b/framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java @@ -51,6 +51,29 @@ public class OnapCommandSchemaMerger { } + public static void mergeWithExistingValue(Object yamlValue, Object existingValue, String key, Map mergedResult){ + if (yamlValue instanceof Map) { + if (existingValue instanceof Map) { + mergeYamlMap((Map) existingValue, (Map) yamlValue); + } else if (existingValue instanceof String) { + throw new IllegalArgumentException("Cannot merge complex element into a simple element: "+key); + } else { + throw unknownValueType(key, yamlValue); + } + } else if (yamlValue instanceof List) { + mergeYamlLists(mergedResult, key, yamlValue); + + } else if (yamlValue instanceof String + || yamlValue instanceof Boolean + || yamlValue instanceof Double + || yamlValue instanceof Integer) { + mergedResult.put(key, yamlValue); + + } else { + throw unknownValueType(key, yamlValue); + } + } + public static void mergeYamlMap(Map mergedResult, Map yamlContents) { if (yamlContents == null) return; @@ -64,27 +87,7 @@ public class OnapCommandSchemaMerger { Object existingValue = mergedResult.get(key); if (existingValue != null) { - if (yamlValue instanceof Map) { - if (existingValue instanceof Map) { - mergeYamlMap((Map) existingValue, (Map) yamlValue); - } else if (existingValue instanceof String) { - throw new IllegalArgumentException("Cannot merge complex element into a simple element: "+key); - } else { - throw unknownValueType(key, yamlValue); - } - } else if (yamlValue instanceof List) { - mergeYamlLists(mergedResult, key, yamlValue); - - } else if (yamlValue instanceof String - || yamlValue instanceof Boolean - || yamlValue instanceof Double - || yamlValue instanceof Integer) { - mergedResult.put(key, yamlValue); - - } else { - throw unknownValueType(key, yamlValue); - } - + mergeWithExistingValue(yamlValue, existingValue, key, mergedResult); } else { if (yamlValue instanceof Map || yamlValue instanceof List @@ -105,6 +108,30 @@ public class OnapCommandSchemaMerger { return new IllegalArgumentException(msg); } + private static void compareWithExistingNames(String nameN, List originalList, Map oN, Object o){ + if (nameN != null) { + + boolean existing = false; + for (Object e: originalList) { + Map oE = (Map) e; + String nameE = (String)oE.getOrDefault(OnapCommandConstants.NAME, null); + + //Name should be existing in the map, otherwise continue as don't know how to compare + if (nameN.equals(nameE)) { + for (Entry oNe : oN.entrySet()) { + oE.put(oNe.getKey(), oNe.getValue()); + } + existing = true; + break; + } + } + + if (!existing) { + originalList.add(o); + } + } + } + @SuppressWarnings("unchecked") private static void mergeYamlLists(Map mergedResult, String key, Object yamlValue) { if (! (yamlValue instanceof List && mergedResult.get(key) instanceof List)) { @@ -118,27 +145,7 @@ public class OnapCommandSchemaMerger { String nameN = (String)oN.getOrDefault(OnapCommandConstants.NAME, null); //Name should be existing in the map, otherwise continue as don't know how to compare - if (nameN != null) { - - boolean existing = false; - for (Object e: originalList) { - Map oE = (Map) e; - String nameE = (String)oE.getOrDefault(OnapCommandConstants.NAME, null); - - //Name should be existing in the map, otherwise continue as don't know how to compare - if (nameN.equals(nameE)) { - for (Entry oNe : oN.entrySet()) { - oE.put(oNe.getKey(), oNe.getValue()); - } - existing = true; - break; - } - } - - if (!existing) { - originalList.add(o); - } - } + compareWithExistingNames(nameN, originalList, oN, o); } } } -- cgit 1.2.3-korg