From bb9cc6b851572a3b756f65738034e546bd33e17d Mon Sep 17 00:00:00 2001 From: Munir Ahmad Date: Sat, 24 Feb 2018 12:42:55 -0500 Subject: Swap while loops with foreach where applicable Change-Id: Id3b54e8192f0e60657aa4c50af56c1ef98fbff5e Issue-ID: SO-437 Signed-off-by: Munir Ahmad --- .../mso/openstack/utils/MsoYamlEditorWithEnvt.java | 108 ++++++++++----------- 1 file changed, 53 insertions(+), 55 deletions(-) (limited to 'adapters') diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoYamlEditorWithEnvt.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoYamlEditorWithEnvt.java index c227d72f0c..8002087ddd 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoYamlEditorWithEnvt.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoYamlEditorWithEnvt.java @@ -27,6 +27,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.Map.Entry; @@ -73,33 +74,32 @@ public class MsoYamlEditorWithEnvt { if (resourceMap == null) { return paramSet; } - Iterator > it = resourceMap.entrySet().iterator(); - - while (it.hasNext()) { - MsoHeatEnvironmentParameter hep = new MsoHeatEnvironmentParameter(); - Map.Entry pair = it.next(); - String value; - Object obj = pair.getValue(); - if (obj instanceof java.lang.String) { - value = yaml.dump(obj); - // but this adds an extra '\n' at the end - which won't hurt - but we don't need it - value = value.substring(0, value.length() - 1); - } else if (obj instanceof java.util.LinkedHashMap) { - //Handle that it's json - try { - value = JSON_MAPPER.writeValueAsString(obj); - } catch (Exception e) { - LOGGER.debug("Exception:", e); - value = "_BAD_JSON_MAPPING"; - } - } else { - //this handles integers/longs/floats/etc. - value = String.valueOf(obj); - } - hep.setName((String) pair.getKey()); - hep.setValue(value); - paramSet.add(hep); - } + + for (Entry stringObjectEntry : resourceMap.entrySet()) { + MsoHeatEnvironmentParameter hep = new MsoHeatEnvironmentParameter(); + Entry pair = stringObjectEntry; + String value; + Object obj = pair.getValue(); + if (obj instanceof String) { + value = yaml.dump(obj); + // but this adds an extra '\n' at the end - which won't hurt - but we don't need it + value = value.substring(0, value.length() - 1); + } else if (obj instanceof LinkedHashMap) { + //Handle that it's json + try { + value = JSON_MAPPER.writeValueAsString(obj); + } catch (Exception e) { + LOGGER.debug("Exception:", e); + value = "_BAD_JSON_MAPPING"; + } + } else { + //this handles integers/longs/floats/etc. + value = String.valueOf(obj); + } + hep.setName((String) pair.getKey()); + hep.setValue(value); + paramSet.add(hep); + } return paramSet; } public synchronized Set getResourceListFromEnvt() { @@ -107,15 +107,14 @@ public class MsoYamlEditorWithEnvt { Set resourceList = new HashSet<>(); @SuppressWarnings("unchecked") Map resourceMap = (Map) yml.get("resource_registry"); - Iterator> it = resourceMap.entrySet().iterator(); - - while (it.hasNext()) { - MsoHeatEnvironmentResource her = new MsoHeatEnvironmentResource(); - Map.Entry pair = it.next(); - her.setName((String) pair.getKey()); - her.setValue((String) pair.getValue()); - resourceList.add(her); - } + + for (Entry stringObjectEntry : resourceMap.entrySet()) { + MsoHeatEnvironmentResource her = new MsoHeatEnvironmentResource(); + Entry pair = stringObjectEntry; + her.setName((String) pair.getKey()); + her.setValue((String) pair.getValue()); + resourceList.add(her); + } return resourceList; } catch (Exception e) { LOGGER.debug("Exception:", e); @@ -126,35 +125,34 @@ public class MsoYamlEditorWithEnvt { Set paramSet = new HashSet <> (); @SuppressWarnings("unchecked") Map resourceMap = (Map ) yml.get ("parameters"); - Iterator > it = resourceMap.entrySet ().iterator (); - while (it.hasNext ()) { - HeatTemplateParam param = new HeatTemplateParam (); - Map.Entry pair = it.next (); + for (Entry stringObjectEntry : resourceMap.entrySet()) { + HeatTemplateParam param = new HeatTemplateParam(); + Entry pair = stringObjectEntry; @SuppressWarnings("unchecked") - Map resourceEntry = (Map ) pair.getValue (); + Map resourceEntry = (Map) pair.getValue(); String value = null; try { - value = resourceEntry.get ("default"); - } catch (java.lang.ClassCastException cce) { + value = resourceEntry.get("default"); + } catch (ClassCastException cce) { LOGGER.debug("Exception:", cce); - // This exception only - the value is an integer. For what we're doing - // here - we don't care - so set value to something - and it will - // get marked as not being required - which is correct. - //System.out.println("cce exception!"); - value = "300"; - // okay + // This exception only - the value is an integer. For what we're doing + // here - we don't care - so set value to something - and it will + // get marked as not being required - which is correct. + //System.out.println("cce exception!"); + value = "300"; + // okay } - param.setParamName ((String) pair.getKey ()); + param.setParamName((String) pair.getKey()); if (value != null) { - param.setRequired (false); + param.setRequired(false); } else { - param.setRequired (true); + param.setRequired(true); } - value = resourceEntry.get ("type"); - param.setParamType (value); + value = resourceEntry.get("type"); + param.setParamType(value); - paramSet.add (param); + paramSet.add(param); } return paramSet; -- cgit 1.2.3-korg