Skip to content

Commit b5db730

Browse files
committed
Merge pull request #31267 from kang-hl
* pr/31267: Update copyright year of changed file Optimize MessageSourceSupport Closes gh-31267
2 parents 6486c2a + 182f911 commit b5db730

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

spring-context/src/main/java/org/springframework/context/support/MessageSourceSupport.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,9 +17,9 @@
1717
package org.springframework.context.support;
1818

1919
import java.text.MessageFormat;
20-
import java.util.HashMap;
2120
import java.util.Locale;
2221
import java.util.Map;
22+
import java.util.concurrent.ConcurrentHashMap;
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
@@ -53,7 +53,7 @@ public abstract class MessageSourceSupport {
5353
* Used for passed-in default messages. MessageFormats for resolved
5454
* codes are cached on a specific basis in subclasses.
5555
*/
56-
private final Map<String, Map<Locale, MessageFormat>> messageFormatsPerMessage = new HashMap<>();
56+
private final Map<String, Map<Locale, MessageFormat>> messageFormatsPerMessage = new ConcurrentHashMap<>();
5757

5858

5959
/**
@@ -116,32 +116,22 @@ protected String formatMessage(String msg, @Nullable Object[] args, Locale local
116116
if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) {
117117
return msg;
118118
}
119-
MessageFormat messageFormat = null;
120-
synchronized (this.messageFormatsPerMessage) {
121-
Map<Locale, MessageFormat> messageFormatsPerLocale = this.messageFormatsPerMessage.get(msg);
122-
if (messageFormatsPerLocale != null) {
123-
messageFormat = messageFormatsPerLocale.get(locale);
119+
Map<Locale, MessageFormat> messageFormatsPerLocale = this.messageFormatsPerMessage
120+
.computeIfAbsent(msg, key -> new ConcurrentHashMap<>());
121+
MessageFormat messageFormat = messageFormatsPerLocale.computeIfAbsent(locale, key -> {
122+
try {
123+
return createMessageFormat(msg, locale);
124124
}
125-
else {
126-
messageFormatsPerLocale = new HashMap<>();
127-
this.messageFormatsPerMessage.put(msg, messageFormatsPerLocale);
128-
}
129-
if (messageFormat == null) {
130-
try {
131-
messageFormat = createMessageFormat(msg, locale);
132-
}
133-
catch (IllegalArgumentException ex) {
134-
// Invalid message format - probably not intended for formatting,
135-
// rather using a message structure with no arguments involved...
136-
if (isAlwaysUseMessageFormat()) {
137-
throw ex;
138-
}
139-
// Silently proceed with raw message if format not enforced...
140-
messageFormat = INVALID_MESSAGE_FORMAT;
125+
catch (IllegalArgumentException ex) {
126+
// Invalid message format - probably not intended for formatting,
127+
// rather using a message structure with no arguments involved...
128+
if (isAlwaysUseMessageFormat()) {
129+
throw ex;
141130
}
142-
messageFormatsPerLocale.put(locale, messageFormat);
131+
// Silently proceed with raw message if format not enforced...
132+
return INVALID_MESSAGE_FORMAT;
143133
}
144-
}
134+
});
145135
if (messageFormat == INVALID_MESSAGE_FORMAT) {
146136
return msg;
147137
}

0 commit comments

Comments
 (0)