Author: ggregory
Date: Sun Sep 23 13:01:27 2012
New Revision: 1389042

URL: http://svn.apache.org/viewvc?rev=1389042&view=rev
Log:
[LANG-825] Create StrBuilder APIs similar to String.format(String, Object...)

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrBuilder.java
    
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1389042&r1=1389041&r2=1389042&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Sun Sep 23 13:01:27 2012
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.2" date="TBA" description="Next release">
+    <action issue="LANG-825" type="add">Create StrBuilder APIs similar to 
String.format(String, Object...)</action>
     <action issue="LANG-817" type="fix">Add 
org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_8</action>
     <action issue="LANG-813" type="fix">StringUtils.equalsIgnoreCase doesn't 
check string reference equality</action>
     <action issue="LANG-810" type="fix">StringUtils.join() endIndex, bugged 
for loop</action>

Modified: 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrBuilder.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrBuilder.java?rev=1389042&r1=1389041&r2=1389042&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrBuilder.java
 (original)
+++ 
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/StrBuilder.java
 Sun Sep 23 13:01:27 2012
@@ -548,6 +548,19 @@ public class StrBuilder implements CharS
     }
 
     /**
+     * Calls {@link String#format(String, Object...)} and appends the result.
+     *
+     * @param format the format string
+     * @param objs the objects to use in the format string
+     * @return {@code this} to enable chaining
+     * @see String#format(String, Object...)
+     * @since 3.2
+     */
+    public StrBuilder append(String format, Object... objs) {
+        return append(String.format(format, objs));
+    }
+
+    /**
      * Appends a string buffer to this string builder.
      * Appending null will call {@link #appendNull()}.
      *
@@ -813,6 +826,19 @@ public class StrBuilder implements CharS
     }
 
     /**
+     * Calls {@link String#format(String, Object...)} and appends the result.
+     *
+     * @param format the format string
+     * @param objs the objects to use in the format string
+     * @return {@code this} to enable chaining
+     * @see String#format(String, Object...)
+     * @since 3.2
+     */
+    public StrBuilder appendln(String format, Object... objs) {
+        return append(format, objs).appendNewLine();
+    }
+
+    /**
      * Appends a string buffer followed by a new line to this string builder.
      * Appending null will call {@link #appendNull()}.
      *

Modified: 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java?rev=1389042&r1=1389041&r2=1389042&view=diff
==============================================================================
--- 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java
 (original)
+++ 
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java
 Sun Sep 23 13:01:27 2012
@@ -459,6 +459,28 @@ public class StrBuilderAppendInsertTest 
 
     //-----------------------------------------------------------------------
     @Test
+    public void testAppendln_FormattedString() {
+        final int[] count = new int[2];
+        StrBuilder sb = new StrBuilder() {
+            @Override
+            public StrBuilder append(String str) {
+                count[0]++;
+                return super.append(str);
+            }
+            @Override
+            public StrBuilder appendNewLine() {
+                count[1]++;
+                return super.appendNewLine();
+            }
+        };
+        sb.appendln("Hello %s", "Alice");
+        assertEquals("Hello Alice" + SEP, sb.toString());
+        assertEquals(2, count[0]);  // appendNewLine() calls append(String)
+        assertEquals(1, count[1]);
+    }
+
+    //-----------------------------------------------------------------------
+    @Test
     public void testAppendln_Object() {
         StrBuilder sb = new StrBuilder();
         sb.appendln((Object) null);
@@ -857,6 +879,28 @@ public class StrBuilderAppendInsertTest 
 
     //-----------------------------------------------------------------------
     @Test
+    public void testAppend_FormattedString() {
+        StrBuilder sb;
+
+        sb = new StrBuilder();
+        sb.append("Hi", (Object[]) null);
+        assertEquals("Hi", sb.toString());
+
+        sb = new StrBuilder();
+        sb.append("Hi", "Alice");
+        assertEquals("Hi", sb.toString());
+
+        sb = new StrBuilder();
+        sb.append("Hi %s", "Alice");
+        assertEquals("Hi Alice", sb.toString());
+
+        sb = new StrBuilder();
+        sb.append("Hi %s %,d", "Alice", 5000);
+        assertEquals("Hi Alice 5,000", sb.toString());
+    }
+
+    //-----------------------------------------------------------------------
+    @Test
     public void testAppendAll_Array() {
         StrBuilder sb = new StrBuilder();
         sb.appendAll((Object[]) null);


Reply via email to