classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] [generics] Patch; FYI: StringBuffer implements Appendable


From: Tom Tromey
Subject: [cp-patches] [generics] Patch; FYI: StringBuffer implements Appendable
Date: 07 Aug 2004 13:33:20 -0600

Another of my random 1.5 patches.
StringBuffer now implements the new Appendable interface.

Tom

Index: ChangeLog
from  Tom Tromey  <address@hidden>
        * java/lang/StringBuffer.java (StringBuffer(CharSequence)): New
        constructor.
        (append(CharSequence)): New method
        (append(CharSequence,int,int)): Likewise.
        (StringBuffer): Implements Appendable.

Index: java/lang/StringBuffer.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/lang/StringBuffer.java,v
retrieving revision 1.27
diff -u -r1.27 StringBuffer.java
--- java/lang/StringBuffer.java 17 Apr 2004 17:08:23 -0000 1.27
+++ java/lang/StringBuffer.java 7 Aug 2004 19:47:55 -0000
@@ -72,7 +72,8 @@
  * @since 1.0
  * @status updated to 1.4
  */
-public final class StringBuffer implements Serializable, CharSequence
+public final class StringBuffer
+  implements Serializable, CharSequence, Appendable
 {
   /**
    * Compatible with JDK 1.0+.
@@ -148,6 +149,25 @@
   }
 
   /**
+   * Create a new <code>StringBuffer</code> with the characters in the
+   * specified <code>CharSequence</code>. Initial capacity will be the
+   * length of the sequence plus 16; if the sequence reports a length
+   * less than or equal to 0, then the initial capacity will be 16.
+   *
+   * @param seq the initializing <code>CharSequence</code>
+   * @throws NullPointerException if str is null
+   * @since 1.5
+   */
+  public StringBuffer(CharSequence seq)
+  {
+    int len = seq.length();
+    count = len <= 0 ? 0 : len;
+    value = new char[count + DEFAULT_CAPACITY];
+    for (int i = 0; i < len; ++i)
+      value[i] = seq.charAt(i);
+  }
+
+  /**
    * Get the length of the <code>String</code> this <code>StringBuffer</code>
    * would create. Not to be confused with the <em>capacity</em> of the
    * <code>StringBuffer</code>.
@@ -407,6 +427,43 @@
   }
 
   /**
+   * Append the characters in the <code>CharSequence</code> to this
+   * buffer.
+   *
+   * @param seq the <code>CharSequence</code> providing the characters
+   * @return this <code>StringBuffer</code>
+   * @since 1.5
+   */
+  public synchronized StringBuffer append(CharSequence seq)
+  {
+    return append(seq, 0, seq.length());
+  }
+
+  /**
+   * Append some characters from the <code>CharSequence</code> to this
+   * buffer.  If the argument is null, the four characters "null" are
+   * appended.
+   *
+   * @param seq the <code>CharSequence</code> providing the characters
+   * @param start the starting index
+   * @param end one past the final index
+   * @return this <code>StringBuffer</code>
+   * @since 1.5
+   */
+  public synchronized StringBuffer append(CharSequence seq, int start, int end)
+  {
+    if (seq == null)
+      return append("null");
+    if (end - start > 0)
+      {
+       ensureCapacity_unsynchronized(count + end - start);
+       for (; start < end; ++start)
+         value[count++] = seq.charAt(start);
+      }
+    return this;
+  }
+
+  /**
    * Append the <code>String</code> value of the argument to this
    * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
    * to <code>String</code>.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]