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