commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1407534 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/text/StrBuilder.java test/java/org/apache/commons/lang3/text/StrBuilderAppendInsertTest.java test/java/org/apache/commons/lang3/text/StrBuilderTest.java
Date Fri, 09 Nov 2012 16:42:20 GMT
Author: mbenson
Date: Fri Nov  9 16:42:18 2012
New Revision: 1407534

URL: http://svn.apache.org/viewvc?rev=1407534&view=rev
Log:
StrBuilder implements Builder; provide toStringBuilder as toStringBuffer

Modified:
    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
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java

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=1407534&r1=1407533&r2=1407534&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
Fri Nov  9 16:42:18 2012
@@ -25,6 +25,7 @@ import java.util.List;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.SystemUtils;
+import org.apache.commons.lang3.builder.Builder;
 
 /**
  * Builds a string from constituent parts providing a more flexible and powerful API
@@ -71,7 +72,7 @@ import org.apache.commons.lang3.SystemUt
  * @since 2.2
  * @version $Id$
  */
-public class StrBuilder implements CharSequence, Appendable, Serializable {
+public class StrBuilder implements CharSequence, Appendable, Serializable, Builder<String>
{
 
     /**
      * The extra capacity for new builders.
@@ -2661,6 +2662,27 @@ public class StrBuilder implements CharS
         return new StringBuffer(size).append(buffer, 0, size);
     }
 
+    /**
+     * Gets a StringBuilder version of the string builder, creating a
+     * new instance each time the method is called.
+     *
+     * @return the builder as a StringBuilder
+     * @since Apache Commons Lang 3.2
+     */
+    public StringBuilder toStringBuilder() {
+        return new StringBuilder(size).append(buffer, 0, size);
+    }
+
+    /**
+     * Implement the {@link Builder} interface.
+     * @return the builder as a String
+     * @since Apache Commons Lang 3.2
+     * @see #toString()
+     */
+    public String build() {
+        return toString();
+    }
+
     //-----------------------------------------------------------------------
     /**
      * Validates parameters defining a range of the builder.

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=1407534&r1=1407533&r2=1407534&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
Fri Nov  9 16:42:18 2012
@@ -916,6 +916,10 @@ public class StrBuilderAppendInsertTest 
         sb.clear();
         sb.appendAll(new Object[]{"foo", "bar", "baz"});
         assertEquals("foobarbaz", sb.toString());
+
+        sb.clear();
+        sb.appendAll("foo", "bar", "baz");
+        assertEquals("foobarbaz", sb.toString());
     }
 
     //-----------------------------------------------------------------------

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java?rev=1407534&r1=1407533&r2=1407534&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/StrBuilderTest.java
Fri Nov  9 16:42:18 2012
@@ -1802,6 +1802,16 @@ public class StrBuilderTest {
 
     //-----------------------------------------------------------------------
     @Test
+    public void testToStringBuilder() {
+        StrBuilder sb = new StrBuilder();
+        assertEquals(new StringBuilder().toString(), sb.toStringBuilder().toString());
+        
+        sb.append("junit");
+        assertEquals(new StringBuilder("junit").toString(), sb.toStringBuilder().toString());
+    }
+
+    //-----------------------------------------------------------------------
+    @Test
     public void testLang294() {
         StrBuilder sb = new StrBuilder("\n%BLAH%\nDo more stuff\neven more stuff\n%BLAH%\n");
         sb.deleteAll("\n%BLAH%");
@@ -1839,4 +1849,10 @@ public class StrBuilderTest {
         assertEquals( "Failed to invoke appendFixedWidthPadLeft correctly", "**********",
sb.toString());
     }
 
+    @Test
+    public void testAsBuilder() {
+        StrBuilder sb = new StrBuilder().appendAll("Lorem", " ", "ipsum", " ", "dolor");
+        assertEquals(sb.toString(), sb.build());
+    }
+
 }



Mime
View raw message