commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r230918 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/text/StrBuilder.java test/org/apache/commons/lang/text/StrBuilderTest.java
Date Mon, 08 Aug 2005 22:45:13 GMT
Author: scolebourne
Date: Mon Aug  8 15:44:59 2005
New Revision: 230918

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

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/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=230918&r1=230917&r2=230918&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
Mon Aug  8 15:44:59 2005
@@ -17,6 +17,7 @@
 
 import java.io.CharArrayReader;
 import java.io.Reader;
+import java.io.Writer;
 import java.util.Collection;
 import java.util.Iterator;
 
@@ -1579,6 +1580,26 @@
     }
 
     //-----------------------------------------------------------------------
+    /**
+     * Gets this builder as a Writer that can be written to.
+     * <p>
+     * This method allows you to populate the contents of the builder
+     * using any standard method that takes a Writer.
+     * <p>
+     * To use, simply create a <code>StrBuilder</code>,
+     * call <code>asWriter</code>, and populate away. The data is available
+     * at any time using the methods of the <code>StrBuilder</code>.
+     * Note however, that no synchronization occurs, so you must not read
+     * the builder from one thread while writing in another thread.
+     * Note also that close and flush have no effect on the writer.
+     *
+     * @return a writer that populates this builder
+     */
+    public Writer asWriter() {
+        return new StrBuilderWriter();
+    }
+
+    //-----------------------------------------------------------------------
 //    /**
 //     * Gets a String version of the string builder by calling the internal
 //     * constructor of String by reflection.
@@ -1663,6 +1684,51 @@
     protected void validateIndex(int index) {
         if (index < 0 || index > size) {
             throw new StringIndexOutOfBoundsException(index);
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Inner class to allow StrBuilder to operate as a writer.
+     */
+    class StrBuilderWriter extends Writer {
+        StrBuilderWriter() {
+            super();
+        }
+
+        /** @inheritdoc */
+        public void close() {
+            // do nothing
+        }
+
+        /** @inheritdoc */
+        public void flush() {
+            // do nothing
+        }
+
+        /** @inheritdoc */
+        public void write(int c) {
+            append((char) c);
+        }
+
+        /** @inheritdoc */
+        public void write(char[] cbuf) {
+            append(cbuf);
+        }
+
+        /** @inheritdoc */
+        public void write(char[] cbuf, int off, int len) {
+            append(cbuf, off, len);
+        }
+
+        /** @inheritdoc */
+        public void write(String str) {
+            append(str);
+        }
+
+        /** @inheritdoc */
+        public void write(String str, int off, int len) {
+            append(str, off, len);
         }
     }
 

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=230918&r1=230917&r2=230918&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
Mon Aug  8 15:44:59 2005
@@ -17,6 +17,7 @@
 package org.apache.commons.lang.text;
 
 import java.io.Reader;
+import java.io.Writer;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -1837,6 +1838,40 @@
         
         buf = new char[40];
         assertEquals(-1, reader.read(buf));
+    }
+
+    //-----------------------------------------------------------------------
+    public void testAsWriter() throws Exception {
+        StrBuilder sb = new StrBuilder ("base");
+        Writer writer = sb.asWriter();
+        
+        writer.write('l');
+        assertEquals("basel", sb.toString());
+        
+        writer.write(new char[] {'i', 'n'});
+        assertEquals("baselin", sb.toString());
+        
+        writer.write(new char[] {'n', 'e', 'r'}, 1, 2);
+        assertEquals("baseliner", sb.toString());
+        
+        writer.write(" rout");
+        assertEquals("baseliner rout", sb.toString());
+        
+        writer.write("ping that server", 1, 3);
+        assertEquals("baseliner routing", sb.toString());
+        
+        writer.flush();  // no effect
+        assertEquals("baseliner routing", sb.toString());
+        
+        writer.close();  // no effect
+        assertEquals("baseliner routing", sb.toString());
+        
+        writer.write(" hi");  // works after close
+        assertEquals("baseliner routing hi", sb.toString());
+        
+        sb.setLength(4);  // mix and match
+        writer.write('d');
+        assertEquals("based", sb.toString());
     }
 
 }



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


Mime
View raw message