diff options
author | Kanagaraj Manickam <kanagaraj.manickam@huawei.com> | 2022-03-31 04:09:27 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2022-03-31 04:09:27 +0000 |
commit | b2aeb4fae1c300d0073b8291e7a5f11bc53d3043 (patch) | |
tree | e2c605bb29676b1d68186e5e877476afbe88a2e2 | |
parent | b5c3467d488875f73bd28b77b1c531c0f39e1678 (diff) | |
parent | 98799b7c586dee5236d885078f6b0a9990a80713 (diff) |
Merge "Reduced code complexity"
-rw-r--r-- | framework/src/main/java/org/onap/cli/fw/schema/OnapCommandSchemaMerger.java | 91 |
1 files changed, 49 insertions, 42 deletions
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<String, Object> mergedResult){ + if (yamlValue instanceof Map) { + if (existingValue instanceof Map) { + mergeYamlMap((Map<String, Object>) existingValue, (Map<String, Object>) 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<String, Object> mergedResult, Map<String, Object> 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<String, Object>) existingValue, (Map<String, Object>) 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<Object> originalList, Map<String, Object> oN, Object o){ + if (nameN != null) { + + boolean existing = false; + for (Object e: originalList) { + Map<String, Object> 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<String, Object> oNe : oN.entrySet()) { + oE.put(oNe.getKey(), oNe.getValue()); + } + existing = true; + break; + } + } + + if (!existing) { + originalList.add(o); + } + } + } + @SuppressWarnings("unchecked") private static void mergeYamlLists(Map<String, Object> 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<String, Object> 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<String, Object> oNe : oN.entrySet()) { - oE.put(oNe.getKey(), oNe.getValue()); - } - existing = true; - break; - } - } - - if (!existing) { - originalList.add(o); - } - } + compareWithExistingNames(nameN, originalList, oN, o); } } } |