You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/08/20 12:14:26 UTC

svn commit: r234016 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/text/StrBuilder.java test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java test/org/apache/commons/lang/text/StrBuilderTest.java

Author: scolebourne
Date: Sat Aug 20 03:14:16 2005
New Revision: 234016

URL: http://svn.apache.org/viewcvs?rev=234016&view=rev
Log:
Add appendNewLine()

Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java?rev=234016&r1=234015&r2=234016&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java Sat Aug 20 03:14:16 2005
@@ -22,6 +22,7 @@
 import java.util.Iterator;
 
 import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.SystemUtils;
 
 /**
  * Builds a string from consituant parts providing a more flexible and powerful API
@@ -71,6 +72,8 @@
     protected char[] buffer;
     /** Current size of the buffer. */
     protected int size;
+    /** The new line. */
+    private String newLine;
     /** The null text. */
     private String nullText;
 
@@ -113,6 +116,27 @@
 
     //-----------------------------------------------------------------------
     /**
+     * Gets the text to be appended when a new line is added.
+     *
+     * @return the new line text, null means use system default
+     */
+    public String getNewLineText() {
+        return newLine;
+    }
+
+    /**
+     * Sets the text to be appended when a new line is added.
+     *
+     * @param newLine  the new line text, null means use system default
+     * @return this, to enable chaining
+     */
+    public StrBuilder setNewLineText(String newLine) {
+        this.newLine = newLine;
+        return this;
+    }
+
+    //-----------------------------------------------------------------------
+    /**
      * Gets the text to be appended when null is added.
      *
      * @return the null text, null means no append
@@ -124,14 +148,14 @@
     /**
      * Sets the text to be appended when null is added.
      *
-     * @param str  the null text, null means no append
+     * @param nullText  the null text, null means no append
      * @return this, to enable chaining
      */
-    public StrBuilder setNullText(String str) {
-        if (str != null && str.length() == 0) {
-            str = null;
+    public StrBuilder setNullText(String nullText) {
+        if (nullText != null && nullText.length() == 0) {
+            nullText = null;
         }
-        nullText = str;
+        this.nullText = nullText;
         return this;
     }
 
@@ -378,6 +402,23 @@
     }
 
     //-----------------------------------------------------------------------
+    /**
+     * Appends the new line string to this string builder.
+     * <p>
+     * The new line string can be altered using {@link #setNewLineText(String)}.
+     * This might be used to force the output to always use Unix line endings
+     * even when on Windows.
+     *
+     * @return this, to enable chaining
+     */
+    public StrBuilder appendNewLine() {
+        if (newLine == null)  {
+            append(SystemUtils.LINE_SEPARATOR);
+            return this;
+        }
+        return append(newLine);
+    }
+
     /**
      * Appends the text representing <code>null</code> to this string builder.
      *

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java?rev=234016&r1=234015&r2=234016&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java (original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderAppendInsertTest.java Sat Aug 20 03:14:16 2005
@@ -21,6 +21,8 @@
 import java.util.Collections;
 import java.util.Iterator;
 
+import org.apache.commons.lang.SystemUtils;
+
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -67,6 +69,17 @@
      */
     public StrBuilderAppendInsertTest(String name) {
         super(name);
+    }
+
+    //-----------------------------------------------------------------------
+    public void testAppendNewLine() {
+        StrBuilder sb = new StrBuilder("---");
+        sb.appendNewLine().append("+++");
+        assertEquals("---" + SystemUtils.LINE_SEPARATOR + "+++", sb.toString());
+        
+        sb = new StrBuilder("---");
+        sb.setNewLineText("#").appendNewLine().setNewLineText(null).appendNewLine();
+        assertEquals("---#" + SystemUtils.LINE_SEPARATOR, sb.toString());
     }
 
     //-----------------------------------------------------------------------

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java?rev=234016&r1=234015&r2=234016&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java (original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java Sat Aug 20 03:14:16 2005
@@ -111,6 +111,7 @@
     //-----------------------------------------------------------------------
     public void testChaining() {
         StrBuilder sb = new StrBuilder();
+        assertSame(sb, sb.setNewLineText(null));
         assertSame(sb, sb.setNullText(null));
         assertSame(sb, sb.setLength(1));
         assertSame(sb, sb.setCharAt(0, 'a'));
@@ -121,6 +122,39 @@
     }
 
     //-----------------------------------------------------------------------
+    public void testGetSetNewLineText() {
+        StrBuilder sb = new StrBuilder();
+        assertEquals(null, sb.getNewLineText());
+
+        sb.setNewLineText("#");
+        assertEquals("#", sb.getNewLineText());
+
+        sb.setNewLineText("");
+        assertEquals("", sb.getNewLineText());
+
+        sb.setNewLineText((String) null);
+        assertEquals(null, sb.getNewLineText());
+    }
+
+    //-----------------------------------------------------------------------
+    public void testGetSetNullText() {
+        StrBuilder sb = new StrBuilder();
+        assertEquals(null, sb.getNullText());
+
+        sb.setNullText("null");
+        assertEquals("null", sb.getNullText());
+
+        sb.setNullText("");
+        assertEquals(null, sb.getNullText());
+
+        sb.setNullText("NULL");
+        assertEquals("NULL", sb.getNullText());
+
+        sb.setNullText((String) null);
+        assertEquals(null, sb.getNullText());
+    }
+
+    //-----------------------------------------------------------------------
     public void testCapacityAndLength() {
         StrBuilder sb = new StrBuilder();
         assertEquals(32, sb.capacity());
@@ -478,24 +512,6 @@
         }
         catch (IndexOutOfBoundsException e) {
         }
-    }
-
-    //-----------------------------------------------------------------------
-    public void testNullText() {
-        StrBuilder sb = new StrBuilder();
-        assertEquals(null, sb.getNullText());
-
-        sb.setNullText("null");
-        assertEquals("null", sb.getNullText());
-
-        sb.setNullText("");
-        assertEquals(null, sb.getNullText());
-
-        sb.setNullText("NULL");
-        assertEquals("NULL", sb.getNullText());
-
-        sb.setNullText((String) null);
-        assertEquals(null, sb.getNullText());
     }
 
     //-----------------------------------------------------------------------



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org