Author: luc
Date: Sun Nov 14 15:33:16 2010
New Revision: 1035010

URL: http://svn.apache.org/viewvc?rev=1035010&view=rev
Log:
allow either specific or generic formats to be null

Modified:
    
commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java
    
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java

Modified: 
commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java?rev=1035010&r1=1035009&r2=1035010&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java
 Sun Nov 14 15:33:16 2010
@@ -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();
     }

Modified: 
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java?rev=1035010&r1=1035009&r2=1035010&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_X/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java
 Sun Nov 14 15:33:16 2010
@@ -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);
+    }
+
 }


Reply via email to