You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2008/01/04 18:56:03 UTC

svn commit: r608942 - in /velocity/tools/trunk/src: main/java/org/apache/velocity/tools/generic/DisplayTool.java test/java/org/apache/velocity/tools/DisplayToolTests.java

Author: nbubna
Date: Fri Jan  4 09:55:58 2008
New Revision: 608942

URL: http://svn.apache.org/viewvc?rev=608942&view=rev
Log:
add printf-like message() methods

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=608942&r1=608941&r2=608942&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 Fri Jan  4 09:55:58 2008
@@ -20,6 +20,7 @@
  */
 
 import java.lang.reflect.Array;
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -31,7 +32,8 @@
  * Currently, this class contains methods for "pretty printing" an array or
  * {@link Collection}, methods for truncating the string value of a reference
  * at a configured or specified length, methods for displaying an alternate
- * value when a specified value is null, a method for generating whitespace, and
+ * value when a specified value is null, a method for generating whitespace, 
+ * a "printf" type of method for formatting messages, and
  * methods for forcing values into "cells" of equal size (via truncation or
  * padding with whitespace).
  *
@@ -287,6 +289,63 @@
             }
         }
         return sb.toString();
+    }
+
+    /**
+     * @deprecated Will be unnecessary with Velocity 1.6
+     */
+    @Deprecated 
+    public String message(String printf, Collection args)
+    {
+        return message(printf, new Object[] { args });
+    }
+
+    /**
+     * @deprecated Will be unnecessary with Velocity 1.6
+     */
+    @Deprecated 
+    public String message(String printf, Object arg)
+    {
+        return message(printf, new Object[] { arg });
+    }
+
+    /**
+     * @deprecated Will be unnecessary with Velocity 1.6
+     */
+    @Deprecated 
+    public String message(String printf, Object arg1, Object arg2)
+    {
+        return message(printf, new Object[] { arg1, arg2 });
+    }
+
+    /**
+     * Uses {@link MessageFormat} to format the specified String with
+     * the specified arguments. If there are no arguments, then the String
+     * is returned directly.
+     */
+    public String message(String printf, Object... args)
+    {
+        if (printf == null)
+        {
+            return null;
+        }
+        if (args == null || args.length == 0)
+        {
+            return printf;
+        }
+        else if (args.length == 1 && args[0] instanceof Collection)
+        {
+            Collection list = (Collection)args[0];
+            if (list.isEmpty())
+            {
+                return printf;
+            }
+            else
+            {
+                args = list.toArray();
+            }
+        }
+        return MessageFormat.format(printf, 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=608942&r1=608941&r2=608942&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 Fri Jan  4 09:55:58 2008
@@ -144,6 +144,19 @@
         assertEquals(3, dims.getHeight());
     }
 
+    public @Test void methodMessage_StringObjectVarArgs() throws Exception
+    {
+        DisplayTool display = new DisplayTool();
+        assertNull(display.message(null));
+        assertEquals("foo", display.message("foo"));
+        assertEquals("foo", display.message("foo", (Object[])null));
+        assertEquals("foo", display.message("foo", new Object[] {}));
+        assertEquals("foo", display.message("foo", new ArrayList()));
+        assertEquals("foo", display.message("foo", 1));
+        assertEquals("foo bar", display.message("foo {0}", "bar"));
+        assertEquals("foo 2 bar", display.message("foo {1} {0}", "bar", 2));
+    }
+
     public @Test void methodList_Object() throws Exception
     {
         DisplayTool display = new DisplayTool();