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