./src/share/classes/java/lang/StringBuilder.java

Print this page
rev 5910 : 6206780: (str) Forwarding append methods in String{Buffer,Builder} are inconsistent
Summary: update StringBuilder & StringBuffer to consistently handle forwarding to AbstractStringBuilder. Some additional cleanup (removal of refs to sub-classes from AbstractStringBuilder)
Reviewed-by: chegar,alanb


 107      */
 108     public StringBuilder(String str) {
 109         super(str.length() + 16);
 110         append(str);
 111     }
 112 
 113     /**
 114      * Constructs a string builder that contains the same characters
 115      * as the specified <code>CharSequence</code>. The initial capacity of
 116      * the string builder is <code>16</code> plus the length of the
 117      * <code>CharSequence</code> argument.
 118      *
 119      * @param      seq   the sequence to copy.
 120      * @throws    NullPointerException if <code>seq</code> is <code>null</code>
 121      */
 122     public StringBuilder(CharSequence seq) {
 123         this(seq.length() + 16);
 124         append(seq);
 125     }
 126 

 127     public StringBuilder append(Object obj) {
 128         return append(String.valueOf(obj));
 129     }
 130 

 131     public StringBuilder append(String str) {
 132         super.append(str);
 133         return this;
 134     }
 135 
 136     // Appends the specified string builder to this sequence.
 137     private StringBuilder append(StringBuilder sb) {
 138         if (sb == null)
 139             return append("null");
 140         int len = sb.length();
 141         int newcount = count + len;
 142         if (newcount > value.length)
 143             expandCapacity(newcount);
 144         sb.getChars(0, len, value, count);
 145         count = newcount;
 146         return this;
 147     }
 148 
 149     /**
 150      * Appends the specified <tt>StringBuffer</tt> to this sequence.
 151      * <p>
 152      * The characters of the <tt>StringBuffer</tt> argument are appended,
 153      * in order, to this sequence, increasing the
 154      * length of this sequence by the length of the argument.
 155      * If <tt>sb</tt> is <tt>null</tt>, then the four characters
 156      * <tt>"null"</tt> are appended to this sequence.
 157      * <p>
 158      * Let <i>n</i> be the length of this character sequence just prior to
 159      * execution of the <tt>append</tt> method. Then the character at index
 160      * <i>k</i> in the new character sequence is equal to the character at
 161      * index <i>k</i> in the old character sequence, if <i>k</i> is less than
 162      * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
 163      * in the argument <code>sb</code>.
 164      *
 165      * @param   sb   the <tt>StringBuffer</tt> to append.
 166      * @return  a reference to this object.
 167      */
 168     public StringBuilder append(StringBuffer sb) {
 169         super.append(sb);
 170         return this;
 171     }
 172 
 173     /**
 174      */
 175     public StringBuilder append(CharSequence s) {
 176         if (s == null)
 177             s = "null";
 178         if (s instanceof String)
 179             return this.append((String)s);
 180         if (s instanceof StringBuffer)
 181             return this.append((StringBuffer)s);
 182         if (s instanceof StringBuilder)
 183             return this.append((StringBuilder)s);
 184         return this.append(s, 0, s.length());
 185     }
 186 
 187     /**
 188      * @throws     IndexOutOfBoundsException {@inheritDoc}
 189      */

 190     public StringBuilder append(CharSequence s, int start, int end) {
 191         super.append(s, start, end);
 192         return this;
 193     }
 194 

 195     public StringBuilder append(char[] str) {
 196         super.append(str);
 197         return this;
 198     }
 199 
 200     /**
 201      * @throws IndexOutOfBoundsException {@inheritDoc}
 202      */

 203     public StringBuilder append(char[] str, int offset, int len) {
 204         super.append(str, offset, len);
 205         return this;
 206     }
 207 

 208     public StringBuilder append(boolean b) {
 209         super.append(b);
 210         return this;
 211     }
 212 

 213     public StringBuilder append(char c) {
 214         super.append(c);
 215         return this;
 216     }
 217 

 218     public StringBuilder append(int i) {
 219         super.append(i);
 220         return this;
 221     }
 222 

 223     public StringBuilder append(long lng) {
 224         super.append(lng);
 225         return this;
 226     }
 227 

 228     public StringBuilder append(float f) {
 229         super.append(f);
 230         return this;
 231     }
 232 

 233     public StringBuilder append(double d) {
 234         super.append(d);
 235         return this;
 236     }
 237 
 238     /**
 239      * @since 1.5
 240      */

 241     public StringBuilder appendCodePoint(int codePoint) {
 242         super.appendCodePoint(codePoint);
 243         return this;
 244     }
 245 
 246     /**
 247      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 248      */

 249     public StringBuilder delete(int start, int end) {
 250         super.delete(start, end);
 251         return this;
 252     }
 253 
 254     /**
 255      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 256      */

 257     public StringBuilder deleteCharAt(int index) {
 258         super.deleteCharAt(index);
 259         return this;
 260     }
 261 
 262     /**
 263      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 264      */

 265     public StringBuilder replace(int start, int end, String str) {
 266         super.replace(start, end, str);
 267         return this;
 268     }
 269 
 270     /**
 271      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 272      */

 273     public StringBuilder insert(int index, char[] str, int offset,
 274                                 int len)
 275     {
 276         super.insert(index, str, offset, len);
 277         return this;
 278     }
 279 
 280     /**
 281      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 282      */

 283     public StringBuilder insert(int offset, Object obj) {
 284         return insert(offset, String.valueOf(obj));

 285     }
 286 
 287     /**
 288      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 289      */

 290     public StringBuilder insert(int offset, String str) {
 291         super.insert(offset, str);
 292         return this;
 293     }
 294 
 295     /**
 296      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 297      */

 298     public StringBuilder insert(int offset, char[] str) {
 299         super.insert(offset, str);
 300         return this;
 301     }
 302 
 303     /**
 304      * @throws IndexOutOfBoundsException {@inheritDoc}
 305      */

 306     public StringBuilder insert(int dstOffset, CharSequence s) {
 307         if (s == null)
 308             s = "null";
 309         if (s instanceof String)
 310             return this.insert(dstOffset, (String)s);
 311         return this.insert(dstOffset, s, 0, s.length());
 312     }
 313 
 314     /**
 315      * @throws IndexOutOfBoundsException {@inheritDoc}
 316      */

 317     public StringBuilder insert(int dstOffset, CharSequence s,
 318                                 int start, int end)
 319     {
 320         super.insert(dstOffset, s, start, end);
 321         return this;
 322     }
 323 
 324     /**
 325      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 326      */

 327     public StringBuilder insert(int offset, boolean b) {
 328         super.insert(offset, b);
 329         return this;
 330     }
 331 
 332     /**
 333      * @throws IndexOutOfBoundsException {@inheritDoc}
 334      */

 335     public StringBuilder insert(int offset, char c) {
 336         super.insert(offset, c);
 337         return this;
 338     }
 339 
 340     /**
 341      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 342      */

 343     public StringBuilder insert(int offset, int i) {
 344         return insert(offset, String.valueOf(i));

 345     }
 346 
 347     /**
 348      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 349      */

 350     public StringBuilder insert(int offset, long l) {
 351         return insert(offset, String.valueOf(l));

 352     }
 353 
 354     /**
 355      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 356      */

 357     public StringBuilder insert(int offset, float f) {
 358         return insert(offset, String.valueOf(f));

 359     }
 360 
 361     /**
 362      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 363      */

 364     public StringBuilder insert(int offset, double d) {
 365         return insert(offset, String.valueOf(d));

 366     }
 367 
 368     /**
 369      * @throws NullPointerException {@inheritDoc}
 370      */

 371     public int indexOf(String str) {
 372         return indexOf(str, 0);
 373     }
 374 
 375     /**
 376      * @throws NullPointerException {@inheritDoc}
 377      */

 378     public int indexOf(String str, int fromIndex) {
 379         return String.indexOf(value, 0, count,
 380                               str.toCharArray(), 0, str.length(), fromIndex);
 381     }
 382 
 383     /**
 384      * @throws NullPointerException {@inheritDoc}
 385      */

 386     public int lastIndexOf(String str) {
 387         return lastIndexOf(str, count);
 388     }
 389 
 390     /**
 391      * @throws NullPointerException {@inheritDoc}
 392      */

 393     public int lastIndexOf(String str, int fromIndex) {
 394         return String.lastIndexOf(value, 0, count,
 395                               str.toCharArray(), 0, str.length(), fromIndex);
 396     }
 397 

 398     public StringBuilder reverse() {
 399         super.reverse();
 400         return this;
 401     }
 402 

 403     public String toString() {
 404         // Create a copy, don't share the array
 405         return new String(value, 0, count);
 406     }
 407 
 408     /**
 409      * Save the state of the <tt>StringBuilder</tt> instance to a stream
 410      * (that is, serialize it).
 411      *
 412      * @serialData the number of characters currently stored in the string
 413      *             builder (<tt>int</tt>), followed by the characters in the
 414      *             string builder (<tt>char[]</tt>).   The length of the
 415      *             <tt>char</tt> array may be greater than the number of
 416      *             characters currently stored in the string builder, in which
 417      *             case extra characters are ignored.
 418      */
 419     private void writeObject(java.io.ObjectOutputStream s)
 420         throws java.io.IOException {
 421         s.defaultWriteObject();
 422         s.writeInt(count);


 107      */
 108     public StringBuilder(String str) {
 109         super(str.length() + 16);
 110         append(str);
 111     }
 112 
 113     /**
 114      * Constructs a string builder that contains the same characters
 115      * as the specified <code>CharSequence</code>. The initial capacity of
 116      * the string builder is <code>16</code> plus the length of the
 117      * <code>CharSequence</code> argument.
 118      *
 119      * @param      seq   the sequence to copy.
 120      * @throws    NullPointerException if <code>seq</code> is <code>null</code>
 121      */
 122     public StringBuilder(CharSequence seq) {
 123         this(seq.length() + 16);
 124         append(seq);
 125     }
 126 
 127     @Override
 128     public StringBuilder append(Object obj) {
 129         return append(String.valueOf(obj));
 130     }
 131 
 132     @Override
 133     public StringBuilder append(String str) {
 134         super.append(str);
 135         return this;
 136     }
 137 













 138     /**
 139      * Appends the specified <tt>StringBuffer</tt> to this sequence.
 140      * <p>
 141      * The characters of the <tt>StringBuffer</tt> argument are appended,
 142      * in order, to this sequence, increasing the
 143      * length of this sequence by the length of the argument.
 144      * If <tt>sb</tt> is <tt>null</tt>, then the four characters
 145      * <tt>"null"</tt> are appended to this sequence.
 146      * <p>
 147      * Let <i>n</i> be the length of this character sequence just prior to
 148      * execution of the <tt>append</tt> method. Then the character at index
 149      * <i>k</i> in the new character sequence is equal to the character at
 150      * index <i>k</i> in the old character sequence, if <i>k</i> is less than
 151      * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
 152      * in the argument <code>sb</code>.
 153      *
 154      * @param   sb   the <tt>StringBuffer</tt> to append.
 155      * @return  a reference to this object.
 156      */
 157     public StringBuilder append(StringBuffer sb) {
 158         super.append(sb);
 159         return this;
 160     }
 161 
 162     @Override

 163     public StringBuilder append(CharSequence s) {
 164         super.append(s);
 165         return this;







 166     }
 167 
 168     /**
 169      * @throws     IndexOutOfBoundsException {@inheritDoc}
 170      */
 171     @Override
 172     public StringBuilder append(CharSequence s, int start, int end) {
 173         super.append(s, start, end);
 174         return this;
 175     }
 176 
 177     @Override
 178     public StringBuilder append(char[] str) {
 179         super.append(str);
 180         return this;
 181     }
 182 
 183     /**
 184      * @throws IndexOutOfBoundsException {@inheritDoc}
 185      */
 186     @Override
 187     public StringBuilder append(char[] str, int offset, int len) {
 188         super.append(str, offset, len);
 189         return this;
 190     }
 191 
 192     @Override
 193     public StringBuilder append(boolean b) {
 194         super.append(b);
 195         return this;
 196     }
 197 
 198     @Override
 199     public StringBuilder append(char c) {
 200         super.append(c);
 201         return this;
 202     }
 203 
 204     @Override
 205     public StringBuilder append(int i) {
 206         super.append(i);
 207         return this;
 208     }
 209 
 210     @Override
 211     public StringBuilder append(long lng) {
 212         super.append(lng);
 213         return this;
 214     }
 215 
 216     @Override
 217     public StringBuilder append(float f) {
 218         super.append(f);
 219         return this;
 220     }
 221 
 222     @Override
 223     public StringBuilder append(double d) {
 224         super.append(d);
 225         return this;
 226     }
 227 
 228     /**
 229      * @since 1.5
 230      */
 231     @Override
 232     public StringBuilder appendCodePoint(int codePoint) {
 233         super.appendCodePoint(codePoint);
 234         return this;
 235     }
 236 
 237     /**
 238      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 239      */
 240     @Override
 241     public StringBuilder delete(int start, int end) {
 242         super.delete(start, end);
 243         return this;
 244     }
 245 
 246     /**
 247      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 248      */
 249     @Override
 250     public StringBuilder deleteCharAt(int index) {
 251         super.deleteCharAt(index);
 252         return this;
 253     }
 254 
 255     /**
 256      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 257      */
 258     @Override
 259     public StringBuilder replace(int start, int end, String str) {
 260         super.replace(start, end, str);
 261         return this;
 262     }
 263 
 264     /**
 265      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 266      */
 267     @Override
 268     public StringBuilder insert(int index, char[] str, int offset,
 269                                 int len)
 270     {
 271         super.insert(index, str, offset, len);
 272         return this;
 273     }
 274 
 275     /**
 276      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 277      */
 278     @Override
 279     public StringBuilder insert(int offset, Object obj) {
 280             super.insert(offset, obj);
 281             return this;
 282     }
 283 
 284     /**
 285      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 286      */
 287     @Override
 288     public StringBuilder insert(int offset, String str) {
 289         super.insert(offset, str);
 290         return this;
 291     }
 292 
 293     /**
 294      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 295      */
 296     @Override
 297     public StringBuilder insert(int offset, char[] str) {
 298         super.insert(offset, str);
 299         return this;
 300     }
 301 
 302     /**
 303      * @throws IndexOutOfBoundsException {@inheritDoc}
 304      */
 305     @Override
 306     public StringBuilder insert(int dstOffset, CharSequence s) {
 307             super.insert(dstOffset, s);
 308             return this;



 309     }
 310 
 311     /**
 312      * @throws IndexOutOfBoundsException {@inheritDoc}
 313      */
 314     @Override
 315     public StringBuilder insert(int dstOffset, CharSequence s,
 316                                 int start, int end)
 317     {
 318         super.insert(dstOffset, s, start, end);
 319         return this;
 320     }
 321 
 322     /**
 323      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 324      */
 325     @Override
 326     public StringBuilder insert(int offset, boolean b) {
 327         super.insert(offset, b);
 328         return this;
 329     }
 330 
 331     /**
 332      * @throws IndexOutOfBoundsException {@inheritDoc}
 333      */
 334     @Override
 335     public StringBuilder insert(int offset, char c) {
 336         super.insert(offset, c);
 337         return this;
 338     }
 339 
 340     /**
 341      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 342      */
 343     @Override
 344     public StringBuilder insert(int offset, int i) {
 345         super.insert(offset, i);
 346         return this;
 347     }
 348 
 349     /**
 350      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 351      */
 352     @Override
 353     public StringBuilder insert(int offset, long l) {
 354         super.insert(offset, l);
 355         return this;
 356     }
 357 
 358     /**
 359      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 360      */
 361     @Override
 362     public StringBuilder insert(int offset, float f) {
 363         super.insert(offset, f);
 364         return this;
 365     }
 366 
 367     /**
 368      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 369      */
 370     @Override
 371     public StringBuilder insert(int offset, double d) {
 372         super.insert(offset, d);
 373         return this;
 374     }
 375 
 376     /**
 377      * @throws NullPointerException {@inheritDoc}
 378      */
 379     @Override
 380     public int indexOf(String str) {
 381         return super.indexOf(str);
 382     }
 383 
 384     /**
 385      * @throws NullPointerException {@inheritDoc}
 386      */
 387     @Override
 388     public int indexOf(String str, int fromIndex) {
 389         return super.indexOf(str, fromIndex);

 390     }
 391 
 392     /**
 393      * @throws NullPointerException {@inheritDoc}
 394      */
 395     @Override
 396     public int lastIndexOf(String str) {
 397         return super.lastIndexOf(str);
 398     }
 399 
 400     /**
 401      * @throws NullPointerException {@inheritDoc}
 402      */
 403     @Override
 404     public int lastIndexOf(String str, int fromIndex) {
 405         return super.lastIndexOf(str, fromIndex);

 406     }
 407 
 408     @Override
 409     public StringBuilder reverse() {
 410         super.reverse();
 411         return this;
 412     }
 413 
 414     @Override
 415     public String toString() {
 416         // Create a copy, don't share the array
 417         return new String(value, 0, count);
 418     }
 419 
 420     /**
 421      * Save the state of the <tt>StringBuilder</tt> instance to a stream
 422      * (that is, serialize it).
 423      *
 424      * @serialData the number of characters currently stored in the string
 425      *             builder (<tt>int</tt>), followed by the characters in the
 426      *             string builder (<tt>char[]</tt>).   The length of the
 427      *             <tt>char</tt> array may be greater than the number of
 428      *             characters currently stored in the string builder, in which
 429      *             case extra characters are ignored.
 430      */
 431     private void writeObject(java.io.ObjectOutputStream s)
 432         throws java.io.IOException {
 433         s.defaultWriteObject();
 434         s.writeInt(count);