diff --git a/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java b/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java index 3b414e900..31b5608da 100644 --- a/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java +++ b/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java @@ -61,15 +61,30 @@ public class MessageFactory { Localizable specific, Localizable general, Object ... arguments) { + final StringBuilder sb = new StringBuilder(); - MessageFormat fmt = null; + final MessageFormat generalFmt = new MessageFormat(general.getLocalizedString(locale), locale); + Object[] generalArgs = arguments; + if (specific != null) { - fmt = new MessageFormat(specific.getLocalizedString(locale), locale); - sb.append(fmt.format(arguments)); + + 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)); sb.append(": "); + } - fmt = new MessageFormat(general.getLocalizedString(locale), locale); - sb.append(fmt.format(arguments)); + + sb.append(generalFmt.format(generalArgs)); return sb.toString(); } diff --git a/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java b/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java new file mode 100644 index 000000000..1bc43f059 --- /dev/null +++ b/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception.util; + +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +public class MessageFactoryTest { + + @Test + public void testSpecificGeneric() { + Localizable specific = new DummyLocalizable("specific {0} - {1} - {2}"); + 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); + } + +}