1 /*
   2  * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 
  29 /**
  30  * A mutable sequence of characters.  This class provides an API compatible
  31  * with <code>StringBuffer</code>, but with no guarantee of synchronization.
  32  * This class is designed for use as a drop-in replacement for
  33  * <code>StringBuffer</code> in places where the string buffer was being
  34  * used by a single thread (as is generally the case).   Where possible,
  35  * it is recommended that this class be used in preference to
  36  * <code>StringBuffer</code> as it will be faster under most implementations.
  37  *
  38  * <p>The principal operations on a <code>StringBuilder</code> are the
  39  * <code>append</code> and <code>insert</code> methods, which are
  40  * overloaded so as to accept data of any type. Each effectively
  41  * converts a given datum to a string and then appends or inserts the
  42  * characters of that string to the string builder. The
  43  * <code>append</code> method always adds these characters at the end
  44  * of the builder; the <code>insert</code> method adds the characters at
  45  * a specified point.
  46  * <p>
  47  * For example, if <code>z</code> refers to a string builder object
  48  * whose current contents are "<code>start</code>", then
  49  * the method call <code>z.append("le")</code> would cause the string
  50  * builder to contain "<code>startle</code>", whereas
  51  * <code>z.insert(4, "le")</code> would alter the string builder to
  52  * contain "<code>starlet</code>".
  53  * <p>
  54  * In general, if sb refers to an instance of a <code>StringBuilder</code>,
  55  * then <code>sb.append(x)</code> has the same effect as
  56  * <code>sb.insert(sb.length(),&nbsp;x)</code>.
  57  *
  58  * Every string builder has a capacity. As long as the length of the
  59  * character sequence contained in the string builder does not exceed
  60  * the capacity, it is not necessary to allocate a new internal
  61  * buffer. If the internal buffer overflows, it is automatically made larger.
  62  *
  63  * <p>Instances of <code>StringBuilder</code> are not safe for
  64  * use by multiple threads. If such synchronization is required then it is
  65  * recommended that {@link java.lang.StringBuffer} be used.
  66  *
  67  * @author      Michael McCloskey
  68  * @see         java.lang.StringBuffer
  69  * @see         java.lang.String
  70  * @since       1.5
  71  */
  72 public final class StringBuilder
  73     extends AbstractStringBuilder
  74     implements java.io.Serializable, CharSequence
  75 {
  76 
  77     /** use serialVersionUID for interoperability */
  78     static final long serialVersionUID = 4383685877147921099L;
  79 
  80     /**
  81      * Constructs a string builder with no characters in it and an
  82      * initial capacity of 16 characters.
  83      */
  84     public StringBuilder() {
  85         super(16);
  86     }
  87 
  88     /**
  89      * Constructs a string builder with no characters in it and an
  90      * initial capacity specified by the <code>capacity</code> argument.
  91      *
  92      * @param      capacity  the initial capacity.
  93      * @throws     NegativeArraySizeException  if the <code>capacity</code>
  94      *               argument is less than <code>0</code>.
  95      */
  96     public StringBuilder(int capacity) {
  97         super(capacity);
  98     }
  99 
 100     /**
 101      * Constructs a string builder initialized to the contents of the
 102      * specified string. The initial capacity of the string builder is
 103      * <code>16</code> plus the length of the string argument.
 104      *
 105      * @param   str   the initial contents of the buffer.
 106      * @throws    NullPointerException if <code>str</code> is <code>null</code>
 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 IllegalArgumentException if n < 0  {@inheritDoc}
 189      * @since 1.8
 190      */
 191     public StringBuilder append(int n, CharSequence cs) {
 192         super.append(n, cs);
 193         return this;
 194     }   
 195     
 196     /**
 197      * @throws     IndexOutOfBoundsException {@inheritDoc}
 198      */
 199     public StringBuilder append(CharSequence s, int start, int end) {
 200         super.append(s, start, end);
 201         return this;
 202     }
 203 
 204     public StringBuilder append(char[] str) {
 205         super.append(str);
 206         return this;
 207     }
 208 
 209     /**
 210      * @throws IndexOutOfBoundsException {@inheritDoc}
 211      */
 212     public StringBuilder append(char[] str, int offset, int len) {
 213         super.append(str, offset, len);
 214         return this;
 215     }
 216 
 217     public StringBuilder append(boolean b) {
 218         super.append(b);
 219         return this;
 220     }
 221 
 222     public StringBuilder append(char c) {
 223         super.append(c);
 224         return this;
 225     }
 226 
 227     public StringBuilder append(int i) {
 228         super.append(i);
 229         return this;
 230     }
 231 
 232     public StringBuilder append(long lng) {
 233         super.append(lng);
 234         return this;
 235     }
 236 
 237     public StringBuilder append(float f) {
 238         super.append(f);
 239         return this;
 240     }
 241 
 242     public StringBuilder append(double d) {
 243         super.append(d);
 244         return this;
 245     }
 246 
 247     /**
 248      * @since 1.5
 249      */
 250     public StringBuilder appendCodePoint(int codePoint) {
 251         super.appendCodePoint(codePoint);
 252         return this;
 253     }
 254 
 255     /**
 256      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 257      */
 258     public StringBuilder delete(int start, int end) {
 259         super.delete(start, end);
 260         return this;
 261     }
 262 
 263     /**
 264      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 265      */
 266     public StringBuilder deleteCharAt(int index) {
 267         super.deleteCharAt(index);
 268         return this;
 269     }
 270 
 271     /**
 272      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 273      */
 274     public StringBuilder replace(int start, int end, String str) {
 275         super.replace(start, end, str);
 276         return this;
 277     }
 278 
 279     /**
 280      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 281      */
 282     public StringBuilder insert(int index, char[] str, int offset,
 283                                 int len)
 284     {
 285         super.insert(index, str, offset, len);
 286         return this;
 287     }
 288 
 289     /**
 290      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 291      */
 292     public StringBuilder insert(int offset, Object obj) {
 293         return insert(offset, String.valueOf(obj));
 294     }
 295 
 296     /**
 297      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 298      */
 299     public StringBuilder insert(int offset, String str) {
 300         super.insert(offset, str);
 301         return this;
 302     }
 303 
 304     /**
 305      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 306      */
 307     public StringBuilder insert(int offset, char[] str) {
 308         super.insert(offset, str);
 309         return this;
 310     }
 311 
 312     /**
 313      * @throws IndexOutOfBoundsException {@inheritDoc}
 314      */
 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 
 323     /**
 324      * @throws IndexOutOfBoundsException {@inheritDoc}
 325      */
 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 
 333     /**
 334      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 335      */
 336     public StringBuilder insert(int offset, boolean b) {
 337         super.insert(offset, b);
 338         return this;
 339     }
 340 
 341     /**
 342      * @throws IndexOutOfBoundsException {@inheritDoc}
 343      */
 344     public StringBuilder insert(int offset, char c) {
 345         super.insert(offset, c);
 346         return this;
 347     }
 348 
 349     /**
 350      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 351      */
 352     public StringBuilder insert(int offset, int i) {
 353         return insert(offset, String.valueOf(i));
 354     }
 355 
 356     /**
 357      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 358      */
 359     public StringBuilder insert(int offset, long l) {
 360         return insert(offset, String.valueOf(l));
 361     }
 362 
 363     /**
 364      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 365      */
 366     public StringBuilder insert(int offset, float f) {
 367         return insert(offset, String.valueOf(f));
 368     }
 369 
 370     /**
 371      * @throws StringIndexOutOfBoundsException {@inheritDoc}
 372      */
 373     public StringBuilder insert(int offset, double d) {
 374         return insert(offset, String.valueOf(d));
 375     }
 376 
 377     /**
 378      * @throws NullPointerException {@inheritDoc}
 379      */
 380     public int indexOf(String str) {
 381         return indexOf(str, 0);
 382     }
 383 
 384     /**
 385      * @throws NullPointerException {@inheritDoc}
 386      */
 387     public int indexOf(String str, int fromIndex) {
 388         return String.indexOf(value, 0, count,
 389                               str.toCharArray(), 0, str.length(), fromIndex);
 390     }
 391 
 392     /**
 393      * @throws NullPointerException {@inheritDoc}
 394      */
 395     public int lastIndexOf(String str) {
 396         return lastIndexOf(str, count);
 397     }
 398 
 399     /**
 400      * @throws NullPointerException {@inheritDoc}
 401      */
 402     public int lastIndexOf(String str, int fromIndex) {
 403         return String.lastIndexOf(value, 0, count,
 404                               str.toCharArray(), 0, str.length(), fromIndex);
 405     }
 406 
 407     public StringBuilder reverse() {
 408         super.reverse();
 409         return this;
 410     }
 411 
 412     public String toString() {
 413         // Create a copy, don't share the array
 414         return new String(value, 0, count);
 415     }
 416 
 417     /**
 418      * Save the state of the <tt>StringBuilder</tt> instance to a stream
 419      * (that is, serialize it).
 420      *
 421      * @serialData the number of characters currently stored in the string
 422      *             builder (<tt>int</tt>), followed by the characters in the
 423      *             string builder (<tt>char[]</tt>).   The length of the
 424      *             <tt>char</tt> array may be greater than the number of
 425      *             characters currently stored in the string builder, in which
 426      *             case extra characters are ignored.
 427      */
 428     private void writeObject(java.io.ObjectOutputStream s)
 429         throws java.io.IOException {
 430         s.defaultWriteObject();
 431         s.writeInt(count);
 432         s.writeObject(value);
 433     }
 434 
 435     /**
 436      * readObject is called to restore the state of the StringBuffer from
 437      * a stream.
 438      */
 439     private void readObject(java.io.ObjectInputStream s)
 440         throws java.io.IOException, ClassNotFoundException {
 441         s.defaultReadObject();
 442         count = s.readInt();
 443         value = (char[]) s.readObject();
 444     }
 445 
 446 }