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:
parent
85fec9ef02
commit
5288b179f0
|
@ -50,8 +50,8 @@ public class MessageFactory {
|
||||||
* an argument list.
|
* an argument list.
|
||||||
*
|
*
|
||||||
* @param locale Locale in which the message should be translated.
|
* @param locale Locale in which the message should be translated.
|
||||||
* @param specific Format specifier.
|
* @param specific Format specifier (may be null).
|
||||||
* @param general Format specifier.
|
* @param general Format specifier (may be null).
|
||||||
* @param arguments Format arguments. They will be substituted first in
|
* @param arguments Format arguments. They will be substituted first in
|
||||||
* the {@code specific} format specifier, then the remaining arguments
|
* the {@code specific} format specifier, then the remaining arguments
|
||||||
* will be substituted in the {@code general} format specifier.
|
* will be substituted in the {@code general} format specifier.
|
||||||
|
@ -63,7 +63,6 @@ public class MessageFactory {
|
||||||
Object ... arguments) {
|
Object ... arguments) {
|
||||||
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
final MessageFormat generalFmt = new MessageFormat(general.getLocalizedString(locale), locale);
|
|
||||||
Object[] generalArgs = arguments;
|
Object[] generalArgs = arguments;
|
||||||
|
|
||||||
if (specific != null) {
|
if (specific != null) {
|
||||||
|
@ -80,11 +79,16 @@ public class MessageFactory {
|
||||||
|
|
||||||
// build the message
|
// build the message
|
||||||
sb.append(specificFmt.format(specificArgs));
|
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();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ import org.junit.Test;
|
||||||
public class MessageFactoryTest {
|
public class MessageFactoryTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSpecificGeneric() {
|
public void testSpecificGeneral() {
|
||||||
Localizable specific = new DummyLocalizable("specific {0} - {1} - {2}");
|
Localizable specific = new DummyLocalizable("specific {0} - {1} - {2}");
|
||||||
Localizable general = new DummyLocalizable("general {0} / {1}");
|
Localizable general = new DummyLocalizable("general {0} / {1}");
|
||||||
String message = MessageFactory.buildMessage(Locale.FRENCH, specific, general,
|
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);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue