Skip to content

Polish: map entries should be iterated when both the key and value are needed #1752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions spring-core/src/main/java/org/springframework/core/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -214,13 +216,18 @@ public Set<String> getNamesForSuffix(@Nullable String nameSuffix) {
*/
public Set<Object> getValues(@Nullable String namePrefix) {
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : "");
Set<Object> values = new HashSet<>();
for (String code : this.fieldCache.keySet()) {
if (code.startsWith(prefixToUse)) {
values.add(this.fieldCache.get(code));
}
}
return values;
return getValues(code -> code.getKey().startsWith(prefixToUse));
}

/**
* Return all values of the given group of constants.
* @param predicate filter values by lambda function
* @return the set of values
*/
private Set<Object> getValues(Predicate<Map.Entry<String, Object>> predicate) {
return this.fieldCache.entrySet().stream().
filter(predicate).
map(Map.Entry::getValue).collect(Collectors.toSet());
}

/**
Expand All @@ -246,13 +253,7 @@ public Set<Object> getValuesForProperty(String propertyName) {
*/
public Set<Object> getValuesForSuffix(@Nullable String nameSuffix) {
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : "");
Set<Object> values = new HashSet<>();
for (String code : this.fieldCache.keySet()) {
if (code.endsWith(suffixToUse)) {
values.add(this.fieldCache.get(code));
}
}
return values;
return getValues(code -> code.getKey().endsWith(suffixToUse));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ public void resolveAliases(StringValueResolver valueResolver) {
Assert.notNull(valueResolver, "StringValueResolver must not be null");
synchronized (this.aliasMap) {
Map<String, String> aliasCopy = new HashMap<>(this.aliasMap);
for (String alias : aliasCopy.keySet()) {
String registeredName = aliasCopy.get(alias);
aliasCopy.forEach((alias, registeredName) -> {
String resolvedAlias = valueResolver.resolveStringValue(alias);
String resolvedName = valueResolver.resolveStringValue(registeredName);
if (resolvedAlias == null || resolvedName == null || resolvedAlias.equals(resolvedName)) {
Expand All @@ -154,7 +153,7 @@ else if (!resolvedAlias.equals(alias)) {
if (existingName.equals(resolvedName)) {
// Pointing to existing alias - just remove placeholder
this.aliasMap.remove(alias);
break;
return;
}
throw new IllegalStateException(
"Cannot register resolved alias '" + resolvedAlias + "' (original: '" + alias +
Expand All @@ -168,7 +167,7 @@ else if (!resolvedAlias.equals(alias)) {
else if (!registeredName.equals(resolvedName)) {
this.aliasMap.put(alias, resolvedName);
}
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1246,14 +1246,14 @@ static void postProcessAnnotationAttributes(@Nullable Object annotatedElement,
if (!attributes.validated) {
// Validate @AliasFor configuration
Map<String, List<String>> aliasMap = getAttributeAliasMap(annotationType);
for (String attributeName : aliasMap.keySet()) {
aliasMap.forEach((attributeName, aliasedAttributeNames) -> {
if (valuesAlreadyReplaced.contains(attributeName)) {
continue;
return;
}
Object value = attributes.get(attributeName);
boolean valuePresent = (value != null && !(value instanceof DefaultValueHolder));

for (String aliasedAttributeName : aliasMap.get(attributeName)) {
for (String aliasedAttributeName : aliasedAttributeNames) {
if (valuesAlreadyReplaced.contains(aliasedAttributeName)) {
continue;
}
Expand Down Expand Up @@ -1290,7 +1290,7 @@ else if (aliasPresent) {
}
}
}
}
});
attributes.validated = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,16 @@ private boolean parametersAreEqual(MimeType other) {
return false;
}

for (String key : this.parameters.keySet()) {
if (!other.parameters.containsKey(key)) {
for (Map.Entry<String, String> entry : this.parameters.entrySet()) {
if (!other.parameters.containsKey(entry.getKey())) {
return false;
}
if (PARAM_CHARSET.equals(key)) {
if (PARAM_CHARSET.equals(entry.getKey())) {
if (!ObjectUtils.nullSafeEquals(getCharset(), other.getCharset())) {
return false;
}
}
else if (!ObjectUtils.nullSafeEquals(this.parameters.get(key), other.parameters.get(key))) {
else if (!ObjectUtils.nullSafeEquals(entry.getValue(), other.parameters.get(entry.getKey()))) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,12 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
}

Map<String, Object> matchedParameters = new HashMap<>(inParameters.size());
for (String parameterName : inParameters.keySet()) {
inParameters.forEach((parameterName, parameterValue) -> {
String parameterNameToMatch = provider.parameterNameToUse(parameterName);
String callParameterName = callParameterNames.get(lowerCase(parameterNameToMatch));
if (callParameterName == null) {
if (logger.isDebugEnabled()) {
Object value = inParameters.get(parameterName);
Object value = parameterValue;
if (value instanceof SqlParameterValue) {
value = ((SqlParameterValue)value).getValue();
}
Expand All @@ -579,9 +579,9 @@ public Map<String, Object> matchInParameterValuesWithCallParameters(SqlParameter
}
}
else {
matchedParameters.put(callParameterName, inParameters.get(parameterName));
matchedParameters.put(callParameterName, parameterValue);
}
}
});

if (matchedParameters.size() < callParameterNames.size()) {
for (String parameterName : callParameterNames.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -244,15 +245,9 @@ public List<Object> matchInParameterValuesWithInsertColumns(SqlParameterSource p
* @param inParameters the parameter names and values
*/
public List<Object> matchInParameterValuesWithInsertColumns(Map<String, ?> inParameters) {
List<Object> values = new ArrayList<>();
Map<String, Object> source = new LinkedHashMap<>(inParameters.size());
for (String key : inParameters.keySet()) {
source.put(key.toLowerCase(), inParameters.get(key));
}
for (String column : this.tableColumns) {
values.add(source.get(column.toLowerCase()));
}
return values;
inParameters.forEach((key, value) -> source.put(key.toLowerCase(), value));
return this.tableColumns.stream().map(column -> source.get(column.toLowerCase())).collect(Collectors.toList());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public void setTranslators(Map<String, SQLExceptionTranslator> translators) {

@Override
public void afterPropertiesSet() {
for (String dbName : this.translators.keySet()) {
CustomSQLExceptionTranslatorRegistry.getInstance().registerTranslator(dbName, this.translators.get(dbName));
}
this.translators.forEach((dbName, translator) ->
CustomSQLExceptionTranslatorRegistry.getInstance().registerTranslator(dbName, translator));
}

}