reverting change introduced in 1035003:
it was the javadoc that did not correspond to the intended behavior, not the code git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1035072 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
318d66e1b1
commit
65e95d0224
|
@ -52,42 +52,25 @@ public class MessageFactory {
|
|||
* @param locale Locale in which the message should be translated.
|
||||
* @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.
|
||||
* @param arguments Format arguments. They will be substituted in
|
||||
* <em>both</em> the {@code general} and {@code specific} format specifiers.
|
||||
* @return a localized message string.
|
||||
*/
|
||||
public static String buildMessage(Locale locale,
|
||||
Localizable specific,
|
||||
Localizable general,
|
||||
Object ... arguments) {
|
||||
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
Object[] generalArgs = arguments;
|
||||
|
||||
if (specific != null) {
|
||||
|
||||
final MessageFormat specificFmt = new MessageFormat(specific.getLocalizedString(locale), locale);
|
||||
|
||||
// split the arguments: first specific ones then general ones
|
||||
final int nbSpecific = Math.min(arguments.length, specificFmt.getFormatsByArgumentIndex().length);
|
||||
final int nbGeneral = arguments.length - nbSpecific;
|
||||
Object[] specificArgs = new Object[nbSpecific];
|
||||
System.arraycopy(arguments, 0, specificArgs, 0, nbSpecific);
|
||||
generalArgs = new Object[nbGeneral];
|
||||
System.arraycopy(arguments, nbSpecific, generalArgs, 0, nbGeneral);
|
||||
|
||||
// build the message
|
||||
sb.append(specificFmt.format(specificArgs));
|
||||
|
||||
}
|
||||
|
||||
if (general != null) {
|
||||
if (specific != null) {
|
||||
final MessageFormat fmt = new MessageFormat(general.getLocalizedString(locale), locale);
|
||||
sb.append(fmt.format(arguments));
|
||||
}
|
||||
if (specific != null) {
|
||||
if (general != null) {
|
||||
sb.append(": ");
|
||||
}
|
||||
final MessageFormat generalFmt = new MessageFormat(general.getLocalizedString(locale), locale);
|
||||
sb.append(generalFmt.format(generalArgs));
|
||||
final MessageFormat fmt = new MessageFormat(specific.getLocalizedString(locale), locale);
|
||||
sb.append(fmt.format(arguments));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
|
|
@ -29,26 +29,25 @@ public class MessageFactoryTest {
|
|||
Localizable general = new DummyLocalizable("general {0} / {1}");
|
||||
String message = MessageFactory.buildMessage(Locale.FRENCH, specific, general,
|
||||
0, 1, 2, 'a', 'b');
|
||||
Assert.assertEquals("specific 0 - 1 - 2: general a / b", message);
|
||||
Assert.assertEquals("general 0 / 1: specific 0 - 1 - 2", 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);
|
||||
0, 1, 2, 'a', 'b');
|
||||
Assert.assertEquals("general 0 / 1", message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullGeneral() {
|
||||
Localizable specific = new DummyLocalizable("specific {0} - {1} - {2}");
|
||||
String message = MessageFactory.buildMessage(Locale.FRENCH, specific, null,
|
||||
0, 1, 2);
|
||||
0, 1, 2, 'a', 'b');
|
||||
Assert.assertEquals("specific 0 - 1 - 2", message);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNull() {
|
||||
String message = MessageFactory.buildMessage(Locale.FRENCH, null, null, "nothing");
|
||||
|
|
Loading…
Reference in New Issue