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     @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);
 435         s.writeObject(value);
 436     }
 437 
 438     /**
 439      * readObject is called to restore the state of the StringBuffer from
 440      * a stream.
 441      */
 442     private void readObject(java.io.ObjectInputStream s)
 443         throws java.io.IOException, ClassNotFoundException {
 444         s.defaultReadObject();
 445         count = s.readInt();
 446         value = (char[]) s.readObject();
 447     }
 448 
 449 }