emul/src/main/java/java/lang/AbstractStringBuilder.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Sep 2012 08:13:32 +0200
branchemul
changeset 61 4b334950499d
parent 58 2ca1bb929895
child 72 06bd18f34608
permissions -rw-r--r--
Removing references to classes that would create too big transitive dependencies
jaroslav@56
     1
/*
jaroslav@56
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@56
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@56
     4
 *
jaroslav@56
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@56
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@56
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@56
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@56
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@56
    10
 *
jaroslav@56
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@56
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@56
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@56
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@56
    15
 * accompanied this code).
jaroslav@56
    16
 *
jaroslav@56
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@56
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@56
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@56
    20
 *
jaroslav@56
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@56
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@56
    23
 * questions.
jaroslav@56
    24
 */
jaroslav@56
    25
jaroslav@56
    26
package java.lang;
jaroslav@56
    27
jaroslav@56
    28
/**
jaroslav@56
    29
 * A mutable sequence of characters.
jaroslav@56
    30
 * <p>
jaroslav@56
    31
 * Implements a modifiable string. At any point in time it contains some
jaroslav@56
    32
 * particular sequence of characters, but the length and content of the
jaroslav@56
    33
 * sequence can be changed through certain method calls.
jaroslav@56
    34
 *
jaroslav@56
    35
 * @author      Michael McCloskey
jaroslav@56
    36
 * @author      Martin Buchholz
jaroslav@56
    37
 * @author      Ulf Zibis
jaroslav@56
    38
 * @since       1.5
jaroslav@56
    39
 */
jaroslav@56
    40
abstract class AbstractStringBuilder implements Appendable, CharSequence {
jaroslav@56
    41
    /**
jaroslav@56
    42
     * The value is used for character storage.
jaroslav@56
    43
     */
jaroslav@56
    44
    char[] value;
jaroslav@56
    45
jaroslav@56
    46
    /**
jaroslav@56
    47
     * The count is the number of characters used.
jaroslav@56
    48
     */
jaroslav@56
    49
    int count;
jaroslav@56
    50
jaroslav@56
    51
    /**
jaroslav@56
    52
     * This no-arg constructor is necessary for serialization of subclasses.
jaroslav@56
    53
     */
jaroslav@56
    54
    AbstractStringBuilder() {
jaroslav@56
    55
    }
jaroslav@56
    56
jaroslav@56
    57
    /**
jaroslav@56
    58
     * Creates an AbstractStringBuilder of the specified capacity.
jaroslav@56
    59
     */
jaroslav@56
    60
    AbstractStringBuilder(int capacity) {
jaroslav@56
    61
        value = new char[capacity];
jaroslav@56
    62
    }
jaroslav@56
    63
jaroslav@56
    64
    /**
jaroslav@56
    65
     * Returns the length (character count).
jaroslav@56
    66
     *
jaroslav@56
    67
     * @return  the length of the sequence of characters currently
jaroslav@56
    68
     *          represented by this object
jaroslav@56
    69
     */
jaroslav@56
    70
    public int length() {
jaroslav@56
    71
        return count;
jaroslav@56
    72
    }
jaroslav@56
    73
jaroslav@56
    74
    /**
jaroslav@56
    75
     * Returns the current capacity. The capacity is the amount of storage
jaroslav@56
    76
     * available for newly inserted characters, beyond which an allocation
jaroslav@56
    77
     * will occur.
jaroslav@56
    78
     *
jaroslav@56
    79
     * @return  the current capacity
jaroslav@56
    80
     */
jaroslav@56
    81
    public int capacity() {
jaroslav@56
    82
        return value.length;
jaroslav@56
    83
    }
jaroslav@56
    84
jaroslav@56
    85
    /**
jaroslav@56
    86
     * Ensures that the capacity is at least equal to the specified minimum.
jaroslav@56
    87
     * If the current capacity is less than the argument, then a new internal
jaroslav@56
    88
     * array is allocated with greater capacity. The new capacity is the
jaroslav@56
    89
     * larger of:
jaroslav@56
    90
     * <ul>
jaroslav@56
    91
     * <li>The <code>minimumCapacity</code> argument.
jaroslav@56
    92
     * <li>Twice the old capacity, plus <code>2</code>.
jaroslav@56
    93
     * </ul>
jaroslav@56
    94
     * If the <code>minimumCapacity</code> argument is nonpositive, this
jaroslav@56
    95
     * method takes no action and simply returns.
jaroslav@56
    96
     *
jaroslav@56
    97
     * @param   minimumCapacity   the minimum desired capacity.
jaroslav@56
    98
     */
jaroslav@56
    99
    public void ensureCapacity(int minimumCapacity) {
jaroslav@56
   100
        if (minimumCapacity > 0)
jaroslav@56
   101
            ensureCapacityInternal(minimumCapacity);
jaroslav@56
   102
    }
jaroslav@56
   103
jaroslav@56
   104
    /**
jaroslav@56
   105
     * This method has the same contract as ensureCapacity, but is
jaroslav@56
   106
     * never synchronized.
jaroslav@56
   107
     */
jaroslav@56
   108
    private void ensureCapacityInternal(int minimumCapacity) {
jaroslav@56
   109
        // overflow-conscious code
jaroslav@56
   110
        if (minimumCapacity - value.length > 0)
jaroslav@56
   111
            expandCapacity(minimumCapacity);
jaroslav@56
   112
    }
jaroslav@56
   113
jaroslav@56
   114
    /**
jaroslav@56
   115
     * This implements the expansion semantics of ensureCapacity with no
jaroslav@56
   116
     * size check or synchronization.
jaroslav@56
   117
     */
jaroslav@56
   118
    void expandCapacity(int minimumCapacity) {
jaroslav@56
   119
        int newCapacity = value.length * 2 + 2;
jaroslav@56
   120
        if (newCapacity - minimumCapacity < 0)
jaroslav@56
   121
            newCapacity = minimumCapacity;
jaroslav@56
   122
        if (newCapacity < 0) {
jaroslav@56
   123
            if (minimumCapacity < 0) // overflow
jaroslav@56
   124
                throw new OutOfMemoryError();
jaroslav@56
   125
            newCapacity = Integer.MAX_VALUE;
jaroslav@56
   126
        }
jaroslav@61
   127
        value = String.copyOf(value, newCapacity);
jaroslav@56
   128
    }
jaroslav@56
   129
jaroslav@56
   130
    /**
jaroslav@56
   131
     * Attempts to reduce storage used for the character sequence.
jaroslav@56
   132
     * If the buffer is larger than necessary to hold its current sequence of
jaroslav@56
   133
     * characters, then it may be resized to become more space efficient.
jaroslav@56
   134
     * Calling this method may, but is not required to, affect the value
jaroslav@56
   135
     * returned by a subsequent call to the {@link #capacity()} method.
jaroslav@56
   136
     */
jaroslav@56
   137
    public void trimToSize() {
jaroslav@56
   138
        if (count < value.length) {
jaroslav@61
   139
            value = String.copyOf(value, count);
jaroslav@56
   140
        }
jaroslav@56
   141
    }
jaroslav@56
   142
jaroslav@56
   143
    /**
jaroslav@56
   144
     * Sets the length of the character sequence.
jaroslav@56
   145
     * The sequence is changed to a new character sequence
jaroslav@56
   146
     * whose length is specified by the argument. For every nonnegative
jaroslav@56
   147
     * index <i>k</i> less than <code>newLength</code>, the character at
jaroslav@56
   148
     * index <i>k</i> in the new character sequence is the same as the
jaroslav@56
   149
     * character at index <i>k</i> in the old sequence if <i>k</i> is less
jaroslav@56
   150
     * than the length of the old character sequence; otherwise, it is the
jaroslav@56
   151
     * null character <code>'&#92;u0000'</code>.
jaroslav@56
   152
     *
jaroslav@56
   153
     * In other words, if the <code>newLength</code> argument is less than
jaroslav@56
   154
     * the current length, the length is changed to the specified length.
jaroslav@56
   155
     * <p>
jaroslav@56
   156
     * If the <code>newLength</code> argument is greater than or equal
jaroslav@56
   157
     * to the current length, sufficient null characters
jaroslav@56
   158
     * (<code>'&#92;u0000'</code>) are appended so that
jaroslav@56
   159
     * length becomes the <code>newLength</code> argument.
jaroslav@56
   160
     * <p>
jaroslav@56
   161
     * The <code>newLength</code> argument must be greater than or equal
jaroslav@56
   162
     * to <code>0</code>.
jaroslav@56
   163
     *
jaroslav@56
   164
     * @param      newLength   the new length
jaroslav@56
   165
     * @throws     IndexOutOfBoundsException  if the
jaroslav@56
   166
     *               <code>newLength</code> argument is negative.
jaroslav@56
   167
     */
jaroslav@56
   168
    public void setLength(int newLength) {
jaroslav@56
   169
        if (newLength < 0)
jaroslav@56
   170
            throw new StringIndexOutOfBoundsException(newLength);
jaroslav@56
   171
        ensureCapacityInternal(newLength);
jaroslav@56
   172
jaroslav@56
   173
        if (count < newLength) {
jaroslav@56
   174
            for (; count < newLength; count++)
jaroslav@56
   175
                value[count] = '\0';
jaroslav@56
   176
        } else {
jaroslav@56
   177
            count = newLength;
jaroslav@56
   178
        }
jaroslav@56
   179
    }
jaroslav@56
   180
jaroslav@56
   181
    /**
jaroslav@56
   182
     * Returns the <code>char</code> value in this sequence at the specified index.
jaroslav@56
   183
     * The first <code>char</code> value is at index <code>0</code>, the next at index
jaroslav@56
   184
     * <code>1</code>, and so on, as in array indexing.
jaroslav@56
   185
     * <p>
jaroslav@56
   186
     * The index argument must be greater than or equal to
jaroslav@56
   187
     * <code>0</code>, and less than the length of this sequence.
jaroslav@56
   188
     *
jaroslav@56
   189
     * <p>If the <code>char</code> value specified by the index is a
jaroslav@56
   190
     * <a href="Character.html#unicode">surrogate</a>, the surrogate
jaroslav@56
   191
     * value is returned.
jaroslav@56
   192
     *
jaroslav@56
   193
     * @param      index   the index of the desired <code>char</code> value.
jaroslav@56
   194
     * @return     the <code>char</code> value at the specified index.
jaroslav@56
   195
     * @throws     IndexOutOfBoundsException  if <code>index</code> is
jaroslav@56
   196
     *             negative or greater than or equal to <code>length()</code>.
jaroslav@56
   197
     */
jaroslav@56
   198
    public char charAt(int index) {
jaroslav@56
   199
        if ((index < 0) || (index >= count))
jaroslav@56
   200
            throw new StringIndexOutOfBoundsException(index);
jaroslav@56
   201
        return value[index];
jaroslav@56
   202
    }
jaroslav@56
   203
jaroslav@56
   204
    /**
jaroslav@56
   205
     * Returns the character (Unicode code point) at the specified
jaroslav@56
   206
     * index. The index refers to <code>char</code> values
jaroslav@56
   207
     * (Unicode code units) and ranges from <code>0</code> to
jaroslav@56
   208
     * {@link #length()}<code> - 1</code>.
jaroslav@56
   209
     *
jaroslav@56
   210
     * <p> If the <code>char</code> value specified at the given index
jaroslav@56
   211
     * is in the high-surrogate range, the following index is less
jaroslav@56
   212
     * than the length of this sequence, and the
jaroslav@56
   213
     * <code>char</code> value at the following index is in the
jaroslav@56
   214
     * low-surrogate range, then the supplementary code point
jaroslav@56
   215
     * corresponding to this surrogate pair is returned. Otherwise,
jaroslav@56
   216
     * the <code>char</code> value at the given index is returned.
jaroslav@56
   217
     *
jaroslav@56
   218
     * @param      index the index to the <code>char</code> values
jaroslav@56
   219
     * @return     the code point value of the character at the
jaroslav@56
   220
     *             <code>index</code>
jaroslav@56
   221
     * @exception  IndexOutOfBoundsException  if the <code>index</code>
jaroslav@56
   222
     *             argument is negative or not less than the length of this
jaroslav@56
   223
     *             sequence.
jaroslav@56
   224
     */
jaroslav@56
   225
    public int codePointAt(int index) {
jaroslav@56
   226
        if ((index < 0) || (index >= count)) {
jaroslav@56
   227
            throw new StringIndexOutOfBoundsException(index);
jaroslav@56
   228
        }
jaroslav@56
   229
        return Character.codePointAt(value, index);
jaroslav@56
   230
    }
jaroslav@56
   231
jaroslav@56
   232
    /**
jaroslav@56
   233
     * Returns the character (Unicode code point) before the specified
jaroslav@56
   234
     * index. The index refers to <code>char</code> values
jaroslav@56
   235
     * (Unicode code units) and ranges from <code>1</code> to {@link
jaroslav@56
   236
     * #length()}.
jaroslav@56
   237
     *
jaroslav@56
   238
     * <p> If the <code>char</code> value at <code>(index - 1)</code>
jaroslav@56
   239
     * is in the low-surrogate range, <code>(index - 2)</code> is not
jaroslav@56
   240
     * negative, and the <code>char</code> value at <code>(index -
jaroslav@56
   241
     * 2)</code> is in the high-surrogate range, then the
jaroslav@56
   242
     * supplementary code point value of the surrogate pair is
jaroslav@56
   243
     * returned. If the <code>char</code> value at <code>index -
jaroslav@56
   244
     * 1</code> is an unpaired low-surrogate or a high-surrogate, the
jaroslav@56
   245
     * surrogate value is returned.
jaroslav@56
   246
     *
jaroslav@56
   247
     * @param     index the index following the code point that should be returned
jaroslav@56
   248
     * @return    the Unicode code point value before the given index.
jaroslav@56
   249
     * @exception IndexOutOfBoundsException if the <code>index</code>
jaroslav@56
   250
     *            argument is less than 1 or greater than the length
jaroslav@56
   251
     *            of this sequence.
jaroslav@56
   252
     */
jaroslav@56
   253
    public int codePointBefore(int index) {
jaroslav@56
   254
        int i = index - 1;
jaroslav@56
   255
        if ((i < 0) || (i >= count)) {
jaroslav@56
   256
            throw new StringIndexOutOfBoundsException(index);
jaroslav@56
   257
        }
jaroslav@56
   258
        return Character.codePointBefore(value, index);
jaroslav@56
   259
    }
jaroslav@56
   260
jaroslav@56
   261
    /**
jaroslav@56
   262
     * Returns the number of Unicode code points in the specified text
jaroslav@56
   263
     * range of this sequence. The text range begins at the specified
jaroslav@56
   264
     * <code>beginIndex</code> and extends to the <code>char</code> at
jaroslav@56
   265
     * index <code>endIndex - 1</code>. Thus the length (in
jaroslav@56
   266
     * <code>char</code>s) of the text range is
jaroslav@56
   267
     * <code>endIndex-beginIndex</code>. Unpaired surrogates within
jaroslav@56
   268
     * this sequence count as one code point each.
jaroslav@56
   269
     *
jaroslav@56
   270
     * @param beginIndex the index to the first <code>char</code> of
jaroslav@56
   271
     * the text range.
jaroslav@56
   272
     * @param endIndex the index after the last <code>char</code> of
jaroslav@56
   273
     * the text range.
jaroslav@56
   274
     * @return the number of Unicode code points in the specified text
jaroslav@56
   275
     * range
jaroslav@56
   276
     * @exception IndexOutOfBoundsException if the
jaroslav@56
   277
     * <code>beginIndex</code> is negative, or <code>endIndex</code>
jaroslav@56
   278
     * is larger than the length of this sequence, or
jaroslav@56
   279
     * <code>beginIndex</code> is larger than <code>endIndex</code>.
jaroslav@56
   280
     */
jaroslav@56
   281
    public int codePointCount(int beginIndex, int endIndex) {
jaroslav@56
   282
        if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
jaroslav@56
   283
            throw new IndexOutOfBoundsException();
jaroslav@56
   284
        }
jaroslav@56
   285
        return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);
jaroslav@56
   286
    }
jaroslav@56
   287
jaroslav@56
   288
    /**
jaroslav@56
   289
     * Returns the index within this sequence that is offset from the
jaroslav@56
   290
     * given <code>index</code> by <code>codePointOffset</code> code
jaroslav@56
   291
     * points. Unpaired surrogates within the text range given by
jaroslav@56
   292
     * <code>index</code> and <code>codePointOffset</code> count as
jaroslav@56
   293
     * one code point each.
jaroslav@56
   294
     *
jaroslav@56
   295
     * @param index the index to be offset
jaroslav@56
   296
     * @param codePointOffset the offset in code points
jaroslav@56
   297
     * @return the index within this sequence
jaroslav@56
   298
     * @exception IndexOutOfBoundsException if <code>index</code>
jaroslav@56
   299
     *   is negative or larger then the length of this sequence,
jaroslav@56
   300
     *   or if <code>codePointOffset</code> is positive and the subsequence
jaroslav@56
   301
     *   starting with <code>index</code> has fewer than
jaroslav@56
   302
     *   <code>codePointOffset</code> code points,
jaroslav@56
   303
     *   or if <code>codePointOffset</code> is negative and the subsequence
jaroslav@56
   304
     *   before <code>index</code> has fewer than the absolute value of
jaroslav@56
   305
     *   <code>codePointOffset</code> code points.
jaroslav@56
   306
     */
jaroslav@56
   307
    public int offsetByCodePoints(int index, int codePointOffset) {
jaroslav@56
   308
        if (index < 0 || index > count) {
jaroslav@56
   309
            throw new IndexOutOfBoundsException();
jaroslav@56
   310
        }
jaroslav@56
   311
        return Character.offsetByCodePointsImpl(value, 0, count,
jaroslav@56
   312
                                                index, codePointOffset);
jaroslav@56
   313
    }
jaroslav@56
   314
jaroslav@56
   315
    /**
jaroslav@56
   316
     * Characters are copied from this sequence into the
jaroslav@56
   317
     * destination character array <code>dst</code>. The first character to
jaroslav@56
   318
     * be copied is at index <code>srcBegin</code>; the last character to
jaroslav@56
   319
     * be copied is at index <code>srcEnd-1</code>. The total number of
jaroslav@56
   320
     * characters to be copied is <code>srcEnd-srcBegin</code>. The
jaroslav@56
   321
     * characters are copied into the subarray of <code>dst</code> starting
jaroslav@56
   322
     * at index <code>dstBegin</code> and ending at index:
jaroslav@56
   323
     * <p><blockquote><pre>
jaroslav@56
   324
     * dstbegin + (srcEnd-srcBegin) - 1
jaroslav@56
   325
     * </pre></blockquote>
jaroslav@56
   326
     *
jaroslav@56
   327
     * @param      srcBegin   start copying at this offset.
jaroslav@56
   328
     * @param      srcEnd     stop copying at this offset.
jaroslav@56
   329
     * @param      dst        the array to copy the data into.
jaroslav@56
   330
     * @param      dstBegin   offset into <code>dst</code>.
jaroslav@56
   331
     * @throws     NullPointerException if <code>dst</code> is
jaroslav@56
   332
     *             <code>null</code>.
jaroslav@56
   333
     * @throws     IndexOutOfBoundsException  if any of the following is true:
jaroslav@56
   334
     *             <ul>
jaroslav@56
   335
     *             <li><code>srcBegin</code> is negative
jaroslav@56
   336
     *             <li><code>dstBegin</code> is negative
jaroslav@56
   337
     *             <li>the <code>srcBegin</code> argument is greater than
jaroslav@56
   338
     *             the <code>srcEnd</code> argument.
jaroslav@56
   339
     *             <li><code>srcEnd</code> is greater than
jaroslav@56
   340
     *             <code>this.length()</code>.
jaroslav@56
   341
     *             <li><code>dstBegin+srcEnd-srcBegin</code> is greater than
jaroslav@56
   342
     *             <code>dst.length</code>
jaroslav@56
   343
     *             </ul>
jaroslav@56
   344
     */
jaroslav@56
   345
    public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
jaroslav@56
   346
    {
jaroslav@56
   347
        if (srcBegin < 0)
jaroslav@56
   348
            throw new StringIndexOutOfBoundsException(srcBegin);
jaroslav@56
   349
        if ((srcEnd < 0) || (srcEnd > count))
jaroslav@56
   350
            throw new StringIndexOutOfBoundsException(srcEnd);
jaroslav@56
   351
        if (srcBegin > srcEnd)
jaroslav@56
   352
            throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
jaroslav@56
   353
        System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
jaroslav@56
   354
    }
jaroslav@56
   355
jaroslav@56
   356
    /**
jaroslav@56
   357
     * The character at the specified index is set to <code>ch</code>. This
jaroslav@56
   358
     * sequence is altered to represent a new character sequence that is
jaroslav@56
   359
     * identical to the old character sequence, except that it contains the
jaroslav@56
   360
     * character <code>ch</code> at position <code>index</code>.
jaroslav@56
   361
     * <p>
jaroslav@56
   362
     * The index argument must be greater than or equal to
jaroslav@56
   363
     * <code>0</code>, and less than the length of this sequence.
jaroslav@56
   364
     *
jaroslav@56
   365
     * @param      index   the index of the character to modify.
jaroslav@56
   366
     * @param      ch      the new character.
jaroslav@56
   367
     * @throws     IndexOutOfBoundsException  if <code>index</code> is
jaroslav@56
   368
     *             negative or greater than or equal to <code>length()</code>.
jaroslav@56
   369
     */
jaroslav@56
   370
    public void setCharAt(int index, char ch) {
jaroslav@56
   371
        if ((index < 0) || (index >= count))
jaroslav@56
   372
            throw new StringIndexOutOfBoundsException(index);
jaroslav@56
   373
        value[index] = ch;
jaroslav@56
   374
    }
jaroslav@56
   375
jaroslav@56
   376
    /**
jaroslav@56
   377
     * Appends the string representation of the {@code Object} argument.
jaroslav@56
   378
     * <p>
jaroslav@56
   379
     * The overall effect is exactly as if the argument were converted
jaroslav@56
   380
     * to a string by the method {@link String#valueOf(Object)},
jaroslav@56
   381
     * and the characters of that string were then
jaroslav@56
   382
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   383
     *
jaroslav@56
   384
     * @param   obj   an {@code Object}.
jaroslav@56
   385
     * @return  a reference to this object.
jaroslav@56
   386
     */
jaroslav@56
   387
    public AbstractStringBuilder append(Object obj) {
jaroslav@56
   388
        return append(String.valueOf(obj));
jaroslav@56
   389
    }
jaroslav@56
   390
jaroslav@56
   391
    /**
jaroslav@56
   392
     * Appends the specified string to this character sequence.
jaroslav@56
   393
     * <p>
jaroslav@56
   394
     * The characters of the {@code String} argument are appended, in
jaroslav@56
   395
     * order, increasing the length of this sequence by the length of the
jaroslav@56
   396
     * argument. If {@code str} is {@code null}, then the four
jaroslav@56
   397
     * characters {@code "null"} are appended.
jaroslav@56
   398
     * <p>
jaroslav@56
   399
     * Let <i>n</i> be the length of this character sequence just prior to
jaroslav@56
   400
     * execution of the {@code append} method. Then the character at
jaroslav@56
   401
     * index <i>k</i> in the new character sequence is equal to the character
jaroslav@56
   402
     * at index <i>k</i> in the old character sequence, if <i>k</i> is less
jaroslav@56
   403
     * than <i>n</i>; otherwise, it is equal to the character at index
jaroslav@56
   404
     * <i>k-n</i> in the argument {@code str}.
jaroslav@56
   405
     *
jaroslav@56
   406
     * @param   str   a string.
jaroslav@56
   407
     * @return  a reference to this object.
jaroslav@56
   408
     */
jaroslav@56
   409
    public AbstractStringBuilder append(String str) {
jaroslav@56
   410
        if (str == null) str = "null";
jaroslav@56
   411
        int len = str.length();
jaroslav@56
   412
        ensureCapacityInternal(count + len);
jaroslav@56
   413
        str.getChars(0, len, value, count);
jaroslav@56
   414
        count += len;
jaroslav@56
   415
        return this;
jaroslav@56
   416
    }
jaroslav@56
   417
jaroslav@56
   418
    // Documentation in subclasses because of synchro difference
jaroslav@56
   419
    public AbstractStringBuilder append(StringBuffer sb) {
jaroslav@56
   420
        if (sb == null)
jaroslav@56
   421
            return append("null");
jaroslav@56
   422
        int len = sb.length();
jaroslav@56
   423
        ensureCapacityInternal(count + len);
jaroslav@56
   424
        sb.getChars(0, len, value, count);
jaroslav@56
   425
        count += len;
jaroslav@56
   426
        return this;
jaroslav@56
   427
    }
jaroslav@56
   428
jaroslav@56
   429
    // Documentation in subclasses because of synchro difference
jaroslav@56
   430
    public AbstractStringBuilder append(CharSequence s) {
jaroslav@56
   431
        if (s == null)
jaroslav@56
   432
            s = "null";
jaroslav@56
   433
        if (s instanceof String)
jaroslav@56
   434
            return this.append((String)s);
jaroslav@56
   435
        if (s instanceof StringBuffer)
jaroslav@56
   436
            return this.append((StringBuffer)s);
jaroslav@56
   437
        return this.append(s, 0, s.length());
jaroslav@56
   438
    }
jaroslav@56
   439
jaroslav@56
   440
    /**
jaroslav@56
   441
     * Appends a subsequence of the specified {@code CharSequence} to this
jaroslav@56
   442
     * sequence.
jaroslav@56
   443
     * <p>
jaroslav@56
   444
     * Characters of the argument {@code s}, starting at
jaroslav@56
   445
     * index {@code start}, are appended, in order, to the contents of
jaroslav@56
   446
     * this sequence up to the (exclusive) index {@code end}. The length
jaroslav@56
   447
     * of this sequence is increased by the value of {@code end - start}.
jaroslav@56
   448
     * <p>
jaroslav@56
   449
     * Let <i>n</i> be the length of this character sequence just prior to
jaroslav@56
   450
     * execution of the {@code append} method. Then the character at
jaroslav@56
   451
     * index <i>k</i> in this character sequence becomes equal to the
jaroslav@56
   452
     * character at index <i>k</i> in this sequence, if <i>k</i> is less than
jaroslav@56
   453
     * <i>n</i>; otherwise, it is equal to the character at index
jaroslav@56
   454
     * <i>k+start-n</i> in the argument {@code s}.
jaroslav@56
   455
     * <p>
jaroslav@56
   456
     * If {@code s} is {@code null}, then this method appends
jaroslav@56
   457
     * characters as if the s parameter was a sequence containing the four
jaroslav@56
   458
     * characters {@code "null"}.
jaroslav@56
   459
     *
jaroslav@56
   460
     * @param   s the sequence to append.
jaroslav@56
   461
     * @param   start   the starting index of the subsequence to be appended.
jaroslav@56
   462
     * @param   end     the end index of the subsequence to be appended.
jaroslav@56
   463
     * @return  a reference to this object.
jaroslav@56
   464
     * @throws     IndexOutOfBoundsException if
jaroslav@56
   465
     *             {@code start} is negative, or
jaroslav@56
   466
     *             {@code start} is greater than {@code end} or
jaroslav@56
   467
     *             {@code end} is greater than {@code s.length()}
jaroslav@56
   468
     */
jaroslav@56
   469
    public AbstractStringBuilder append(CharSequence s, int start, int end) {
jaroslav@56
   470
        if (s == null)
jaroslav@56
   471
            s = "null";
jaroslav@56
   472
        if ((start < 0) || (start > end) || (end > s.length()))
jaroslav@56
   473
            throw new IndexOutOfBoundsException(
jaroslav@56
   474
                "start " + start + ", end " + end + ", s.length() "
jaroslav@56
   475
                + s.length());
jaroslav@56
   476
        int len = end - start;
jaroslav@56
   477
        ensureCapacityInternal(count + len);
jaroslav@56
   478
        for (int i = start, j = count; i < end; i++, j++)
jaroslav@56
   479
            value[j] = s.charAt(i);
jaroslav@56
   480
        count += len;
jaroslav@56
   481
        return this;
jaroslav@56
   482
    }
jaroslav@56
   483
jaroslav@56
   484
    /**
jaroslav@56
   485
     * Appends the string representation of the {@code char} array
jaroslav@56
   486
     * argument to this sequence.
jaroslav@56
   487
     * <p>
jaroslav@56
   488
     * The characters of the array argument are appended, in order, to
jaroslav@56
   489
     * the contents of this sequence. The length of this sequence
jaroslav@56
   490
     * increases by the length of the argument.
jaroslav@56
   491
     * <p>
jaroslav@56
   492
     * The overall effect is exactly as if the argument were converted
jaroslav@56
   493
     * to a string by the method {@link String#valueOf(char[])},
jaroslav@56
   494
     * and the characters of that string were then
jaroslav@56
   495
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   496
     *
jaroslav@56
   497
     * @param   str   the characters to be appended.
jaroslav@56
   498
     * @return  a reference to this object.
jaroslav@56
   499
     */
jaroslav@56
   500
    public AbstractStringBuilder append(char[] str) {
jaroslav@56
   501
        int len = str.length;
jaroslav@56
   502
        ensureCapacityInternal(count + len);
jaroslav@56
   503
        System.arraycopy(str, 0, value, count, len);
jaroslav@56
   504
        count += len;
jaroslav@56
   505
        return this;
jaroslav@56
   506
    }
jaroslav@56
   507
jaroslav@56
   508
    /**
jaroslav@56
   509
     * Appends the string representation of a subarray of the
jaroslav@56
   510
     * {@code char} array argument to this sequence.
jaroslav@56
   511
     * <p>
jaroslav@56
   512
     * Characters of the {@code char} array {@code str}, starting at
jaroslav@56
   513
     * index {@code offset}, are appended, in order, to the contents
jaroslav@56
   514
     * of this sequence. The length of this sequence increases
jaroslav@56
   515
     * by the value of {@code len}.
jaroslav@56
   516
     * <p>
jaroslav@56
   517
     * The overall effect is exactly as if the arguments were converted
jaroslav@56
   518
     * to a string by the method {@link String#valueOf(char[],int,int)},
jaroslav@56
   519
     * and the characters of that string were then
jaroslav@56
   520
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   521
     *
jaroslav@56
   522
     * @param   str      the characters to be appended.
jaroslav@56
   523
     * @param   offset   the index of the first {@code char} to append.
jaroslav@56
   524
     * @param   len      the number of {@code char}s to append.
jaroslav@56
   525
     * @return  a reference to this object.
jaroslav@56
   526
     * @throws IndexOutOfBoundsException
jaroslav@56
   527
     *         if {@code offset < 0} or {@code len < 0}
jaroslav@56
   528
     *         or {@code offset+len > str.length}
jaroslav@56
   529
     */
jaroslav@56
   530
    public AbstractStringBuilder append(char str[], int offset, int len) {
jaroslav@56
   531
        if (len > 0)                // let arraycopy report AIOOBE for len < 0
jaroslav@56
   532
            ensureCapacityInternal(count + len);
jaroslav@56
   533
        System.arraycopy(str, offset, value, count, len);
jaroslav@56
   534
        count += len;
jaroslav@56
   535
        return this;
jaroslav@56
   536
    }
jaroslav@56
   537
jaroslav@56
   538
    /**
jaroslav@56
   539
     * Appends the string representation of the {@code boolean}
jaroslav@56
   540
     * argument to the sequence.
jaroslav@56
   541
     * <p>
jaroslav@56
   542
     * The overall effect is exactly as if the argument were converted
jaroslav@56
   543
     * to a string by the method {@link String#valueOf(boolean)},
jaroslav@56
   544
     * and the characters of that string were then
jaroslav@56
   545
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   546
     *
jaroslav@56
   547
     * @param   b   a {@code boolean}.
jaroslav@56
   548
     * @return  a reference to this object.
jaroslav@56
   549
     */
jaroslav@56
   550
    public AbstractStringBuilder append(boolean b) {
jaroslav@56
   551
        if (b) {
jaroslav@56
   552
            ensureCapacityInternal(count + 4);
jaroslav@56
   553
            value[count++] = 't';
jaroslav@56
   554
            value[count++] = 'r';
jaroslav@56
   555
            value[count++] = 'u';
jaroslav@56
   556
            value[count++] = 'e';
jaroslav@56
   557
        } else {
jaroslav@56
   558
            ensureCapacityInternal(count + 5);
jaroslav@56
   559
            value[count++] = 'f';
jaroslav@56
   560
            value[count++] = 'a';
jaroslav@56
   561
            value[count++] = 'l';
jaroslav@56
   562
            value[count++] = 's';
jaroslav@56
   563
            value[count++] = 'e';
jaroslav@56
   564
        }
jaroslav@56
   565
        return this;
jaroslav@56
   566
    }
jaroslav@56
   567
jaroslav@56
   568
    /**
jaroslav@56
   569
     * Appends the string representation of the {@code char}
jaroslav@56
   570
     * argument to this sequence.
jaroslav@56
   571
     * <p>
jaroslav@56
   572
     * The argument is appended to the contents of this sequence.
jaroslav@56
   573
     * The length of this sequence increases by {@code 1}.
jaroslav@56
   574
     * <p>
jaroslav@56
   575
     * The overall effect is exactly as if the argument were converted
jaroslav@56
   576
     * to a string by the method {@link String#valueOf(char)},
jaroslav@56
   577
     * and the character in that string were then
jaroslav@56
   578
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   579
     *
jaroslav@56
   580
     * @param   c   a {@code char}.
jaroslav@56
   581
     * @return  a reference to this object.
jaroslav@56
   582
     */
jaroslav@56
   583
    public AbstractStringBuilder append(char c) {
jaroslav@56
   584
        ensureCapacityInternal(count + 1);
jaroslav@56
   585
        value[count++] = c;
jaroslav@56
   586
        return this;
jaroslav@56
   587
    }
jaroslav@56
   588
jaroslav@56
   589
    /**
jaroslav@56
   590
     * Appends the string representation of the {@code int}
jaroslav@56
   591
     * argument to this sequence.
jaroslav@56
   592
     * <p>
jaroslav@56
   593
     * The overall effect is exactly as if the argument were converted
jaroslav@56
   594
     * to a string by the method {@link String#valueOf(int)},
jaroslav@56
   595
     * and the characters of that string were then
jaroslav@56
   596
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   597
     *
jaroslav@56
   598
     * @param   i   an {@code int}.
jaroslav@56
   599
     * @return  a reference to this object.
jaroslav@56
   600
     */
jaroslav@56
   601
    public AbstractStringBuilder append(int i) {
jaroslav@56
   602
        if (i == Integer.MIN_VALUE) {
jaroslav@56
   603
            append("-2147483648");
jaroslav@56
   604
            return this;
jaroslav@56
   605
        }
jaroslav@56
   606
        int appendedLength = (i < 0) ? Integer.stringSize(-i) + 1
jaroslav@56
   607
                                     : Integer.stringSize(i);
jaroslav@56
   608
        int spaceNeeded = count + appendedLength;
jaroslav@56
   609
        ensureCapacityInternal(spaceNeeded);
jaroslav@56
   610
        Integer.getChars(i, spaceNeeded, value);
jaroslav@56
   611
        count = spaceNeeded;
jaroslav@56
   612
        return this;
jaroslav@56
   613
    }
jaroslav@56
   614
jaroslav@56
   615
    /**
jaroslav@56
   616
     * Appends the string representation of the {@code long}
jaroslav@56
   617
     * argument to this sequence.
jaroslav@56
   618
     * <p>
jaroslav@56
   619
     * The overall effect is exactly as if the argument were converted
jaroslav@56
   620
     * to a string by the method {@link String#valueOf(long)},
jaroslav@56
   621
     * and the characters of that string were then
jaroslav@56
   622
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   623
     *
jaroslav@56
   624
     * @param   l   a {@code long}.
jaroslav@56
   625
     * @return  a reference to this object.
jaroslav@56
   626
     */
jaroslav@56
   627
    public AbstractStringBuilder append(long l) {
jaroslav@56
   628
        if (l == Long.MIN_VALUE) {
jaroslav@56
   629
            append("-9223372036854775808");
jaroslav@56
   630
            return this;
jaroslav@56
   631
        }
jaroslav@56
   632
        int appendedLength = (l < 0) ? Long.stringSize(-l) + 1
jaroslav@56
   633
                                     : Long.stringSize(l);
jaroslav@56
   634
        int spaceNeeded = count + appendedLength;
jaroslav@56
   635
        ensureCapacityInternal(spaceNeeded);
jaroslav@56
   636
        Long.getChars(l, spaceNeeded, value);
jaroslav@56
   637
        count = spaceNeeded;
jaroslav@56
   638
        return this;
jaroslav@56
   639
    }
jaroslav@56
   640
jaroslav@56
   641
    /**
jaroslav@56
   642
     * Appends the string representation of the {@code float}
jaroslav@56
   643
     * argument to this sequence.
jaroslav@56
   644
     * <p>
jaroslav@56
   645
     * The overall effect is exactly as if the argument were converted
jaroslav@56
   646
     * to a string by the method {@link String#valueOf(float)},
jaroslav@56
   647
     * and the characters of that string were then
jaroslav@56
   648
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   649
     *
jaroslav@56
   650
     * @param   f   a {@code float}.
jaroslav@56
   651
     * @return  a reference to this object.
jaroslav@56
   652
     */
jaroslav@56
   653
    public AbstractStringBuilder append(float f) {
jaroslav@58
   654
        throw new UnsupportedOperationException();
jaroslav@56
   655
    }
jaroslav@56
   656
jaroslav@56
   657
    /**
jaroslav@56
   658
     * Appends the string representation of the {@code double}
jaroslav@56
   659
     * argument to this sequence.
jaroslav@56
   660
     * <p>
jaroslav@56
   661
     * The overall effect is exactly as if the argument were converted
jaroslav@56
   662
     * to a string by the method {@link String#valueOf(double)},
jaroslav@56
   663
     * and the characters of that string were then
jaroslav@56
   664
     * {@link #append(String) appended} to this character sequence.
jaroslav@56
   665
     *
jaroslav@56
   666
     * @param   d   a {@code double}.
jaroslav@56
   667
     * @return  a reference to this object.
jaroslav@56
   668
     */
jaroslav@56
   669
    public AbstractStringBuilder append(double d) {
jaroslav@58
   670
        throw new UnsupportedOperationException();
jaroslav@56
   671
    }
jaroslav@56
   672
jaroslav@56
   673
    /**
jaroslav@56
   674
     * Removes the characters in a substring of this sequence.
jaroslav@56
   675
     * The substring begins at the specified {@code start} and extends to
jaroslav@56
   676
     * the character at index {@code end - 1} or to the end of the
jaroslav@56
   677
     * sequence if no such character exists. If
jaroslav@56
   678
     * {@code start} is equal to {@code end}, no changes are made.
jaroslav@56
   679
     *
jaroslav@56
   680
     * @param      start  The beginning index, inclusive.
jaroslav@56
   681
     * @param      end    The ending index, exclusive.
jaroslav@56
   682
     * @return     This object.
jaroslav@56
   683
     * @throws     StringIndexOutOfBoundsException  if {@code start}
jaroslav@56
   684
     *             is negative, greater than {@code length()}, or
jaroslav@56
   685
     *             greater than {@code end}.
jaroslav@56
   686
     */
jaroslav@56
   687
    public AbstractStringBuilder delete(int start, int end) {
jaroslav@56
   688
        if (start < 0)
jaroslav@56
   689
            throw new StringIndexOutOfBoundsException(start);
jaroslav@56
   690
        if (end > count)
jaroslav@56
   691
            end = count;
jaroslav@56
   692
        if (start > end)
jaroslav@56
   693
            throw new StringIndexOutOfBoundsException();
jaroslav@56
   694
        int len = end - start;
jaroslav@56
   695
        if (len > 0) {
jaroslav@56
   696
            System.arraycopy(value, start+len, value, start, count-end);
jaroslav@56
   697
            count -= len;
jaroslav@56
   698
        }
jaroslav@56
   699
        return this;
jaroslav@56
   700
    }
jaroslav@56
   701
jaroslav@56
   702
    /**
jaroslav@56
   703
     * Appends the string representation of the {@code codePoint}
jaroslav@56
   704
     * argument to this sequence.
jaroslav@56
   705
     *
jaroslav@56
   706
     * <p> The argument is appended to the contents of this sequence.
jaroslav@56
   707
     * The length of this sequence increases by
jaroslav@56
   708
     * {@link Character#charCount(int) Character.charCount(codePoint)}.
jaroslav@56
   709
     *
jaroslav@56
   710
     * <p> The overall effect is exactly as if the argument were
jaroslav@56
   711
     * converted to a {@code char} array by the method
jaroslav@56
   712
     * {@link Character#toChars(int)} and the character in that array
jaroslav@56
   713
     * were then {@link #append(char[]) appended} to this character
jaroslav@56
   714
     * sequence.
jaroslav@56
   715
     *
jaroslav@56
   716
     * @param   codePoint   a Unicode code point
jaroslav@56
   717
     * @return  a reference to this object.
jaroslav@56
   718
     * @exception IllegalArgumentException if the specified
jaroslav@56
   719
     * {@code codePoint} isn't a valid Unicode code point
jaroslav@56
   720
     */
jaroslav@56
   721
    public AbstractStringBuilder appendCodePoint(int codePoint) {
jaroslav@56
   722
        final int count = this.count;
jaroslav@56
   723
jaroslav@56
   724
        if (Character.isBmpCodePoint(codePoint)) {
jaroslav@56
   725
            ensureCapacityInternal(count + 1);
jaroslav@56
   726
            value[count] = (char) codePoint;
jaroslav@56
   727
            this.count = count + 1;
jaroslav@56
   728
        } else if (Character.isValidCodePoint(codePoint)) {
jaroslav@56
   729
            ensureCapacityInternal(count + 2);
jaroslav@56
   730
            Character.toSurrogates(codePoint, value, count);
jaroslav@56
   731
            this.count = count + 2;
jaroslav@56
   732
        } else {
jaroslav@56
   733
            throw new IllegalArgumentException();
jaroslav@56
   734
        }
jaroslav@56
   735
        return this;
jaroslav@56
   736
    }
jaroslav@56
   737
jaroslav@56
   738
    /**
jaroslav@56
   739
     * Removes the <code>char</code> at the specified position in this
jaroslav@56
   740
     * sequence. This sequence is shortened by one <code>char</code>.
jaroslav@56
   741
     *
jaroslav@56
   742
     * <p>Note: If the character at the given index is a supplementary
jaroslav@56
   743
     * character, this method does not remove the entire character. If
jaroslav@56
   744
     * correct handling of supplementary characters is required,
jaroslav@56
   745
     * determine the number of <code>char</code>s to remove by calling
jaroslav@56
   746
     * <code>Character.charCount(thisSequence.codePointAt(index))</code>,
jaroslav@56
   747
     * where <code>thisSequence</code> is this sequence.
jaroslav@56
   748
     *
jaroslav@56
   749
     * @param       index  Index of <code>char</code> to remove
jaroslav@56
   750
     * @return      This object.
jaroslav@56
   751
     * @throws      StringIndexOutOfBoundsException  if the <code>index</code>
jaroslav@56
   752
     *              is negative or greater than or equal to
jaroslav@56
   753
     *              <code>length()</code>.
jaroslav@56
   754
     */
jaroslav@56
   755
    public AbstractStringBuilder deleteCharAt(int index) {
jaroslav@56
   756
        if ((index < 0) || (index >= count))
jaroslav@56
   757
            throw new StringIndexOutOfBoundsException(index);
jaroslav@56
   758
        System.arraycopy(value, index+1, value, index, count-index-1);
jaroslav@56
   759
        count--;
jaroslav@56
   760
        return this;
jaroslav@56
   761
    }
jaroslav@56
   762
jaroslav@56
   763
    /**
jaroslav@56
   764
     * Replaces the characters in a substring of this sequence
jaroslav@56
   765
     * with characters in the specified <code>String</code>. The substring
jaroslav@56
   766
     * begins at the specified <code>start</code> and extends to the character
jaroslav@56
   767
     * at index <code>end - 1</code> or to the end of the
jaroslav@56
   768
     * sequence if no such character exists. First the
jaroslav@56
   769
     * characters in the substring are removed and then the specified
jaroslav@56
   770
     * <code>String</code> is inserted at <code>start</code>. (This
jaroslav@56
   771
     * sequence will be lengthened to accommodate the
jaroslav@56
   772
     * specified String if necessary.)
jaroslav@56
   773
     *
jaroslav@56
   774
     * @param      start    The beginning index, inclusive.
jaroslav@56
   775
     * @param      end      The ending index, exclusive.
jaroslav@56
   776
     * @param      str   String that will replace previous contents.
jaroslav@56
   777
     * @return     This object.
jaroslav@56
   778
     * @throws     StringIndexOutOfBoundsException  if <code>start</code>
jaroslav@56
   779
     *             is negative, greater than <code>length()</code>, or
jaroslav@56
   780
     *             greater than <code>end</code>.
jaroslav@56
   781
     */
jaroslav@56
   782
    public AbstractStringBuilder replace(int start, int end, String str) {
jaroslav@56
   783
        if (start < 0)
jaroslav@56
   784
            throw new StringIndexOutOfBoundsException(start);
jaroslav@56
   785
        if (start > count)
jaroslav@56
   786
            throw new StringIndexOutOfBoundsException("start > length()");
jaroslav@56
   787
        if (start > end)
jaroslav@56
   788
            throw new StringIndexOutOfBoundsException("start > end");
jaroslav@56
   789
jaroslav@56
   790
        if (end > count)
jaroslav@56
   791
            end = count;
jaroslav@56
   792
        int len = str.length();
jaroslav@56
   793
        int newCount = count + len - (end - start);
jaroslav@56
   794
        ensureCapacityInternal(newCount);
jaroslav@56
   795
jaroslav@56
   796
        System.arraycopy(value, end, value, start + len, count - end);
jaroslav@56
   797
        str.getChars(value, start);
jaroslav@56
   798
        count = newCount;
jaroslav@56
   799
        return this;
jaroslav@56
   800
    }
jaroslav@56
   801
jaroslav@56
   802
    /**
jaroslav@56
   803
     * Returns a new <code>String</code> that contains a subsequence of
jaroslav@56
   804
     * characters currently contained in this character sequence. The
jaroslav@56
   805
     * substring begins at the specified index and extends to the end of
jaroslav@56
   806
     * this sequence.
jaroslav@56
   807
     *
jaroslav@56
   808
     * @param      start    The beginning index, inclusive.
jaroslav@56
   809
     * @return     The new string.
jaroslav@56
   810
     * @throws     StringIndexOutOfBoundsException  if <code>start</code> is
jaroslav@56
   811
     *             less than zero, or greater than the length of this object.
jaroslav@56
   812
     */
jaroslav@56
   813
    public String substring(int start) {
jaroslav@56
   814
        return substring(start, count);
jaroslav@56
   815
    }
jaroslav@56
   816
jaroslav@56
   817
    /**
jaroslav@56
   818
     * Returns a new character sequence that is a subsequence of this sequence.
jaroslav@56
   819
     *
jaroslav@56
   820
     * <p> An invocation of this method of the form
jaroslav@56
   821
     *
jaroslav@56
   822
     * <blockquote><pre>
jaroslav@56
   823
     * sb.subSequence(begin,&nbsp;end)</pre></blockquote>
jaroslav@56
   824
     *
jaroslav@56
   825
     * behaves in exactly the same way as the invocation
jaroslav@56
   826
     *
jaroslav@56
   827
     * <blockquote><pre>
jaroslav@56
   828
     * sb.substring(begin,&nbsp;end)</pre></blockquote>
jaroslav@56
   829
     *
jaroslav@56
   830
     * This method is provided so that this class can
jaroslav@56
   831
     * implement the {@link CharSequence} interface. </p>
jaroslav@56
   832
     *
jaroslav@56
   833
     * @param      start   the start index, inclusive.
jaroslav@56
   834
     * @param      end     the end index, exclusive.
jaroslav@56
   835
     * @return     the specified subsequence.
jaroslav@56
   836
     *
jaroslav@56
   837
     * @throws  IndexOutOfBoundsException
jaroslav@56
   838
     *          if <tt>start</tt> or <tt>end</tt> are negative,
jaroslav@56
   839
     *          if <tt>end</tt> is greater than <tt>length()</tt>,
jaroslav@56
   840
     *          or if <tt>start</tt> is greater than <tt>end</tt>
jaroslav@56
   841
     * @spec JSR-51
jaroslav@56
   842
     */
jaroslav@56
   843
    public CharSequence subSequence(int start, int end) {
jaroslav@56
   844
        return substring(start, end);
jaroslav@56
   845
    }
jaroslav@56
   846
jaroslav@56
   847
    /**
jaroslav@56
   848
     * Returns a new <code>String</code> that contains a subsequence of
jaroslav@56
   849
     * characters currently contained in this sequence. The
jaroslav@56
   850
     * substring begins at the specified <code>start</code> and
jaroslav@56
   851
     * extends to the character at index <code>end - 1</code>.
jaroslav@56
   852
     *
jaroslav@56
   853
     * @param      start    The beginning index, inclusive.
jaroslav@56
   854
     * @param      end      The ending index, exclusive.
jaroslav@56
   855
     * @return     The new string.
jaroslav@56
   856
     * @throws     StringIndexOutOfBoundsException  if <code>start</code>
jaroslav@56
   857
     *             or <code>end</code> are negative or greater than
jaroslav@56
   858
     *             <code>length()</code>, or <code>start</code> is
jaroslav@56
   859
     *             greater than <code>end</code>.
jaroslav@56
   860
     */
jaroslav@56
   861
    public String substring(int start, int end) {
jaroslav@56
   862
        if (start < 0)
jaroslav@56
   863
            throw new StringIndexOutOfBoundsException(start);
jaroslav@56
   864
        if (end > count)
jaroslav@56
   865
            throw new StringIndexOutOfBoundsException(end);
jaroslav@56
   866
        if (start > end)
jaroslav@56
   867
            throw new StringIndexOutOfBoundsException(end - start);
jaroslav@56
   868
        return new String(value, start, end - start);
jaroslav@56
   869
    }
jaroslav@56
   870
jaroslav@56
   871
    /**
jaroslav@56
   872
     * Inserts the string representation of a subarray of the {@code str}
jaroslav@56
   873
     * array argument into this sequence. The subarray begins at the
jaroslav@56
   874
     * specified {@code offset} and extends {@code len} {@code char}s.
jaroslav@56
   875
     * The characters of the subarray are inserted into this sequence at
jaroslav@56
   876
     * the position indicated by {@code index}. The length of this
jaroslav@56
   877
     * sequence increases by {@code len} {@code char}s.
jaroslav@56
   878
     *
jaroslav@56
   879
     * @param      index    position at which to insert subarray.
jaroslav@56
   880
     * @param      str       A {@code char} array.
jaroslav@56
   881
     * @param      offset   the index of the first {@code char} in subarray to
jaroslav@56
   882
     *             be inserted.
jaroslav@56
   883
     * @param      len      the number of {@code char}s in the subarray to
jaroslav@56
   884
     *             be inserted.
jaroslav@56
   885
     * @return     This object
jaroslav@56
   886
     * @throws     StringIndexOutOfBoundsException  if {@code index}
jaroslav@56
   887
     *             is negative or greater than {@code length()}, or
jaroslav@56
   888
     *             {@code offset} or {@code len} are negative, or
jaroslav@56
   889
     *             {@code (offset+len)} is greater than
jaroslav@56
   890
     *             {@code str.length}.
jaroslav@56
   891
     */
jaroslav@56
   892
    public AbstractStringBuilder insert(int index, char[] str, int offset,
jaroslav@56
   893
                                        int len)
jaroslav@56
   894
    {
jaroslav@56
   895
        if ((index < 0) || (index > length()))
jaroslav@56
   896
            throw new StringIndexOutOfBoundsException(index);
jaroslav@56
   897
        if ((offset < 0) || (len < 0) || (offset > str.length - len))
jaroslav@56
   898
            throw new StringIndexOutOfBoundsException(
jaroslav@56
   899
                "offset " + offset + ", len " + len + ", str.length "
jaroslav@56
   900
                + str.length);
jaroslav@56
   901
        ensureCapacityInternal(count + len);
jaroslav@56
   902
        System.arraycopy(value, index, value, index + len, count - index);
jaroslav@56
   903
        System.arraycopy(str, offset, value, index, len);
jaroslav@56
   904
        count += len;
jaroslav@56
   905
        return this;
jaroslav@56
   906
    }
jaroslav@56
   907
jaroslav@56
   908
    /**
jaroslav@56
   909
     * Inserts the string representation of the {@code Object}
jaroslav@56
   910
     * argument into this character sequence.
jaroslav@56
   911
     * <p>
jaroslav@56
   912
     * The overall effect is exactly as if the second argument were
jaroslav@56
   913
     * converted to a string by the method {@link String#valueOf(Object)},
jaroslav@56
   914
     * and the characters of that string were then
jaroslav@56
   915
     * {@link #insert(int,String) inserted} into this character
jaroslav@56
   916
     * sequence at the indicated offset.
jaroslav@56
   917
     * <p>
jaroslav@56
   918
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
   919
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
   920
     * of this sequence.
jaroslav@56
   921
     *
jaroslav@56
   922
     * @param      offset   the offset.
jaroslav@56
   923
     * @param      obj      an {@code Object}.
jaroslav@56
   924
     * @return     a reference to this object.
jaroslav@56
   925
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
   926
     */
jaroslav@56
   927
    public AbstractStringBuilder insert(int offset, Object obj) {
jaroslav@56
   928
        return insert(offset, String.valueOf(obj));
jaroslav@56
   929
    }
jaroslav@56
   930
jaroslav@56
   931
    /**
jaroslav@56
   932
     * Inserts the string into this character sequence.
jaroslav@56
   933
     * <p>
jaroslav@56
   934
     * The characters of the {@code String} argument are inserted, in
jaroslav@56
   935
     * order, into this sequence at the indicated offset, moving up any
jaroslav@56
   936
     * characters originally above that position and increasing the length
jaroslav@56
   937
     * of this sequence by the length of the argument. If
jaroslav@56
   938
     * {@code str} is {@code null}, then the four characters
jaroslav@56
   939
     * {@code "null"} are inserted into this sequence.
jaroslav@56
   940
     * <p>
jaroslav@56
   941
     * The character at index <i>k</i> in the new character sequence is
jaroslav@56
   942
     * equal to:
jaroslav@56
   943
     * <ul>
jaroslav@56
   944
     * <li>the character at index <i>k</i> in the old character sequence, if
jaroslav@56
   945
     * <i>k</i> is less than {@code offset}
jaroslav@56
   946
     * <li>the character at index <i>k</i>{@code -offset} in the
jaroslav@56
   947
     * argument {@code str}, if <i>k</i> is not less than
jaroslav@56
   948
     * {@code offset} but is less than {@code offset+str.length()}
jaroslav@56
   949
     * <li>the character at index <i>k</i>{@code -str.length()} in the
jaroslav@56
   950
     * old character sequence, if <i>k</i> is not less than
jaroslav@56
   951
     * {@code offset+str.length()}
jaroslav@56
   952
     * </ul><p>
jaroslav@56
   953
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
   954
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
   955
     * of this sequence.
jaroslav@56
   956
     *
jaroslav@56
   957
     * @param      offset   the offset.
jaroslav@56
   958
     * @param      str      a string.
jaroslav@56
   959
     * @return     a reference to this object.
jaroslav@56
   960
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
   961
     */
jaroslav@56
   962
    public AbstractStringBuilder insert(int offset, String str) {
jaroslav@56
   963
        if ((offset < 0) || (offset > length()))
jaroslav@56
   964
            throw new StringIndexOutOfBoundsException(offset);
jaroslav@56
   965
        if (str == null)
jaroslav@56
   966
            str = "null";
jaroslav@56
   967
        int len = str.length();
jaroslav@56
   968
        ensureCapacityInternal(count + len);
jaroslav@56
   969
        System.arraycopy(value, offset, value, offset + len, count - offset);
jaroslav@56
   970
        str.getChars(value, offset);
jaroslav@56
   971
        count += len;
jaroslav@56
   972
        return this;
jaroslav@56
   973
    }
jaroslav@56
   974
jaroslav@56
   975
    /**
jaroslav@56
   976
     * Inserts the string representation of the {@code char} array
jaroslav@56
   977
     * argument into this sequence.
jaroslav@56
   978
     * <p>
jaroslav@56
   979
     * The characters of the array argument are inserted into the
jaroslav@56
   980
     * contents of this sequence at the position indicated by
jaroslav@56
   981
     * {@code offset}. The length of this sequence increases by
jaroslav@56
   982
     * the length of the argument.
jaroslav@56
   983
     * <p>
jaroslav@56
   984
     * The overall effect is exactly as if the second argument were
jaroslav@56
   985
     * converted to a string by the method {@link String#valueOf(char[])},
jaroslav@56
   986
     * and the characters of that string were then
jaroslav@56
   987
     * {@link #insert(int,String) inserted} into this character
jaroslav@56
   988
     * sequence at the indicated offset.
jaroslav@56
   989
     * <p>
jaroslav@56
   990
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
   991
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
   992
     * of this sequence.
jaroslav@56
   993
     *
jaroslav@56
   994
     * @param      offset   the offset.
jaroslav@56
   995
     * @param      str      a character array.
jaroslav@56
   996
     * @return     a reference to this object.
jaroslav@56
   997
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
   998
     */
jaroslav@56
   999
    public AbstractStringBuilder insert(int offset, char[] str) {
jaroslav@56
  1000
        if ((offset < 0) || (offset > length()))
jaroslav@56
  1001
            throw new StringIndexOutOfBoundsException(offset);
jaroslav@56
  1002
        int len = str.length;
jaroslav@56
  1003
        ensureCapacityInternal(count + len);
jaroslav@56
  1004
        System.arraycopy(value, offset, value, offset + len, count - offset);
jaroslav@56
  1005
        System.arraycopy(str, 0, value, offset, len);
jaroslav@56
  1006
        count += len;
jaroslav@56
  1007
        return this;
jaroslav@56
  1008
    }
jaroslav@56
  1009
jaroslav@56
  1010
    /**
jaroslav@56
  1011
     * Inserts the specified {@code CharSequence} into this sequence.
jaroslav@56
  1012
     * <p>
jaroslav@56
  1013
     * The characters of the {@code CharSequence} argument are inserted,
jaroslav@56
  1014
     * in order, into this sequence at the indicated offset, moving up
jaroslav@56
  1015
     * any characters originally above that position and increasing the length
jaroslav@56
  1016
     * of this sequence by the length of the argument s.
jaroslav@56
  1017
     * <p>
jaroslav@56
  1018
     * The result of this method is exactly the same as if it were an
jaroslav@56
  1019
     * invocation of this object's
jaroslav@56
  1020
     * {@link #insert(int,CharSequence,int,int) insert}(dstOffset, s, 0, s.length())
jaroslav@56
  1021
     * method.
jaroslav@56
  1022
     *
jaroslav@56
  1023
     * <p>If {@code s} is {@code null}, then the four characters
jaroslav@56
  1024
     * {@code "null"} are inserted into this sequence.
jaroslav@56
  1025
     *
jaroslav@56
  1026
     * @param      dstOffset   the offset.
jaroslav@56
  1027
     * @param      s the sequence to be inserted
jaroslav@56
  1028
     * @return     a reference to this object.
jaroslav@56
  1029
     * @throws     IndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
  1030
     */
jaroslav@56
  1031
    public AbstractStringBuilder insert(int dstOffset, CharSequence s) {
jaroslav@56
  1032
        if (s == null)
jaroslav@56
  1033
            s = "null";
jaroslav@56
  1034
        if (s instanceof String)
jaroslav@56
  1035
            return this.insert(dstOffset, (String)s);
jaroslav@56
  1036
        return this.insert(dstOffset, s, 0, s.length());
jaroslav@56
  1037
    }
jaroslav@56
  1038
jaroslav@56
  1039
    /**
jaroslav@56
  1040
     * Inserts a subsequence of the specified {@code CharSequence} into
jaroslav@56
  1041
     * this sequence.
jaroslav@56
  1042
     * <p>
jaroslav@56
  1043
     * The subsequence of the argument {@code s} specified by
jaroslav@56
  1044
     * {@code start} and {@code end} are inserted,
jaroslav@56
  1045
     * in order, into this sequence at the specified destination offset, moving
jaroslav@56
  1046
     * up any characters originally above that position. The length of this
jaroslav@56
  1047
     * sequence is increased by {@code end - start}.
jaroslav@56
  1048
     * <p>
jaroslav@56
  1049
     * The character at index <i>k</i> in this sequence becomes equal to:
jaroslav@56
  1050
     * <ul>
jaroslav@56
  1051
     * <li>the character at index <i>k</i> in this sequence, if
jaroslav@56
  1052
     * <i>k</i> is less than {@code dstOffset}
jaroslav@56
  1053
     * <li>the character at index <i>k</i>{@code +start-dstOffset} in
jaroslav@56
  1054
     * the argument {@code s}, if <i>k</i> is greater than or equal to
jaroslav@56
  1055
     * {@code dstOffset} but is less than {@code dstOffset+end-start}
jaroslav@56
  1056
     * <li>the character at index <i>k</i>{@code -(end-start)} in this
jaroslav@56
  1057
     * sequence, if <i>k</i> is greater than or equal to
jaroslav@56
  1058
     * {@code dstOffset+end-start}
jaroslav@56
  1059
     * </ul><p>
jaroslav@56
  1060
     * The {@code dstOffset} argument must be greater than or equal to
jaroslav@56
  1061
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
  1062
     * of this sequence.
jaroslav@56
  1063
     * <p>The start argument must be nonnegative, and not greater than
jaroslav@56
  1064
     * {@code end}.
jaroslav@56
  1065
     * <p>The end argument must be greater than or equal to
jaroslav@56
  1066
     * {@code start}, and less than or equal to the length of s.
jaroslav@56
  1067
     *
jaroslav@56
  1068
     * <p>If {@code s} is {@code null}, then this method inserts
jaroslav@56
  1069
     * characters as if the s parameter was a sequence containing the four
jaroslav@56
  1070
     * characters {@code "null"}.
jaroslav@56
  1071
     *
jaroslav@56
  1072
     * @param      dstOffset   the offset in this sequence.
jaroslav@56
  1073
     * @param      s       the sequence to be inserted.
jaroslav@56
  1074
     * @param      start   the starting index of the subsequence to be inserted.
jaroslav@56
  1075
     * @param      end     the end index of the subsequence to be inserted.
jaroslav@56
  1076
     * @return     a reference to this object.
jaroslav@56
  1077
     * @throws     IndexOutOfBoundsException  if {@code dstOffset}
jaroslav@56
  1078
     *             is negative or greater than {@code this.length()}, or
jaroslav@56
  1079
     *              {@code start} or {@code end} are negative, or
jaroslav@56
  1080
     *              {@code start} is greater than {@code end} or
jaroslav@56
  1081
     *              {@code end} is greater than {@code s.length()}
jaroslav@56
  1082
     */
jaroslav@56
  1083
     public AbstractStringBuilder insert(int dstOffset, CharSequence s,
jaroslav@56
  1084
                                         int start, int end) {
jaroslav@56
  1085
        if (s == null)
jaroslav@56
  1086
            s = "null";
jaroslav@56
  1087
        if ((dstOffset < 0) || (dstOffset > this.length()))
jaroslav@56
  1088
            throw new IndexOutOfBoundsException("dstOffset "+dstOffset);
jaroslav@56
  1089
        if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))
jaroslav@56
  1090
            throw new IndexOutOfBoundsException(
jaroslav@56
  1091
                "start " + start + ", end " + end + ", s.length() "
jaroslav@56
  1092
                + s.length());
jaroslav@56
  1093
        int len = end - start;
jaroslav@56
  1094
        ensureCapacityInternal(count + len);
jaroslav@56
  1095
        System.arraycopy(value, dstOffset, value, dstOffset + len,
jaroslav@56
  1096
                         count - dstOffset);
jaroslav@56
  1097
        for (int i=start; i<end; i++)
jaroslav@56
  1098
            value[dstOffset++] = s.charAt(i);
jaroslav@56
  1099
        count += len;
jaroslav@56
  1100
        return this;
jaroslav@56
  1101
    }
jaroslav@56
  1102
jaroslav@56
  1103
    /**
jaroslav@56
  1104
     * Inserts the string representation of the {@code boolean}
jaroslav@56
  1105
     * argument into this sequence.
jaroslav@56
  1106
     * <p>
jaroslav@56
  1107
     * The overall effect is exactly as if the second argument were
jaroslav@56
  1108
     * converted to a string by the method {@link String#valueOf(boolean)},
jaroslav@56
  1109
     * and the characters of that string were then
jaroslav@56
  1110
     * {@link #insert(int,String) inserted} into this character
jaroslav@56
  1111
     * sequence at the indicated offset.
jaroslav@56
  1112
     * <p>
jaroslav@56
  1113
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
  1114
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
  1115
     * of this sequence.
jaroslav@56
  1116
     *
jaroslav@56
  1117
     * @param      offset   the offset.
jaroslav@56
  1118
     * @param      b        a {@code boolean}.
jaroslav@56
  1119
     * @return     a reference to this object.
jaroslav@56
  1120
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
  1121
     */
jaroslav@56
  1122
    public AbstractStringBuilder insert(int offset, boolean b) {
jaroslav@56
  1123
        return insert(offset, String.valueOf(b));
jaroslav@56
  1124
    }
jaroslav@56
  1125
jaroslav@56
  1126
    /**
jaroslav@56
  1127
     * Inserts the string representation of the {@code char}
jaroslav@56
  1128
     * argument into this sequence.
jaroslav@56
  1129
     * <p>
jaroslav@56
  1130
     * The overall effect is exactly as if the second argument were
jaroslav@56
  1131
     * converted to a string by the method {@link String#valueOf(char)},
jaroslav@56
  1132
     * and the character in that string were then
jaroslav@56
  1133
     * {@link #insert(int,String) inserted} into this character
jaroslav@56
  1134
     * sequence at the indicated offset.
jaroslav@56
  1135
     * <p>
jaroslav@56
  1136
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
  1137
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
  1138
     * of this sequence.
jaroslav@56
  1139
     *
jaroslav@56
  1140
     * @param      offset   the offset.
jaroslav@56
  1141
     * @param      c        a {@code char}.
jaroslav@56
  1142
     * @return     a reference to this object.
jaroslav@56
  1143
     * @throws     IndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
  1144
     */
jaroslav@56
  1145
    public AbstractStringBuilder insert(int offset, char c) {
jaroslav@56
  1146
        ensureCapacityInternal(count + 1);
jaroslav@56
  1147
        System.arraycopy(value, offset, value, offset + 1, count - offset);
jaroslav@56
  1148
        value[offset] = c;
jaroslav@56
  1149
        count += 1;
jaroslav@56
  1150
        return this;
jaroslav@56
  1151
    }
jaroslav@56
  1152
jaroslav@56
  1153
    /**
jaroslav@56
  1154
     * Inserts the string representation of the second {@code int}
jaroslav@56
  1155
     * argument into this sequence.
jaroslav@56
  1156
     * <p>
jaroslav@56
  1157
     * The overall effect is exactly as if the second argument were
jaroslav@56
  1158
     * converted to a string by the method {@link String#valueOf(int)},
jaroslav@56
  1159
     * and the characters of that string were then
jaroslav@56
  1160
     * {@link #insert(int,String) inserted} into this character
jaroslav@56
  1161
     * sequence at the indicated offset.
jaroslav@56
  1162
     * <p>
jaroslav@56
  1163
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
  1164
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
  1165
     * of this sequence.
jaroslav@56
  1166
     *
jaroslav@56
  1167
     * @param      offset   the offset.
jaroslav@56
  1168
     * @param      i        an {@code int}.
jaroslav@56
  1169
     * @return     a reference to this object.
jaroslav@56
  1170
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
  1171
     */
jaroslav@56
  1172
    public AbstractStringBuilder insert(int offset, int i) {
jaroslav@56
  1173
        return insert(offset, String.valueOf(i));
jaroslav@56
  1174
    }
jaroslav@56
  1175
jaroslav@56
  1176
    /**
jaroslav@56
  1177
     * Inserts the string representation of the {@code long}
jaroslav@56
  1178
     * argument into this sequence.
jaroslav@56
  1179
     * <p>
jaroslav@56
  1180
     * The overall effect is exactly as if the second argument were
jaroslav@56
  1181
     * converted to a string by the method {@link String#valueOf(long)},
jaroslav@56
  1182
     * and the characters of that string were then
jaroslav@56
  1183
     * {@link #insert(int,String) inserted} into this character
jaroslav@56
  1184
     * sequence at the indicated offset.
jaroslav@56
  1185
     * <p>
jaroslav@56
  1186
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
  1187
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
  1188
     * of this sequence.
jaroslav@56
  1189
     *
jaroslav@56
  1190
     * @param      offset   the offset.
jaroslav@56
  1191
     * @param      l        a {@code long}.
jaroslav@56
  1192
     * @return     a reference to this object.
jaroslav@56
  1193
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
  1194
     */
jaroslav@56
  1195
    public AbstractStringBuilder insert(int offset, long l) {
jaroslav@56
  1196
        return insert(offset, String.valueOf(l));
jaroslav@56
  1197
    }
jaroslav@56
  1198
jaroslav@56
  1199
    /**
jaroslav@56
  1200
     * Inserts the string representation of the {@code float}
jaroslav@56
  1201
     * argument into this sequence.
jaroslav@56
  1202
     * <p>
jaroslav@56
  1203
     * The overall effect is exactly as if the second argument were
jaroslav@56
  1204
     * converted to a string by the method {@link String#valueOf(float)},
jaroslav@56
  1205
     * and the characters of that string were then
jaroslav@56
  1206
     * {@link #insert(int,String) inserted} into this character
jaroslav@56
  1207
     * sequence at the indicated offset.
jaroslav@56
  1208
     * <p>
jaroslav@56
  1209
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
  1210
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
  1211
     * of this sequence.
jaroslav@56
  1212
     *
jaroslav@56
  1213
     * @param      offset   the offset.
jaroslav@56
  1214
     * @param      f        a {@code float}.
jaroslav@56
  1215
     * @return     a reference to this object.
jaroslav@56
  1216
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
  1217
     */
jaroslav@56
  1218
    public AbstractStringBuilder insert(int offset, float f) {
jaroslav@56
  1219
        return insert(offset, String.valueOf(f));
jaroslav@56
  1220
    }
jaroslav@56
  1221
jaroslav@56
  1222
    /**
jaroslav@56
  1223
     * Inserts the string representation of the {@code double}
jaroslav@56
  1224
     * argument into this sequence.
jaroslav@56
  1225
     * <p>
jaroslav@56
  1226
     * The overall effect is exactly as if the second argument were
jaroslav@56
  1227
     * converted to a string by the method {@link String#valueOf(double)},
jaroslav@56
  1228
     * and the characters of that string were then
jaroslav@56
  1229
     * {@link #insert(int,String) inserted} into this character
jaroslav@56
  1230
     * sequence at the indicated offset.
jaroslav@56
  1231
     * <p>
jaroslav@56
  1232
     * The {@code offset} argument must be greater than or equal to
jaroslav@56
  1233
     * {@code 0}, and less than or equal to the {@linkplain #length() length}
jaroslav@56
  1234
     * of this sequence.
jaroslav@56
  1235
     *
jaroslav@56
  1236
     * @param      offset   the offset.
jaroslav@56
  1237
     * @param      d        a {@code double}.
jaroslav@56
  1238
     * @return     a reference to this object.
jaroslav@56
  1239
     * @throws     StringIndexOutOfBoundsException  if the offset is invalid.
jaroslav@56
  1240
     */
jaroslav@56
  1241
    public AbstractStringBuilder insert(int offset, double d) {
jaroslav@56
  1242
        return insert(offset, String.valueOf(d));
jaroslav@56
  1243
    }
jaroslav@56
  1244
jaroslav@56
  1245
    /**
jaroslav@56
  1246
     * Returns the index within this string of the first occurrence of the
jaroslav@56
  1247
     * specified substring. The integer returned is the smallest value
jaroslav@56
  1248
     * <i>k</i> such that:
jaroslav@56
  1249
     * <blockquote><pre>
jaroslav@56
  1250
     * this.toString().startsWith(str, <i>k</i>)
jaroslav@56
  1251
     * </pre></blockquote>
jaroslav@56
  1252
     * is <code>true</code>.
jaroslav@56
  1253
     *
jaroslav@56
  1254
     * @param   str   any string.
jaroslav@56
  1255
     * @return  if the string argument occurs as a substring within this
jaroslav@56
  1256
     *          object, then the index of the first character of the first
jaroslav@56
  1257
     *          such substring is returned; if it does not occur as a
jaroslav@56
  1258
     *          substring, <code>-1</code> is returned.
jaroslav@56
  1259
     * @throws  java.lang.NullPointerException if <code>str</code> is
jaroslav@56
  1260
     *          <code>null</code>.
jaroslav@56
  1261
     */
jaroslav@56
  1262
    public int indexOf(String str) {
jaroslav@56
  1263
        return indexOf(str, 0);
jaroslav@56
  1264
    }
jaroslav@56
  1265
jaroslav@56
  1266
    /**
jaroslav@56
  1267
     * Returns the index within this string of the first occurrence of the
jaroslav@56
  1268
     * specified substring, starting at the specified index.  The integer
jaroslav@56
  1269
     * returned is the smallest value <tt>k</tt> for which:
jaroslav@56
  1270
     * <blockquote><pre>
jaroslav@56
  1271
     *     k >= Math.min(fromIndex, str.length()) &&
jaroslav@56
  1272
     *                   this.toString().startsWith(str, k)
jaroslav@56
  1273
     * </pre></blockquote>
jaroslav@56
  1274
     * If no such value of <i>k</i> exists, then -1 is returned.
jaroslav@56
  1275
     *
jaroslav@56
  1276
     * @param   str         the substring for which to search.
jaroslav@56
  1277
     * @param   fromIndex   the index from which to start the search.
jaroslav@56
  1278
     * @return  the index within this string of the first occurrence of the
jaroslav@56
  1279
     *          specified substring, starting at the specified index.
jaroslav@56
  1280
     * @throws  java.lang.NullPointerException if <code>str</code> is
jaroslav@56
  1281
     *            <code>null</code>.
jaroslav@56
  1282
     */
jaroslav@56
  1283
    public int indexOf(String str, int fromIndex) {
jaroslav@56
  1284
        return String.indexOf(value, 0, count,
jaroslav@56
  1285
                              str.toCharArray(), 0, str.length(), fromIndex);
jaroslav@56
  1286
    }
jaroslav@56
  1287
jaroslav@56
  1288
    /**
jaroslav@56
  1289
     * Returns the index within this string of the rightmost occurrence
jaroslav@56
  1290
     * of the specified substring.  The rightmost empty string "" is
jaroslav@56
  1291
     * considered to occur at the index value <code>this.length()</code>.
jaroslav@56
  1292
     * The returned index is the largest value <i>k</i> such that
jaroslav@56
  1293
     * <blockquote><pre>
jaroslav@56
  1294
     * this.toString().startsWith(str, k)
jaroslav@56
  1295
     * </pre></blockquote>
jaroslav@56
  1296
     * is true.
jaroslav@56
  1297
     *
jaroslav@56
  1298
     * @param   str   the substring to search for.
jaroslav@56
  1299
     * @return  if the string argument occurs one or more times as a substring
jaroslav@56
  1300
     *          within this object, then the index of the first character of
jaroslav@56
  1301
     *          the last such substring is returned. If it does not occur as
jaroslav@56
  1302
     *          a substring, <code>-1</code> is returned.
jaroslav@56
  1303
     * @throws  java.lang.NullPointerException  if <code>str</code> is
jaroslav@56
  1304
     *          <code>null</code>.
jaroslav@56
  1305
     */
jaroslav@56
  1306
    public int lastIndexOf(String str) {
jaroslav@56
  1307
        return lastIndexOf(str, count);
jaroslav@56
  1308
    }
jaroslav@56
  1309
jaroslav@56
  1310
    /**
jaroslav@56
  1311
     * Returns the index within this string of the last occurrence of the
jaroslav@56
  1312
     * specified substring. The integer returned is the largest value <i>k</i>
jaroslav@56
  1313
     * such that:
jaroslav@56
  1314
     * <blockquote><pre>
jaroslav@56
  1315
     *     k <= Math.min(fromIndex, str.length()) &&
jaroslav@56
  1316
     *                   this.toString().startsWith(str, k)
jaroslav@56
  1317
     * </pre></blockquote>
jaroslav@56
  1318
     * If no such value of <i>k</i> exists, then -1 is returned.
jaroslav@56
  1319
     *
jaroslav@56
  1320
     * @param   str         the substring to search for.
jaroslav@56
  1321
     * @param   fromIndex   the index to start the search from.
jaroslav@56
  1322
     * @return  the index within this sequence of the last occurrence of the
jaroslav@56
  1323
     *          specified substring.
jaroslav@56
  1324
     * @throws  java.lang.NullPointerException if <code>str</code> is
jaroslav@56
  1325
     *          <code>null</code>.
jaroslav@56
  1326
     */
jaroslav@56
  1327
    public int lastIndexOf(String str, int fromIndex) {
jaroslav@56
  1328
        return String.lastIndexOf(value, 0, count,
jaroslav@56
  1329
                              str.toCharArray(), 0, str.length(), fromIndex);
jaroslav@56
  1330
    }
jaroslav@56
  1331
jaroslav@56
  1332
    /**
jaroslav@56
  1333
     * Causes this character sequence to be replaced by the reverse of
jaroslav@56
  1334
     * the sequence. If there are any surrogate pairs included in the
jaroslav@56
  1335
     * sequence, these are treated as single characters for the
jaroslav@56
  1336
     * reverse operation. Thus, the order of the high-low surrogates
jaroslav@56
  1337
     * is never reversed.
jaroslav@56
  1338
     *
jaroslav@56
  1339
     * Let <i>n</i> be the character length of this character sequence
jaroslav@56
  1340
     * (not the length in <code>char</code> values) just prior to
jaroslav@56
  1341
     * execution of the <code>reverse</code> method. Then the
jaroslav@56
  1342
     * character at index <i>k</i> in the new character sequence is
jaroslav@56
  1343
     * equal to the character at index <i>n-k-1</i> in the old
jaroslav@56
  1344
     * character sequence.
jaroslav@56
  1345
     *
jaroslav@56
  1346
     * <p>Note that the reverse operation may result in producing
jaroslav@56
  1347
     * surrogate pairs that were unpaired low-surrogates and
jaroslav@56
  1348
     * high-surrogates before the operation. For example, reversing
jaroslav@56
  1349
     * "&#92;uDC00&#92;uD800" produces "&#92;uD800&#92;uDC00" which is
jaroslav@56
  1350
     * a valid surrogate pair.
jaroslav@56
  1351
     *
jaroslav@56
  1352
     * @return  a reference to this object.
jaroslav@56
  1353
     */
jaroslav@56
  1354
    public AbstractStringBuilder reverse() {
jaroslav@56
  1355
        boolean hasSurrogate = false;
jaroslav@56
  1356
        int n = count - 1;
jaroslav@56
  1357
        for (int j = (n-1) >> 1; j >= 0; --j) {
jaroslav@56
  1358
            char temp = value[j];
jaroslav@56
  1359
            char temp2 = value[n - j];
jaroslav@56
  1360
            if (!hasSurrogate) {
jaroslav@56
  1361
                hasSurrogate = (temp >= Character.MIN_SURROGATE && temp <= Character.MAX_SURROGATE)
jaroslav@56
  1362
                    || (temp2 >= Character.MIN_SURROGATE && temp2 <= Character.MAX_SURROGATE);
jaroslav@56
  1363
            }
jaroslav@56
  1364
            value[j] = temp2;
jaroslav@56
  1365
            value[n - j] = temp;
jaroslav@56
  1366
        }
jaroslav@56
  1367
        if (hasSurrogate) {
jaroslav@56
  1368
            // Reverse back all valid surrogate pairs
jaroslav@56
  1369
            for (int i = 0; i < count - 1; i++) {
jaroslav@56
  1370
                char c2 = value[i];
jaroslav@56
  1371
                if (Character.isLowSurrogate(c2)) {
jaroslav@56
  1372
                    char c1 = value[i + 1];
jaroslav@56
  1373
                    if (Character.isHighSurrogate(c1)) {
jaroslav@56
  1374
                        value[i++] = c1;
jaroslav@56
  1375
                        value[i] = c2;
jaroslav@56
  1376
                    }
jaroslav@56
  1377
                }
jaroslav@56
  1378
            }
jaroslav@56
  1379
        }
jaroslav@56
  1380
        return this;
jaroslav@56
  1381
    }
jaroslav@56
  1382
jaroslav@56
  1383
    /**
jaroslav@56
  1384
     * Returns a string representing the data in this sequence.
jaroslav@56
  1385
     * A new <code>String</code> object is allocated and initialized to
jaroslav@56
  1386
     * contain the character sequence currently represented by this
jaroslav@56
  1387
     * object. This <code>String</code> is then returned. Subsequent
jaroslav@56
  1388
     * changes to this sequence do not affect the contents of the
jaroslav@56
  1389
     * <code>String</code>.
jaroslav@56
  1390
     *
jaroslav@56
  1391
     * @return  a string representation of this sequence of characters.
jaroslav@56
  1392
     */
jaroslav@56
  1393
    public abstract String toString();
jaroslav@56
  1394
jaroslav@56
  1395
    /**
jaroslav@56
  1396
     * Needed by <tt>String</tt> for the contentEquals method.
jaroslav@56
  1397
     */
jaroslav@56
  1398
    final char[] getValue() {
jaroslav@56
  1399
        return value;
jaroslav@56
  1400
    }
jaroslav@56
  1401
jaroslav@56
  1402
}