Skip to content

Commit

Permalink
Use Arrays.asList as composite key in StaticMessageSource
Browse files Browse the repository at this point in the history
  • Loading branch information
stsypanov committed Nov 13, 2019
1 parent f4c847b commit e4241fd
Showing 1 changed file with 8 additions and 6 deletions.
@@ -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

0 comments on commit e4241fd

Please sign in to comment.