Merge pull request #186 from sdutry/sonar-entrySet

changed Map iterations to use entrySet when both key and value are used
This commit is contained in:
Stefaan Dutry
2017-12-01 20:46:00 +01:00
committed by GitHub
18 changed files with 73 additions and 62 deletions
@@ -154,15 +154,15 @@ public abstract class AbstractMatcher<E> implements Serializable {
Map<String, String> map = new LinkedHashMap<>();
//this will set the group index references, like {1}
for (String key : orig.keySet()) {
map.put(key, convertParam(orig.get(key), vars));
for (Map.Entry<String,String> entry : orig.entrySet()) {
map.put(entry.getKey(), convertParam(entry.getValue(), vars));
}
//the values map will contain entries like name->"Lex Luthor" and 1->"Lex Luthor"
//now add the non-numeric values
for (String key: vars.keySet()) {
if (!NumberUtils.isNumber(key)) {
map.put(key, vars.get(key));
for (Map.Entry<String,String> entry: vars.entrySet()) {
if (!NumberUtils.isNumber(entry.getKey())) {
map.put(entry.getKey(), entry.getValue());
}
}
@@ -59,8 +59,8 @@ public class ActionConfigMatcher extends AbstractMatcher<ActionConfig> implement
Map<String, ActionConfig> configs,
boolean looseMatch) {
super(patternMatcher);
for (String name : configs.keySet()) {
addPattern(name, configs.get(name), looseMatch);
for (Map.Entry<String, ActionConfig> entry : configs.entrySet()) {
addPattern(entry.getKey(), entry.getValue(), looseMatch);
}
}
@@ -419,8 +419,8 @@ public class DefaultConfiguration implements Configuration {
this.namespaceActionConfigMatchers = new LinkedHashMap<>();
this.namespaceMatcher = new NamespaceMatcher(matcher, namespaceActionConfigs.keySet());
for (String ns : namespaceActionConfigs.keySet()) {
namespaceActionConfigMatchers.put(ns, new ActionConfigMatcher(matcher, namespaceActionConfigs.get(ns), true));
for (Map.Entry<String, Map<String, ActionConfig>> entry : namespaceActionConfigs.entrySet()) {
namespaceActionConfigMatchers.put(entry.getKey(), new ActionConfigMatcher(matcher, entry.getValue(), true));
}
}
@@ -496,11 +496,11 @@ public class DefaultConfiguration implements Configuration {
public String toString() {
StringBuilder buff = new StringBuilder("RuntimeConfiguration - actions are\n");
for (String namespace : namespaceActionConfigs.keySet()) {
Map<String, ActionConfig> actionConfigs = namespaceActionConfigs.get(namespace);
for (Map.Entry<String, Map<String, ActionConfig>> entry : namespaceActionConfigs.entrySet()) {
Map<String, ActionConfig> actionConfigs = entry.getValue();
for (String s : actionConfigs.keySet()) {
buff.append(namespace).append("/").append(s).append("\n");
buff.append(entry.getKey()).append("/").append(s).append("\n");
}
}
@@ -130,8 +130,8 @@ public class InterceptorBuilder {
* interceptorStack1 -> [interceptor1.param1 -> someValue, interceptor1.param2 -> anotherValue]
*
*/
for (String key : refParams.keySet()) {
String value = refParams.get(key);
for (Map.Entry<String, String> entry : refParams.entrySet()) {
String key = entry.getKey();
try {
String name = key.substring(0, key.indexOf('.'));
@@ -144,7 +144,7 @@ public class InterceptorBuilder {
map = new LinkedHashMap<>();
}
map.put(key, value);
map.put(key, entry.getValue());
params.put(name, map);
} catch (Exception e) {
@@ -154,9 +154,9 @@ public class InterceptorBuilder {
result = new ArrayList<>(stackConfig.getInterceptors());
for (String key : params.keySet()) {
Map<String, String> map = params.get(key);
for (Map.Entry<String, Map<String, String>> entry : params.entrySet()) {
String key = entry.getKey();
Map<String, String> map = entry.getValue();
Object interceptorCfgObj = interceptorLocator.getInterceptorConfig(key);
@@ -412,8 +412,8 @@ public class XWorkConverter extends DefaultTypeConverter {
Object converter = mapping.get(property);
if (converter == null && LOG.isDebugEnabled()) {
LOG.debug("Converter is null for property [{}]. Mapping size [{}]:", property, mapping.size());
for (String next : mapping.keySet()) {
LOG.debug("{}:{}", next, mapping.get(next));
for (Map.Entry<String, Object> entry : mapping.entrySet()) {
LOG.debug("{}:{}", entry.getKey(), entry.getValue());
}
}
return converter;
@@ -118,11 +118,13 @@ public class ParameterFilterInterceptor extends AbstractInterceptor {
for (String param : parameters.keySet()) {
boolean currentAllowed = !isDefaultBlock();
for (String currRule : includesExcludesMap.keySet()) {
for (Map.Entry<String, Boolean> entry : includesExcludesMap.entrySet()) {
String currRule = entry.getKey();
if (param.startsWith(currRule) &&
(param.length() == currRule.length() || isPropertySeparator(param.charAt(currRule.length())))
) {
currentAllowed = includesExcludesMap.get(currRule);
currentAllowed = entry.getValue();
}
}
if (!currentAllowed) {
@@ -172,10 +172,11 @@ public class ParametersInterceptor extends MethodFilterInterceptor {
acceptableParameters = new TreeMap<>();
}
for (String name : params.keySet()) {
Parameter parameter = params.get(name);
if (isAcceptableParameter(name, action)) {
acceptableParameters.put(name, parameter);
for (Map.Entry<String, Parameter> entry : params.entrySet()) {
String parameterName = entry.getKey();
if (isAcceptableParameter(parameterName, action)) {
acceptableParameters.put(parameterName, entry.getValue());
}
}
@@ -270,10 +271,10 @@ public class ParametersInterceptor extends MethodFilterInterceptor {
}
StringBuilder logEntry = new StringBuilder();
for (String name : parameters.keySet()) {
logEntry.append(String.valueOf(name));
for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
logEntry.append(entry.getKey());
logEntry.append(" => ");
logEntry.append(parameters.get(name).getValue());
logEntry.append(entry.getValue().getValue());
logEntry.append(" ");
}
@@ -1241,9 +1241,11 @@ public abstract class UIBean extends Component {
}
public void setDynamicAttributes(Map<String, Object> tagDynamicAttributes) {
for (String key : tagDynamicAttributes.keySet()) {
for (Map.Entry<String, Object> entry : tagDynamicAttributes.entrySet()) {
String key = entry.getKey();
if (!isValidTagAttribute(key)) {
dynamicAttributes.put(key, tagDynamicAttributes.get(key));
dynamicAttributes.put(key, entry.getValue());
}
}
}
@@ -60,8 +60,8 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
*/
public void cleanUp() {
LOG.debug("Performing File Upload temporary storage cleanup.");
for (String fieldName : fileInfos.keySet()) {
for (FileInfo fileInfo : fileInfos.get(fieldName)) {
for (List<FileInfo> fileInfoList : fileInfos.values()) {
for (FileInfo fileInfo : fileInfoList) {
File file = fileInfo.getFile();
LOG.debug("Deleting file '{}'.", file.getName());
if (!file.delete()) {
@@ -26,7 +26,9 @@ import org.apache.struts2.dispatcher.Parameter;
import org.apache.struts2.dispatcher.HttpParameters;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* <!-- START SNIPPET: description -->
@@ -60,14 +62,16 @@ public class CheckboxInterceptor extends AbstractInterceptor {
HttpParameters parameters = ai.getInvocationContext().getParameters();
Map<String, Parameter> extraParams = new HashMap<>();
for (String name : parameters.keySet()) {
Set<String> checkboxParameters = new HashSet<>();
for (Map.Entry<String, Parameter> parameter : parameters.entrySet()) {
String name = parameter.getKey();
if (name.startsWith("__checkbox_")) {
String checkboxName = name.substring("__checkbox_".length());
Parameter value = parameters.get(name);
parameters = parameters.remove(name);
Parameter value = parameter.getValue();
checkboxParameters.add(name);
if (value.isMultiple()) {
LOG.debug("Bypassing automatic checkbox detection due to multiple checkboxes of the same name: {}", name);
LOG.debug("Bypassing automatic checkbox detection due to multiple checkboxes of the same name: {}", name);
continue;
}
@@ -78,7 +82,7 @@ public class CheckboxInterceptor extends AbstractInterceptor {
}
}
}
parameters.remove(checkboxParameters);
ai.getInvocationContext().getParameters().appendAll(extraParams);
@@ -215,8 +215,9 @@ public class PostbackResult extends StrutsResultSupport {
private void writeFormElements(HttpServletRequest request, PrintWriter pw) throws UnsupportedEncodingException {
Map<String, String[]> params = request.getParameterMap();
for (String name : params.keySet()) {
String[] values = params.get(name);
for (Map.Entry<String, String[]> entry : params.entrySet()) {
String name = entry.getKey();
String[] values = entry.getValue();
if (isElementIncluded(name, values)) {
writeFormElement(pw, name, values);
}
@@ -524,8 +524,8 @@ public class FreemarkerManager {
ScopesHashModel model = buildScopesHashModel(servletContext, request, response, wrapper, stack);
populateContext(model, stack, action, request, response);
if (tagLibraries != null) {
for (String prefix : tagLibraries.keySet()) {
model.put(prefix, tagLibraries.get(prefix).getModels(stack, request, response));
for (Map.Entry<String, TagLibraryModelProvider> entry : tagLibraries.entrySet()) {
model.put(entry.getKey(), entry.getValue().getModels(stack, request, response));
}
}
@@ -681,8 +681,7 @@ public class PackageBasedActionConfigBuilder implements ActionConfigBuilder {
&& actionAnnotation == null && actionsAnnotation == null
&& (alwaysMapExecute || map.isEmpty())) {
boolean found = false;
for (String method : map.keySet()) {
List<Action> actions = map.get(method);
for (List<Action> actions : map.values()) {
for (Action action : actions) {
// Check if there are duplicate action names in the annotations.
@@ -709,8 +708,9 @@ public class PackageBasedActionConfigBuilder implements ActionConfigBuilder {
}
// Build the actions for the annotations
for (String method : map.keySet()) {
List<Action> actions = map.get(method);
for (Map.Entry<String, List<Action>> entry : map.entrySet()) {
String method = entry.getKey();
List<Action> actions = entry.getValue();
for (Action action : actions) {
PackageConfig.Builder pkgCfg = defaultPackageConfig;
if (action.value().contains("/") && !slashesInActionNames) {
@@ -24,6 +24,7 @@ import org.apache.commons.lang3.StringEscapeUtils;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
/**
* Write tags as XHTML
@@ -53,11 +54,11 @@ public class XHTMLTagSerializer implements TagSerializer {
writer.write("<");
writer.write(name);
if (attrs != null) {
for (String key : attrs.keySet()) {
for (Map.Entry<String, String> entry : attrs.entrySet()) {
writer.write(" ");
writer.write(key);
writer.write(entry.getKey());
writer.write("=\"");
writer.write(attrs.get(key));
writer.write(entry.getValue());
writer.write("\"");
}
}
@@ -101,8 +101,7 @@ public abstract class JSONCleaner {
boolean allow = !isDefaultBlock();
if (includesExcludesMap != null) {
for (String currRule : includesExcludesMap.keySet()) {
Filter f = includesExcludesMap.get(currRule);
for (Filter f : includesExcludesMap.values()) {
if (f.pattern.matcher(ognl).matches()) {
allow = f.allow;
}
@@ -108,12 +108,13 @@ public abstract class BaseOsgiHost implements OsgiHost {
configProps.put(AutoProcessor.AUTO_START_PROP + ".2", StringUtils.join(bundles, " "));
}
} else {
for (String runLevel : runLevels.keySet()) {
if ("1".endsWith(runLevel)) {
for (Map.Entry<String, String> runLevel : runLevels.entrySet()) {
String runLevelKey = runLevel.getKey();
if ("1".endsWith(runLevelKey)) {
throw new StrutsException("Run level dirs must be greater than 1. Run level 1 is reserved for the Felix bundles");
}
List<String> bundles = getBundlesInDir(runLevels.get(runLevel));
configProps.put(AutoProcessor.AUTO_START_PROP + "." + runLevel, StringUtils.join(bundles, " "));
List<String> bundles = getBundlesInDir(runLevel.getValue());
configProps.put(AutoProcessor.AUTO_START_PROP + "." + runLevelKey, StringUtils.join(bundles, " "));
}
}
}
@@ -319,4 +320,4 @@ public abstract class BaseOsgiHost implements OsgiHost {
}
}
}
}
@@ -278,8 +278,9 @@ public class PortletUrlHelper {
Map<String, String[]> result = null;
if (params != null) {
result = new LinkedHashMap<String, String[]>(params.size());
for ( String key : params.keySet() ) {
Object val = params.get(key);
for (Map.Entry<String, Object> entry : params.entrySet() ) {
String key = entry.getKey();
Object val = entry.getValue();
if (val instanceof String[]) {
result.put(key, (String[]) val);
} else {
@@ -118,10 +118,9 @@ public class StrutsWildcardServletApplicationContext extends ServletApplicationC
Pattern pattern = WildcardUtil.compileWildcardPattern(path);
Map<String, URL> matches = finder.getResourcesMap("");
for (String resource : matches.keySet()) {
if (pattern.matcher(resource).matches()) {
URL url = matches.get(resource);
resources.add(new StrutsApplicationResource(url));
for (Map.Entry<String, URL> entry : matches.entrySet()) {
if (pattern.matcher(entry.getKey()).matches()) {
resources.add(new StrutsApplicationResource(entry.getValue()));
}
}