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 ++++++++++----------- .../org/openecomp/mso/asdc/util/YamlEditor.java | 33 +++---- .../mso/client/dmaap/DmaapPropertiesLoader.java | 5 +- .../mso/bpmn/core/domain/ServiceDecomposition.java | 37 +++---- 4 files changed, 84 insertions(+), 99 deletions(-) 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; diff --git a/asdc-controller/src/main/java/org/openecomp/mso/asdc/util/YamlEditor.java b/asdc-controller/src/main/java/org/openecomp/mso/asdc/util/YamlEditor.java index f94e0a153b..a90fdeb8e6 100644 --- a/asdc-controller/src/main/java/org/openecomp/mso/asdc/util/YamlEditor.java +++ b/asdc-controller/src/main/java/org/openecomp/mso/asdc/util/YamlEditor.java @@ -82,12 +82,10 @@ public class YamlEditor { @SuppressWarnings("unchecked") Map resourceMap = (Map ) yml.get ("resources"); - Iterator > it = resourceMap.entrySet ().iterator (); - while (it.hasNext ()) { - Map.Entry pair = it.next (); + for (Entry pair : resourceMap.entrySet()) { @SuppressWarnings("unchecked") - Map resourceEntry = (Map ) pair.getValue (); - typeList.add (resourceEntry.get ("type")); + Map resourceEntry = (Map) pair.getValue(); + typeList.add(resourceEntry.get("type")); } return typeList; } @@ -100,28 +98,27 @@ public class YamlEditor { 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 (); - - param.setParamName (pair.getKey ()); + Map resourceEntry = (Map) pair.getValue(); + + param.setParamName(pair.getKey()); // System.out.println(pair.getKey()+":"+type); if (resourceEntry.containsKey("default")) { - param.setRequired (false); + param.setRequired(false); } else { - param.setRequired (true); + param.setRequired(true); } // Now set the type - String value = resourceEntry.get ("type"); - param.setParamType (value); - + String value = resourceEntry.get("type"); + param.setParamType(value); + param.setHeatTemplateArtifactUuid(artifactUUID); - paramSet.add (param); + paramSet.add(param); } return paramSet; diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/dmaap/DmaapPropertiesLoader.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/dmaap/DmaapPropertiesLoader.java index a21dbe8477..1bdecbf2a7 100644 --- a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/dmaap/DmaapPropertiesLoader.java +++ b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/dmaap/DmaapPropertiesLoader.java @@ -39,9 +39,8 @@ public class DmaapPropertiesLoader { } public DmaapProperties getImpl() { - Iterator propertyImpls = services.iterator(); - while (propertyImpls.hasNext()) { - return propertyImpls.next(); + for (DmaapProperties service : services) { + return service; } return null; diff --git a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java index 0e985cc8f1..15a1cee5c0 100644 --- a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java +++ b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java @@ -186,15 +186,13 @@ public class ServiceDecomposition extends JsonWrapper implements Serializable { @JsonIgnore public String getVnfResource(String resourceObjectId) { - Iterator iter = getServiceResources().iterator(); - while (iter.hasNext()) { - Resource resource = iter.next(); - //resource.getModelInfo().getModelInvariantId(); + for (Resource resource : getServiceResources()) { + //resource.getModelInfo().getModelInvariantId(); - if ("extracted information".equals(resourceObjectId)){ - return resource.toJsonString(); - } - } + if ("extracted information".equals(resourceObjectId)) { + return resource.toJsonString(); + } + } return ""; } @@ -290,13 +288,11 @@ public class ServiceDecomposition extends JsonWrapper implements Serializable { public boolean replaceResource(Resource newResource){ boolean result = false; List serviceResources = getServiceResources(); - Iterator iter = serviceResources.iterator(); - while (iter.hasNext()) { - Resource resource = iter.next(); + for (Resource resource : (Iterable) serviceResources) { System.out.println("resource found"); - if (resource.resourceType == newResource.resourceType){ + if (resource.resourceType == newResource.resourceType) { System.out.println("resource type matches"); - if (resource.getResourceId().equalsIgnoreCase(newResource.getResourceId())){ + if (resource.getResourceId().equalsIgnoreCase(newResource.getResourceId())) { System.out.println("resource id matches"); //returns TRUE if replacement is a success result = Collections.replaceAll(serviceResources, resource, newResource); @@ -326,12 +322,9 @@ public class ServiceDecomposition extends JsonWrapper implements Serializable { */ public boolean deleteResource(Resource resource){ List serviceResources = getServiceResources(); - Iterator iter = serviceResources.iterator(); - while (iter.hasNext()) { - Resource item = iter.next(); - - if (item.resourceType == resource.resourceType){ - if (item.getResourceId().equalsIgnoreCase(resource.getResourceId())){ + for (Resource item : (Iterable) serviceResources) { + if (item.resourceType == resource.resourceType) { + if (item.getResourceId().equalsIgnoreCase(resource.getResourceId())) { //returns TRUE if replacement is a success return serviceResources.remove(resource); } @@ -377,10 +370,8 @@ public class ServiceDecomposition extends JsonWrapper implements Serializable { @JsonIgnore public Resource getServiceResource(String resourceId){ List resources = getServiceResources(); - Iterator iter = resources.iterator(); - while (iter.hasNext()){ - Resource resource = iter.next(); - if (resource.getResourceId().equalsIgnoreCase(resourceId)){ + for (Resource resource : resources) { + if (resource.getResourceId().equalsIgnoreCase(resourceId)) { //match return resource; } -- cgit 1.2.3-korg