allow either specific or generic formats to be null

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_X@1035010 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2010-11-14 15:33:16 +00:00
parent 85fec9ef02
commit 5288b179f0
2 changed files with 32 additions and 6 deletions

View File

@ -50,8 +50,8 @@ public class MessageFactory {
* an argument list.
*
* @param locale Locale in which the message should be translated.
* @param specific Format specifier.
* @param general Format specifier.
* @param specific Format specifier (may be null).
* @param general Format specifier (may be null).
* @param arguments Format arguments. They will be substituted first in
* the {@code specific} format specifier, then the remaining arguments
* will be substituted in the {@code general} format specifier.
@ -63,7 +63,6 @@ public class MessageFactory {
Object ... arguments) {
final StringBuilder sb = new StringBuilder();
final MessageFormat generalFmt = new MessageFormat(general.getLocalizedString(locale), locale);
Object[] generalArgs = arguments;
if (specific != null) {
@ -80,11 +79,16 @@ public class MessageFactory {
// build the message
sb.append(specificFmt.format(specificArgs));
sb.append(": ");
}
sb.append(generalFmt.format(generalArgs));
if (general != null) {
if (specific != null) {
sb.append(": ");
}
final MessageFormat generalFmt = new MessageFormat(general.getLocalizedString(locale), locale);
sb.append(generalFmt.format(generalArgs));
}
return sb.toString();
}

View File

@ -24,7 +24,7 @@ import org.junit.Test;
public class MessageFactoryTest {
@Test
public void testSpecificGeneric() {
public void testSpecificGeneral() {
Localizable specific = new DummyLocalizable("specific {0} - {1} - {2}");
Localizable general = new DummyLocalizable("general {0} / {1}");
String message = MessageFactory.buildMessage(Locale.FRENCH, specific, general,
@ -32,4 +32,26 @@ public class MessageFactoryTest {
Assert.assertEquals("specific 0 - 1 - 2: general a / b", message);
}
@Test
public void testNullSpecific() {
Localizable general = new DummyLocalizable("general {0} / {1}");
String message = MessageFactory.buildMessage(Locale.FRENCH, null, general,
'a', 'b');
Assert.assertEquals("general a / b", message);
}
@Test
public void testNullGeneral() {
Localizable specific = new DummyLocalizable("specific {0} - {1} - {2}");
String message = MessageFactory.buildMessage(Locale.FRENCH, specific, null,
0, 1, 2);
Assert.assertEquals("specific 0 - 1 - 2", message);
}
@Test
public void testNull() {
String message = MessageFactory.buildMessage(Locale.FRENCH, null, null, "nothing");
Assert.assertEquals("", message);
}
}