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