1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.lang; 19 20import java.io.IOException; 21import java.io.ObjectInputStream; 22import java.io.ObjectOutputStream; 23import java.io.Serializable; 24 25/** 26 * A modifiable {@link CharSequence sequence of characters} for use in creating 27 * strings. This class is intended as a direct replacement of 28 * {@link StringBuffer} for non-concurrent use; unlike {@code StringBuffer} this 29 * class is not synchronized. 30 * 31 * <p>For particularly complex string-building needs, consider {@link java.util.Formatter}. 32 * 33 * <p>The majority of the modification methods on this class return {@code 34 * this} so that method calls can be chained together. For example: 35 * {@code new StringBuilder("a").append("b").append("c").toString()}. 36 * 37 * @see CharSequence 38 * @see Appendable 39 * @see StringBuffer 40 * @see String 41 * @see String#format 42 * @since 1.5 43 */ 44public final class StringBuilder extends AbstractStringBuilder implements 45 Appendable, CharSequence, Serializable { 46 47 private static final long serialVersionUID = 4383685877147921099L; 48 49 /** 50 * Constructs an instance with an initial capacity of {@code 16}. 51 * 52 * @see #capacity() 53 */ 54 public StringBuilder() { 55 } 56 57 /** 58 * Constructs an instance with the specified capacity. 59 * 60 * @param capacity 61 * the initial capacity to use. 62 * @throws NegativeArraySizeException 63 * if the specified {@code capacity} is negative. 64 * @see #capacity() 65 */ 66 public StringBuilder(int capacity) { 67 super(capacity); 68 } 69 70 /** 71 * Constructs an instance that's initialized with the contents of the 72 * specified {@code CharSequence}. The capacity of the new builder will be 73 * the length of the {@code CharSequence} plus 16. 74 * 75 * @param seq 76 * the {@code CharSequence} to copy into the builder. 77 * @throws NullPointerException 78 * if {@code seq} is {@code null}. 79 */ 80 public StringBuilder(CharSequence seq) { 81 super(seq.toString()); 82 } 83 84 /** 85 * Constructs an instance that's initialized with the contents of the 86 * specified {@code String}. The capacity of the new builder will be the 87 * length of the {@code String} plus 16. 88 * 89 * @param str 90 * the {@code String} to copy into the builder. 91 * @throws NullPointerException 92 * if {@code str} is {@code null}. 93 */ 94 public StringBuilder(String str) { 95 super(str); 96 } 97 98 /** 99 * Appends the string representation of the specified {@code boolean} value. 100 * The {@code boolean} value is converted to a String according to the rule 101 * defined by {@link String#valueOf(boolean)}. 102 * 103 * @param b 104 * the {@code boolean} value to append. 105 * @return this builder. 106 * @see String#valueOf(boolean) 107 */ 108 public StringBuilder append(boolean b) { 109 append0(b ? "true" : "false"); 110 return this; 111 } 112 113 /** 114 * Appends the string representation of the specified {@code char} value. 115 * The {@code char} value is converted to a string according to the rule 116 * defined by {@link String#valueOf(char)}. 117 * 118 * @param c 119 * the {@code char} value to append. 120 * @return this builder. 121 * @see String#valueOf(char) 122 */ 123 public StringBuilder append(char c) { 124 append0(c); 125 return this; 126 } 127 128 /** 129 * Appends the string representation of the specified {@code int} value. The 130 * {@code int} value is converted to a string according to the rule defined 131 * by {@link String#valueOf(int)}. 132 * 133 * @param i 134 * the {@code int} value to append. 135 * @return this builder. 136 * @see String#valueOf(int) 137 */ 138 public StringBuilder append(int i) { 139 IntegralToString.appendInt(this, i); 140 return this; 141 } 142 143 /** 144 * Appends the string representation of the specified {@code long} value. 145 * The {@code long} value is converted to a string according to the rule 146 * defined by {@link String#valueOf(long)}. 147 * 148 * @param l 149 * the {@code long} value. 150 * @return this builder. 151 * @see String#valueOf(long) 152 */ 153 public StringBuilder append(long l) { 154 IntegralToString.appendLong(this, l); 155 return this; 156 } 157 158 /** 159 * Appends the string representation of the specified {@code float} value. 160 * The {@code float} value is converted to a string according to the rule 161 * defined by {@link String#valueOf(float)}. 162 * 163 * @param f 164 * the {@code float} value to append. 165 * @return this builder. 166 * @see String#valueOf(float) 167 */ 168 public StringBuilder append(float f) { 169 RealToString.getInstance().appendFloat(this, f); 170 return this; 171 } 172 173 /** 174 * Appends the string representation of the specified {@code double} value. 175 * The {@code double} value is converted to a string according to the rule 176 * defined by {@link String#valueOf(double)}. 177 * 178 * @param d 179 * the {@code double} value to append. 180 * @return this builder. 181 * @see String#valueOf(double) 182 */ 183 public StringBuilder append(double d) { 184 RealToString.getInstance().appendDouble(this, d); 185 return this; 186 } 187 188 /** 189 * Appends the string representation of the specified {@code Object}. 190 * The {@code Object} value is converted to a string according to the rule 191 * defined by {@link String#valueOf(Object)}. 192 * 193 * @param obj 194 * the {@code Object} to append. 195 * @return this builder. 196 * @see String#valueOf(Object) 197 */ 198 public StringBuilder append(Object obj) { 199 if (obj == null) { 200 appendNull(); 201 } else { 202 append0(obj.toString()); 203 } 204 return this; 205 } 206 207 /** 208 * Appends the contents of the specified string. If the string is {@code 209 * null}, then the string {@code "null"} is appended. 210 * 211 * @param str 212 * the string to append. 213 * @return this builder. 214 */ 215 public StringBuilder append(String str) { 216 append0(str); 217 return this; 218 } 219 220 /** 221 * Appends the contents of the specified {@code StringBuffer}. If the 222 * StringBuffer is {@code null}, then the string {@code "null"} is 223 * appended. 224 * 225 * @param sb 226 * the {@code StringBuffer} to append. 227 * @return this builder. 228 */ 229 public StringBuilder append(StringBuffer sb) { 230 if (sb == null) { 231 appendNull(); 232 } else { 233 append0(sb.getValue(), 0, sb.length()); 234 } 235 return this; 236 } 237 238 /** 239 * Appends the string representation of the specified {@code char[]}. 240 * The {@code char[]} is converted to a string according to the rule 241 * defined by {@link String#valueOf(char[])}. 242 * 243 * @param chars 244 * the {@code char[]} to append.. 245 * @return this builder. 246 * @see String#valueOf(char[]) 247 */ 248 public StringBuilder append(char[] chars) { 249 append0(chars); 250 return this; 251 } 252 253 /** 254 * Appends the string representation of the specified subset of the {@code 255 * char[]}. The {@code char[]} value is converted to a String according to 256 * the rule defined by {@link String#valueOf(char[],int,int)}. 257 * 258 * @param str 259 * the {@code char[]} to append. 260 * @param offset 261 * the inclusive offset index. 262 * @param len 263 * the number of characters. 264 * @return this builder. 265 * @throws ArrayIndexOutOfBoundsException 266 * if {@code offset} and {@code len} do not specify a valid 267 * subsequence. 268 * @see String#valueOf(char[],int,int) 269 */ 270 public StringBuilder append(char[] str, int offset, int len) { 271 append0(str, offset, len); 272 return this; 273 } 274 275 /** 276 * Appends the string representation of the specified {@code CharSequence}. 277 * If the {@code CharSequence} is {@code null}, then the string {@code 278 * "null"} is appended. 279 * 280 * @param csq 281 * the {@code CharSequence} to append. 282 * @return this builder. 283 */ 284 public StringBuilder append(CharSequence csq) { 285 if (csq == null) { 286 appendNull(); 287 } else { 288 append0(csq, 0, csq.length()); 289 } 290 return this; 291 } 292 293 /** 294 * Appends the string representation of the specified subsequence of the 295 * {@code CharSequence}. If the {@code CharSequence} is {@code null}, then 296 * the string {@code "null"} is used to extract the subsequence from. 297 * 298 * @param csq 299 * the {@code CharSequence} to append. 300 * @param start 301 * the beginning index. 302 * @param end 303 * the ending index. 304 * @return this builder. 305 * @throws IndexOutOfBoundsException 306 * if {@code start} or {@code end} are negative, {@code start} 307 * is greater than {@code end} or {@code end} is greater than 308 * the length of {@code csq}. 309 */ 310 public StringBuilder append(CharSequence csq, int start, int end) { 311 append0(csq, start, end); 312 return this; 313 } 314 315 /** 316 * Appends the encoded Unicode code point. The code point is converted to a 317 * {@code char[]} as defined by {@link Character#toChars(int)}. 318 * 319 * @param codePoint 320 * the Unicode code point to encode and append. 321 * @return this builder. 322 * @see Character#toChars(int) 323 */ 324 public StringBuilder appendCodePoint(int codePoint) { 325 append0(Character.toChars(codePoint)); 326 return this; 327 } 328 329 /** 330 * Deletes a sequence of characters specified by {@code start} and {@code 331 * end}. Shifts any remaining characters to the left. 332 * 333 * @param start 334 * the inclusive start index. 335 * @param end 336 * the exclusive end index. 337 * @return this builder. 338 * @throws StringIndexOutOfBoundsException 339 * if {@code start} is less than zero, greater than the current 340 * length or greater than {@code end}. 341 */ 342 public StringBuilder delete(int start, int end) { 343 delete0(start, end); 344 return this; 345 } 346 347 /** 348 * Deletes the character at the specified index. shifts any remaining 349 * characters to the left. 350 * 351 * @param index 352 * the index of the character to delete. 353 * @return this builder. 354 * @throws StringIndexOutOfBoundsException 355 * if {@code index} is less than zero or is greater than or 356 * equal to the current length. 357 */ 358 public StringBuilder deleteCharAt(int index) { 359 deleteCharAt0(index); 360 return this; 361 } 362 363 /** 364 * Inserts the string representation of the specified {@code boolean} value 365 * at the specified {@code offset}. The {@code boolean} value is converted 366 * to a string according to the rule defined by 367 * {@link String#valueOf(boolean)}. 368 * 369 * @param offset 370 * the index to insert at. 371 * @param b 372 * the {@code boolean} value to insert. 373 * @return this builder. 374 * @throws StringIndexOutOfBoundsException 375 * if {@code offset} is negative or greater than the current 376 * {@code length}. 377 * @see String#valueOf(boolean) 378 */ 379 public StringBuilder insert(int offset, boolean b) { 380 insert0(offset, b ? "true" : "false"); 381 return this; 382 } 383 384 /** 385 * Inserts the string representation of the specified {@code char} value at 386 * the specified {@code offset}. The {@code char} value is converted to a 387 * string according to the rule defined by {@link String#valueOf(char)}. 388 * 389 * @param offset 390 * the index to insert at. 391 * @param c 392 * the {@code char} value to insert. 393 * @return this builder. 394 * @throws IndexOutOfBoundsException 395 * if {@code offset} is negative or greater than the current 396 * {@code length()}. 397 * @see String#valueOf(char) 398 */ 399 public StringBuilder insert(int offset, char c) { 400 insert0(offset, c); 401 return this; 402 } 403 404 /** 405 * Inserts the string representation of the specified {@code int} value at 406 * the specified {@code offset}. The {@code int} value is converted to a 407 * String according to the rule defined by {@link String#valueOf(int)}. 408 * 409 * @param offset 410 * the index to insert at. 411 * @param i 412 * the {@code int} value to insert. 413 * @return this builder. 414 * @throws StringIndexOutOfBoundsException 415 * if {@code offset} is negative or greater than the current 416 * {@code length()}. 417 * @see String#valueOf(int) 418 */ 419 public StringBuilder insert(int offset, int i) { 420 insert0(offset, Integer.toString(i)); 421 return this; 422 } 423 424 /** 425 * Inserts the string representation of the specified {@code long} value at 426 * the specified {@code offset}. The {@code long} value is converted to a 427 * String according to the rule defined by {@link String#valueOf(long)}. 428 * 429 * @param offset 430 * the index to insert at. 431 * @param l 432 * the {@code long} value to insert. 433 * @return this builder. 434 * @throws StringIndexOutOfBoundsException 435 * if {@code offset} is negative or greater than the current 436 * {code length()}. 437 * @see String#valueOf(long) 438 */ 439 public StringBuilder insert(int offset, long l) { 440 insert0(offset, Long.toString(l)); 441 return this; 442 } 443 444 /** 445 * Inserts the string representation of the specified {@code float} value at 446 * the specified {@code offset}. The {@code float} value is converted to a 447 * string according to the rule defined by {@link String#valueOf(float)}. 448 * 449 * @param offset 450 * the index to insert at. 451 * @param f 452 * the {@code float} value to insert. 453 * @return this builder. 454 * @throws StringIndexOutOfBoundsException 455 * if {@code offset} is negative or greater than the current 456 * {@code length()}. 457 * @see String#valueOf(float) 458 */ 459 public StringBuilder insert(int offset, float f) { 460 insert0(offset, Float.toString(f)); 461 return this; 462 } 463 464 /** 465 * Inserts the string representation of the specified {@code double} value 466 * at the specified {@code offset}. The {@code double} value is converted 467 * to a String according to the rule defined by 468 * {@link String#valueOf(double)}. 469 * 470 * @param offset 471 * the index to insert at. 472 * @param d 473 * the {@code double} value to insert. 474 * @return this builder. 475 * @throws StringIndexOutOfBoundsException 476 * if {@code offset} is negative or greater than the current 477 * {@code length()}. 478 * @see String#valueOf(double) 479 */ 480 public StringBuilder insert(int offset, double d) { 481 insert0(offset, Double.toString(d)); 482 return this; 483 } 484 485 /** 486 * Inserts the string representation of the specified {@code Object} at the 487 * specified {@code offset}. The {@code Object} value is converted to a 488 * String according to the rule defined by {@link String#valueOf(Object)}. 489 * 490 * @param offset 491 * the index to insert at. 492 * @param obj 493 * the {@code Object} to insert. 494 * @return this builder. 495 * @throws StringIndexOutOfBoundsException 496 * if {@code offset} is negative or greater than the current 497 * {@code length()}. 498 * @see String#valueOf(Object) 499 */ 500 public StringBuilder insert(int offset, Object obj) { 501 insert0(offset, obj == null ? "null" : obj.toString()); 502 return this; 503 } 504 505 /** 506 * Inserts the specified string at the specified {@code offset}. If the 507 * specified string is null, then the String {@code "null"} is inserted. 508 * 509 * @param offset 510 * the index to insert at. 511 * @param str 512 * the {@code String} to insert. 513 * @return this builder. 514 * @throws StringIndexOutOfBoundsException 515 * if {@code offset} is negative or greater than the current 516 * {@code length()}. 517 */ 518 public StringBuilder insert(int offset, String str) { 519 insert0(offset, str); 520 return this; 521 } 522 523 /** 524 * Inserts the string representation of the specified {@code char[]} at the 525 * specified {@code offset}. The {@code char[]} value is converted to a 526 * String according to the rule defined by {@link String#valueOf(char[])}. 527 * 528 * @param offset 529 * the index to insert at. 530 * @param ch 531 * the {@code char[]} to insert. 532 * @return this builder. 533 * @throws StringIndexOutOfBoundsException 534 * if {@code offset} is negative or greater than the current 535 * {@code length()}. 536 * @see String#valueOf(char[]) 537 */ 538 public StringBuilder insert(int offset, char[] ch) { 539 insert0(offset, ch); 540 return this; 541 } 542 543 /** 544 * Inserts the string representation of the specified subsequence of the 545 * {@code char[]} at the specified {@code offset}. The {@code char[]} value 546 * is converted to a String according to the rule defined by 547 * {@link String#valueOf(char[],int,int)}. 548 * 549 * @param offset 550 * the index to insert at. 551 * @param str 552 * the {@code char[]} to insert. 553 * @param strOffset 554 * the inclusive index. 555 * @param strLen 556 * the number of characters. 557 * @return this builder. 558 * @throws StringIndexOutOfBoundsException 559 * if {@code offset} is negative or greater than the current 560 * {@code length()}, or {@code strOffset} and {@code strLen} do 561 * not specify a valid subsequence. 562 * @see String#valueOf(char[],int,int) 563 */ 564 public StringBuilder insert(int offset, char[] str, int strOffset, 565 int strLen) { 566 insert0(offset, str, strOffset, strLen); 567 return this; 568 } 569 570 /** 571 * Inserts the string representation of the specified {@code CharSequence} 572 * at the specified {@code offset}. The {@code CharSequence} is converted 573 * to a String as defined by {@link CharSequence#toString()}. If {@code s} 574 * is {@code null}, then the String {@code "null"} is inserted. 575 * 576 * @param offset 577 * the index to insert at. 578 * @param s 579 * the {@code CharSequence} to insert. 580 * @return this builder. 581 * @throws IndexOutOfBoundsException 582 * if {@code offset} is negative or greater than the current 583 * {@code length()}. 584 * @see CharSequence#toString() 585 */ 586 public StringBuilder insert(int offset, CharSequence s) { 587 insert0(offset, s == null ? "null" : s.toString()); 588 return this; 589 } 590 591 /** 592 * Inserts the string representation of the specified subsequence of the 593 * {@code CharSequence} at the specified {@code offset}. The {@code 594 * CharSequence} is converted to a String as defined by 595 * {@link CharSequence#subSequence(int, int)}. If the {@code CharSequence} 596 * is {@code null}, then the string {@code "null"} is used to determine the 597 * subsequence. 598 * 599 * @param offset 600 * the index to insert at. 601 * @param s 602 * the {@code CharSequence} to insert. 603 * @param start 604 * the start of the subsequence of the character sequence. 605 * @param end 606 * the end of the subsequence of the character sequence. 607 * @return this builder. 608 * @throws IndexOutOfBoundsException 609 * if {@code offset} is negative or greater than the current 610 * {@code length()}, or {@code start} and {@code end} do not 611 * specify a valid subsequence. 612 * @see CharSequence#subSequence(int, int) 613 */ 614 public StringBuilder insert(int offset, CharSequence s, int start, int end) { 615 insert0(offset, s, start, end); 616 return this; 617 } 618 619 /** 620 * Replaces the specified subsequence in this builder with the specified 621 * string. 622 * 623 * @param start 624 * the inclusive begin index. 625 * @param end 626 * the exclusive end index. 627 * @param string 628 * the replacement string. 629 * @return this builder. 630 * @throws StringIndexOutOfBoundsException 631 * if {@code start} is negative, greater than the current 632 * {@code length()} or greater than {@code end}. 633 * @throws NullPointerException 634 * if {@code str} is {@code null}. 635 */ 636 public StringBuilder replace(int start, int end, String string) { 637 replace0(start, end, string); 638 return this; 639 } 640 641 /** 642 * Reverses the order of characters in this builder. 643 * 644 * @return this buffer. 645 */ 646 public StringBuilder reverse() { 647 reverse0(); 648 return this; 649 } 650 651 /** 652 * Returns the contents of this builder. 653 * 654 * @return the string representation of the data in this builder. 655 */ 656 @Override 657 public String toString() { 658 /* Note: This method is required to workaround a compiler bug 659 * in the RI javac (at least in 1.5.0_06) that will generate a 660 * reference to the non-public AbstractStringBuilder if we don't 661 * override it here. 662 */ 663 return super.toString(); 664 } 665 666 /** 667 * Reads the state of a {@code StringBuilder} from the passed stream and 668 * restores it to this instance. 669 * 670 * @param in 671 * the stream to read the state from. 672 * @throws IOException 673 * if the stream throws it during the read. 674 * @throws ClassNotFoundException 675 * if the stream throws it during the read. 676 */ 677 private void readObject(ObjectInputStream in) throws IOException, 678 ClassNotFoundException { 679 in.defaultReadObject(); 680 int count = in.readInt(); 681 char[] value = (char[]) in.readObject(); 682 set(value, count); 683 } 684 685 /** 686 * Writes the state of this object to the stream passed. 687 * 688 * @param out 689 * the stream to write the state to. 690 * @throws IOException 691 * if the stream throws it during the write. 692 * @serialData {@code int} - the length of this object. {@code char[]} - the 693 * buffer from this object, which may be larger than the length 694 * field. 695 */ 696 private void writeObject(ObjectOutputStream out) throws IOException { 697 out.defaultWriteObject(); 698 out.writeInt(length()); 699 out.writeObject(getValue()); 700 } 701} 702