StringBuilder.java
001 /** 002 * @(#)StringBuilder.java 1.11 05/11/17 003 * 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 006 */ 007 008 package java.lang; 048 * 049 * @author Michael McCloskey 050 * @version 1.11, 11/17/05 051 * @see java.lang.StringBuffer 052 * @see java.lang.String 053 * @since 1.5 054 */ 055 public final class StringBuilder 056 extends AbstractStringBuilder 057 implements java.io.Serializable, CharSequence 058 { 059 060 /*** use serialVersionUID for interoperability */ 061 static final long serialVersionUID = 4383685877147921099L; 062 067 public StringBuilder() { 068 super(16); 069 } 079 public StringBuilder(int capacity) { 080 super(capacity); 081 } 082 091 public StringBuilder(String str) { 092 super(str.length() + 16); 093 append(str); 094 } 095 105 public StringBuilder(CharSequence seq) { 106 this(seq.length() + 16); 107 append(seq); 108 } 109 114 public StringBuilder append(Object obj) { 115 return append(String.valueOf(obj)); 116 } 117 118 public StringBuilder append(String str) { 119 super.append(str); 120 return this; 121 } 122 123 // Appends the specified string builder to this sequence. 124 private StringBuilder append(StringBuilder sb) { 125 if (sb == null) 126 return append("null"); 127 int len = sb.length(); 128 int newcount = count + len; 129 if (newcount > value.length) 130 expandCapacity(newcount); 131 sb.getChars(0, len, value, count); 132 count = newcount; 133 return this; 134 } 155 public StringBuilder append(StringBuffer sb) { 156 super.append(sb); 157 return this; 158 } 159 163 public StringBuilder append(CharSequence s) { 164 if (s == null) 165 s = "null"; 166 if (s instanceof String) 167 return this.append((String)s); 168 if (s instanceof StringBuffer) 169 return this.append((StringBuffer)s); 170 if (s instanceof StringBuilder) 171 return this.append((StringBuilder)s); 172 return this.append(s, 0, s.length()); 173 } 174 178 public StringBuilder append(CharSequence s, int start, int end) { 179 super.append(s, start, end); 180 return this; 181 } 182 183 public StringBuilder append(char str[]) { 184 super.append(str); 185 return this; 186 } 187 188 public StringBuilder append(char str[], int offset, int len) { 189 super.append(str, offset, len); 190 return this; 191 } 192 197 public StringBuilder append(boolean b) { 198 super.append(b); 199 return this; 200 } 201 202 public StringBuilder append(char c) { 203 super.append(c); 204 return this; 205 } 206 211 public StringBuilder append(int i) { 212 super.append(i); 213 return this; 214 } 215 220 public StringBuilder append(long lng) { 221 super.append(lng); 222 return this; 223 } 224 229 public StringBuilder append(float f) { 230 super.append(f); 231 return this; 232 } 233 238 public StringBuilder append(double d) { 239 super.append(d); 240 return this; 241 } 242 246 public StringBuilder appendCodePoint(int codePoint) { 247 super.appendCodePoint(codePoint); 248 return this; 249 } 250 254 public StringBuilder delete(int start, int end) { 255 super.delete(start, end); 256 return this; 257 } 258 262 public StringBuilder deleteCharAt(int index) { 263 super.deleteCharAt(index); 264 return this; 265 } 266 270 public StringBuilder replace(int start, int end, String str) { 271 super.replace(start, end, str); 272 return this; 273 } 274 278 public StringBuilder insert(int index, char str[], int offset, 279 int len) 280 { 281 super.insert(index, str, offset, len); 282 return this; 283 } 284 291 public StringBuilder insert(int offset, Object obj) { 292 return insert(offset, String.valueOf(obj)); 293 } 299 public StringBuilder insert(int offset, String str) { 300 super.insert(offset, str); 301 return this; 302 } 303 307 public StringBuilder insert(int offset, char str[]) { 308 super.insert(offset, str); 309 return this; 310 } 311 315 public StringBuilder insert(int dstOffset, CharSequence s) { 316 if (s == null) 317 s = "null"; 318 if (s instanceof String) 319 return this.insert(dstOffset, (String)s); 320 return this.insert(dstOffset, s, 0, s.length()); 321 } 322 326 public StringBuilder insert(int dstOffset, CharSequence s, 327 int start, int end) 328 { 329 super.insert(dstOffset, s, start, end); 330 return this; 331 } 332 339 public StringBuilder insert(int offset, boolean b) { 340 super.insert(offset, b); 341 return this; 342 } 343 348 public StringBuilder insert(int offset, char c) { 349 super.insert(offset, c); 350 return this; 351 } 352 359 public StringBuilder insert(int offset, int i) { 360 return insert(offset, String.valueOf(i)); 361 } 362 369 public StringBuilder insert(int offset, long l) { 370 return insert(offset, String.valueOf(l)); 371 } 372 379 public StringBuilder insert(int offset, float f) { 380 return insert(offset, String.valueOf(f)); 381 } 382 389 public StringBuilder insert(int offset, double d) { 390 return insert(offset, String.valueOf(d)); 391 } 392 396 public int indexOf(String str) { 397 return indexOf(str, 0); 398 } 399 403 public int indexOf(String str, int fromIndex) { 404 return String.indexOf(value, 0, count, 405 str.toCharArray(), 0, str.length(), fromIndex); 406 } 407 411 public int lastIndexOf(String str) { 412 return lastIndexOf(str, count); 413 } 414 418 public int lastIndexOf(String str, int fromIndex) { 419 return String.lastIndexOf(value, 0, count, 420 str.toCharArray(), 0, str.length(), fromIndex); 421 } 422 423 public StringBuilder reverse() { 424 super.reverse(); 425 return this; 426 } 427 428 public String toString() { 429 // Create a copy, don't share the array 430 return new String(value, 0, count); 431 } 432 444 private void writeObject(java.io.ObjectOutputStream s) 445 throws java.io.IOException { 446 s.defaultWriteObject(); 447 s.writeInt(count); 448 s.writeObject(value); 449 } 450 455 private void readObject(java.io.ObjectInputStream s) 456 throws java.io.IOException, ClassNotFoundException { 457 s.defaultReadObject(); 458 count = s.readInt(); 459 value = (char[]) s.readObject(); 460 } 461 462 }