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