Author: nbubna
Date: Thu Nov 20 22:06:52 2008
New Revision: 719491

URL: http://svn.apache.org/viewvc?rev=719491&view=rev
Log:
VELTOOLS-109 add a printf(String,Object...) method

Modified:
    
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java
    
velocity/tools/trunk/src/test/java/org/apache/velocity/tools/DisplayToolTests.java

Modified: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java?rev=719491&r1=719490&r2=719491&view=diff
==============================================================================
--- 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java
 (original)
+++ 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/DisplayTool.java
 Thu Nov 20 22:06:52 2008
@@ -68,7 +68,7 @@
  * @version $Id: DisplayTool.java 463298 2006-10-12 16:10:32Z henning $
  */
 @DefaultKey("display")
-public class DisplayTool extends SafeConfig
+public class DisplayTool extends LocaleConfig
 {
     public static final String LIST_DELIM_KEY = "listDelim";
     public static final String LIST_FINAL_DELIM_KEY = "listFinalDelim";
@@ -295,57 +295,95 @@
      * @deprecated Will be unnecessary with Velocity 1.6
      */
     @Deprecated 
-    public String message(String printf, Collection args)
+    public String message(String format, Collection args)
     {
-        return message(printf, new Object[] { args });
+        return message(format, new Object[] { args });
     }
 
     /**
      * @deprecated Will be unnecessary with Velocity 1.6
      */
     @Deprecated 
-    public String message(String printf, Object arg)
+    public String message(String format, Object arg)
     {
-        return message(printf, new Object[] { arg });
+        return message(format, new Object[] { arg });
     }
 
     /**
      * @deprecated Will be unnecessary with Velocity 1.6
      */
     @Deprecated 
-    public String message(String printf, Object arg1, Object arg2)
+    public String message(String format, Object arg1, Object arg2)
     {
-        return message(printf, new Object[] { arg1, arg2 });
+        return message(format, new Object[] { arg1, arg2 });
     }
 
     /**
      * Uses [EMAIL PROTECTED] MessageFormat} to format the specified String 
with
      * the specified arguments. If there are no arguments, then the String
-     * is returned directly.
+     * is returned directly.  Please note that the format
+     * required here is quite different from that of
+     * [EMAIL PROTECTED] #printf(String,Object...)}.
+     *
+     * @since VelocityTools 2.0
      */
-    public String message(String printf, Object... args)
+    public String message(String format, Object... args)
     {
-        if (printf == null)
+        if (format == null)
         {
             return null;
         }
         if (args == null || args.length == 0)
         {
-            return printf;
+            return format;
         }
         else if (args.length == 1 && args[0] instanceof Collection)
         {
             Collection list = (Collection)args[0];
             if (list.isEmpty())
             {
-                return printf;
+                return format;
+            }
+            else
+            {
+                args = list.toArray();
+            }
+        }
+        return MessageFormat.format(format, args);
+    }
+
+    /**
+     * Uses [EMAIL PROTECTED] String#format(Locale,String,Object...} to format 
the specified String
+     * with the specified arguments.  Please note that the format
+     * required here is quite different from that of
+     * [EMAIL PROTECTED] #message(String,Object...)}.
+     *
+     * @see java.util.Formatter
+     * @since VelocityTools 2.0
+     */
+    public String printf(String format, Object... args)
+    {
+        if (format == null)
+        {
+            return null;
+        }
+        if (args == null || args.length == 0)
+        {
+            return format;
+        }
+        if (args.length == 1 && args[0] instanceof Collection)
+        {
+            Collection list = (Collection)args[0];
+            if (list.isEmpty())
+            {
+                return format;
             }
             else
             {
                 args = list.toArray();
             }
         }
-        return MessageFormat.format(printf, args);
+        return String.format(getLocale(), format, args);
     }
 
     /**

Modified: 
velocity/tools/trunk/src/test/java/org/apache/velocity/tools/DisplayToolTests.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/src/test/java/org/apache/velocity/tools/DisplayToolTests.java?rev=719491&r1=719490&r2=719491&view=diff
==============================================================================
--- 
velocity/tools/trunk/src/test/java/org/apache/velocity/tools/DisplayToolTests.java
 (original)
+++ 
velocity/tools/trunk/src/test/java/org/apache/velocity/tools/DisplayToolTests.java
 Thu Nov 20 22:06:52 2008
@@ -157,6 +157,19 @@
         assertEquals("foo 2 bar", display.message("foo {1} {0}", "bar", 2));
     }
 
+    public @Test void methodPrintf_StringObjectVarArgs() throws Exception
+    {
+        DisplayTool display = new DisplayTool();
+        assertNull(display.printf(null));
+        assertEquals("foo", display.printf("foo"));
+        assertEquals("foo", display.printf("foo", (Object[])null));
+        assertEquals("foo", display.printf("foo", new Object[] {}));
+        assertEquals("foo", display.printf("foo", new ArrayList()));
+        assertEquals("foo", display.printf("foo", 1));
+        assertEquals("foo bar", display.printf("foo %s", "bar"));
+        assertEquals("foo 2 bar", display.printf("foo %2$d %1$s", "bar", 2));
+    }
+
     public @Test void methodList_Object() throws Exception
     {
         DisplayTool display = new DisplayTool();


Reply via email to