Skip to content
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

Avoid String concatenation for lookup in StaticMessageSource #22451

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,9 @@
package org.springframework.context.support;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

Expand All @@ -37,20 +39,20 @@
public class StaticMessageSource extends AbstractMessageSource {

/** Map from 'code + locale' keys to message Strings. */
private final Map<String, String> messages = new HashMap<>();
private final Map<List<?>, String> messages = new HashMap<>();

private final Map<String, MessageFormat> cachedMessageFormats = new HashMap<>();
private final Map<List<?>, MessageFormat> cachedMessageFormats = new HashMap<>();


@Override
protected String resolveCodeWithoutArguments(String code, Locale locale) {
return this.messages.get(code + '_' + locale.toString());
return this.messages.get(Arrays.asList(code, locale));
}

@Override
@Nullable
protected MessageFormat resolveCode(String code, Locale locale) {
String key = code + '_' + locale.toString();
List<?> key = Arrays.asList(code, locale);
String msg = this.messages.get(key);
if (msg == null) {
return null;
Expand All @@ -75,7 +77,7 @@ public void addMessage(String code, Locale locale, String msg) {
Assert.notNull(code, "Code must not be null");
Assert.notNull(locale, "Locale must not be null");
Assert.notNull(msg, "Message must not be null");
this.messages.put(code + '_' + locale.toString(), msg);
this.messages.put(Arrays.asList(code, locale), msg);
if (logger.isDebugEnabled()) {
logger.debug("Added message [" + msg + "] for code [" + code + "] and Locale [" + locale + "]");
}
Expand Down