FileDocCategorySizeDatePackage
AbstractStringBuilder.javaAPI DocAndroid 1.5 API28747Wed May 06 22:41:04 BST 2009java.lang

AbstractStringBuilder

public abstract class AbstractStringBuilder extends Object

A modifiable {@link CharSequence sequence of characters} for use in creating and modifying Strings. This class is intended as a base class for {@link StringBuffer} and {@link StringBuilder}.

see
StringBuffer
see
StringBuilder
since
Android 1.0

Fields Summary
static final int
INITIAL_CAPACITY
private char[]
value
private int
count
private boolean
shared
Constructors Summary
AbstractStringBuilder()

        value = new char[INITIAL_CAPACITY];
    
AbstractStringBuilder(int capacity)

        if (capacity < 0) {
            throw new NegativeArraySizeException();
        }
        value = new char[capacity];
    
AbstractStringBuilder(String string)

        count = string.length();
        shared = false;
        value = new char[count + INITIAL_CAPACITY];
        // BEGIN android-changed
        string._getChars(0, count, value, 0);
        // END android-changed
    
Methods Summary
final voidappend0(char[] chars, int start, int length)

        if (chars == null) {
            throw new NullPointerException();
        }
        // start + length could overflow, start/length maybe MaxInt
        if (start >= 0 && 0 <= length && length <= chars.length - start) {
            int newSize = count + length;
            if (newSize > value.length) {
                enlargeBuffer(newSize);
            } else if (shared) {
                value = value.clone();
                shared = false;
            }
            System.arraycopy(chars, start, value, count, length);
            count = newSize;
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    
final voidappend0(char ch)

        if (count == value.length) {
            enlargeBuffer(count + 1);
        }
        if (shared) {
            value = value.clone();
            shared = false;
        }
        value[count++] = ch;
    
final voidappend0(java.lang.String string)

        if (string == null) {
            appendNull();
            return;
        }
        int adding = string.length();
        int newSize = count + adding;
        if (newSize > value.length) {
            enlargeBuffer(newSize);
        } else if (shared) {
            value = value.clone();
            shared = false;
        }
        // BEGIN android-changed
        string._getChars(0, adding, value, count);
        // END android-changed
        count = newSize;
    
final voidappend0(java.lang.CharSequence s, int start, int end)

        if (s == null) {
            s = "null"; //$NON-NLS-1$
        }
        if (start < 0 || end < 0 || start > end || end > s.length()) {
            throw new IndexOutOfBoundsException();
        }

        // BEGIN android-changed
        int adding = end - start;
        int newSize = count + adding;
        if (newSize > value.length) {
            enlargeBuffer(newSize);
        } else if (shared) {
            value = value.clone();
            shared = false;
        }

        if (s instanceof String) {
            // BEGIN android-changed
            ((String) s)._getChars(start, end, value, count);
            // END android-changed
        } else {
            int j = count; // Destination index.
            for (int i = start; i < end; i++) {
                value[j++] = s.charAt(i);
            }
        }

        this.count = newSize;
        // END android-changed
    
final voidappend0(char[] chars)

        int newSize = count + chars.length;
        if (newSize > value.length) {
            enlargeBuffer(newSize);
        } else if (shared) {
            value = value.clone();
            shared = false;
        }
        System.arraycopy(chars, 0, value, count, chars.length);
        count = newSize;
    
final voidappendNull()

        int newSize = count + 4;
        if (newSize > value.length) {
            enlargeBuffer(newSize);
        } else if (shared) {
            value = value.clone();
            shared = false;
        }
        value[count++] = 'n";
        value[count++] = 'u";
        value[count++] = 'l";
        value[count++] = 'l";
    
public intcapacity()
Returns the number of characters that can be held without growing.

return
the capacity
see
#ensureCapacity
see
#length

        return value.length;
    
public charcharAt(int index)
Retrieves the character at the {@code index}.

param
index the index of the character to retrieve.
return
the char value.
throws
IndexOutOfBoundsException if {@code index} is negative or greater than or equal to the current {@link #length()}.

        if (index < 0 || index >= count) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    
public intcodePointAt(int index)
Retrieves the Unicode code point value at the {@code index}.

param
index the index to the {@code char} code unit.
return
the Unicode code point value.
throws
IndexOutOfBoundsException if {@code index} is negative or greater than or equal to {@link #length()}.
see
Character
see
Character#codePointAt(char[], int, int)

        if (index < 0 || index >= count) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return Character.codePointAt(value, index, count);
    
public intcodePointBefore(int index)
Retrieves the Unicode code point value that precedes the {@code index}.

param
index the index to the {@code char} code unit within this object.
return
the Unicode code point value.
throws
IndexOutOfBoundsException if {@code index} is less than 1 or greater than {@link #length()}.
see
Character
see
Character#codePointBefore(char[], int)

        if (index < 1 || index > count) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return Character.codePointBefore(value, index);
    
public intcodePointCount(int beginIndex, int endIndex)

Calculates the number of Unicode code points between {@code beginIndex} and {@code endIndex}.

param
beginIndex the inclusive beginning index of the subsequence.
param
endIndex the exclusive end index of the subsequence.
return
the number of Unicode code points in the subsequence.
throws
IndexOutOfBoundsException if {@code beginIndex} is negative or greater than {@code endIndex} or {@code endIndex} is greater than {@link #length()}.
see
Character
see
Character#codePointCount(char[], int, int)

        if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {
            throw new StringIndexOutOfBoundsException();
        }
        return Character.codePointCount(value, beginIndex, endIndex
                - beginIndex);
    
final voiddelete0(int start, int end)

        if (start >= 0) {
            if (end > count) {
                end = count;
            }
            if (end == start) {
                return;
            }
            if (end > start) {
                int length = count - end;
                if (length > 0) {
                    if (!shared) {
                        System.arraycopy(value, end, value, start, length);
                    } else {
                        char[] newData = new char[value.length];
                        System.arraycopy(value, 0, newData, 0, start);
                        System.arraycopy(value, end, newData, start, length);
                        value = newData;
                        shared = false;
                    }
                }
                count -= end - start;
                return;
            }
        }
        throw new StringIndexOutOfBoundsException();
    
final voiddeleteCharAt0(int location)

        if (0 > location || location >= count) {
            throw new StringIndexOutOfBoundsException(location);
        }
        int length = count - location - 1;
        if (length > 0) {
            if (!shared) {
                System.arraycopy(value, location + 1, value, location, length);
            } else {
                char[] newData = new char[value.length];
                System.arraycopy(value, 0, newData, 0, location);
                System
                        .arraycopy(value, location + 1, newData, location,
                                length);
                value = newData;
                shared = false;
            }
        }
        count--;
    
private voidenlargeBuffer(int min)

        int twice = (value.length << 1) + 2;
        char[] newData = new char[min > twice ? min : twice];
        System.arraycopy(value, 0, newData, 0, count);
        value = newData;
        shared = false;
    
public voidensureCapacity(int min)
Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged. The general policy of this method is that if the {@code minimumCapacity} is larger than the current {@link #capacity()}, then the capacity will be increased to the largest value of either the {@code minimumCapacity} or the current capacity multiplied by two plus two. Although this is the general policy, there is no guarantee that the capacity will change.

param
min the new minimum capacity to set.

        if (min > value.length) {
            enlargeBuffer(min);
        }
    
public voidgetChars(int start, int end, char[] dest, int destStart)
Copies the requested sequence of characters to the {@code char[]} passed starting at {@code destStart}.

param
start the inclusive start index of the characters to copy.
param
end the exclusive end index of the characters to copy.
param
dest the {@code char[]} to copy the characters to.
param
destStart the inclusive start index of {@code dest} to begin copying to.
throws
IndexOutOfBoundsException if the {@code start} is negative, the {@code destStart} is negative, the {@code start} is greater than {@code end}, the {@code end} is greater than the current {@link #length()} or {@code destStart + end - begin} is greater than {@code dest.length}.

        if (start > count || end > count || start > end) {
            throw new StringIndexOutOfBoundsException();
        }
        System.arraycopy(value, start, dest, destStart, end - start);
    
final char[]getValue()


    /*
     * Returns the character array.
     */
       
        return value;
    
public intindexOf(java.lang.String string)
Searches for the first index of the specified character. The search for the character starts at the beginning and moves towards the end.

param
string the string to find.
return
the index of the specified character, -1 if the character isn't found.
see
#lastIndexOf(String)

        return indexOf(string, 0);
    
public intindexOf(java.lang.String subString, int start)
Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the end.

param
subString the string to find.
param
start the starting offset.
return
the index of the specified character, -1 if the character isn't found
see
#lastIndexOf(String,int)

        if (start < 0) {
            start = 0;
        }
        int subCount = subString.length();
        if (subCount > 0) {
            if (subCount + start > count) {
                return -1;
            }
            // TODO optimize charAt to direct array access
            char firstChar = subString.charAt(0);
            while (true) {
                int i = start;
                boolean found = false;
                for (; i < count; i++) {
                    if (value[i] == firstChar) {
                        found = true;
                        break;
                    }
                }
                if (!found || subCount + i > count) {
                    return -1; // handles subCount > count || start >= count
                }
                int o1 = i, o2 = 0;
                while (++o2 < subCount && value[++o1] == subString.charAt(o2)) {
                    // Intentionally empty
                }
                if (o2 == subCount) {
                    return i;
                }
                start = i + 1;
            }
        }
        return (start < count || start == 0) ? start : count;
    
final voidinsert0(int index, char[] chars)

        if (0 > index || index > count) {
            throw new StringIndexOutOfBoundsException(index);
        }
        if (chars.length != 0) {
            move(chars.length, index);
            System.arraycopy(chars, 0, value, index, chars.length);
            count += chars.length;
        }
    
final voidinsert0(int index, char[] chars, int start, int length)

        if (0 <= index && index <= count) {
            // start + length could overflow, start/length maybe MaxInt
            if (start >= 0 && 0 <= length && length <= chars.length - start) {
                if (length != 0) {
                    move(length, index);
                    System.arraycopy(chars, start, value, index, length);
                    count += length;
                }
                return;
            }
            throw new StringIndexOutOfBoundsException("offset " + start
                    + ", len " + length + ", array.length " + chars.length);
        }
        throw new StringIndexOutOfBoundsException(index);
    
final voidinsert0(int index, char ch)

        if (0 > index || index > count) {
            // RI compatible exception type
            throw new ArrayIndexOutOfBoundsException(index);
        }
        move(1, index);
        value[index] = ch;
        count++;
    
final voidinsert0(int index, java.lang.String string)

        if (0 <= index && index <= count) {
            if (string == null) {
                string = "null"; //$NON-NLS-1$
            }
            int min = string.length();
            if (min != 0) {
                move(min, index);
                // BEGIN android-changed
                string._getChars(0, min, value, index);
                // END android-changed
                count += min;
            }
        } else {
            throw new StringIndexOutOfBoundsException(index);
        }
    
final voidinsert0(int index, java.lang.CharSequence s, int start, int end)

        if (s == null) {
            s = "null"; //$NON-NLS-1$
        }
        if (index < 0 || index > count || start < 0 || end < 0 || start > end
                || end > s.length()) {
            throw new IndexOutOfBoundsException();
        }
        insert0(index, s.subSequence(start, end).toString());
    
public intlastIndexOf(java.lang.String string)
Searches for the last index of the specified character. The search for the character starts at the end and moves towards the beginning.

param
string the string to find.
return
the index of the specified character, -1 if the character isn't found.
see
String#lastIndexOf(String)

        return lastIndexOf(string, count);
    
public intlastIndexOf(java.lang.String subString, int start)
Searches for the index of the specified character. The search for the character starts at the specified offset and moves towards the beginning.

param
subString the string to find.
param
start the starting offset.
return
the index of the specified character, -1 if the character isn't found.
see
String#lastIndexOf(String,int)

        int subCount = subString.length();
        if (subCount <= count && start >= 0) {
            if (subCount > 0) {
                if (start > count - subCount) {
                    start = count - subCount; // count and subCount are both
                }
                // >= 1
                // TODO optimize charAt to direct array access
                char firstChar = subString.charAt(0);
                while (true) {
                    int i = start;
                    boolean found = false;
                    for (; i >= 0; --i) {
                        if (value[i] == firstChar) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        return -1;
                    }
                    int o1 = i, o2 = 0;
                    while (++o2 < subCount
                            && value[++o1] == subString.charAt(o2)) {
                        // Intentionally empty
                    }
                    if (o2 == subCount) {
                        return i;
                    }
                    start = i - 1;
                }
            }
            return start < count ? start : count;
        }
        return -1;
    
public intlength()
The current length.

return
the number of characters contained in this instance.

        return count;
    
private voidmove(int size, int index)

        int newSize;
        if (value.length - count >= size) {
            if (!shared) {
                System.arraycopy(value, index, value, index + size, count
                        - index); // index == count case is no-op
                return;
            }
            newSize = value.length;
        } else {
            int a = count + size, b = (value.length << 1) + 2;
            newSize = a > b ? a : b;
        }

        char[] newData = new char[newSize];
        System.arraycopy(value, 0, newData, 0, index);
        // index == count case is no-op
        System.arraycopy(value, index, newData, index + size, count - index);
        value = newData;
        shared = false;
    
public intoffsetByCodePoints(int index, int codePointOffset)

Returns the index that is offset {@code codePointOffset} code points from {@code index}.

param
index the index to calculate the offset from.
param
codePointOffset the number of code points to count.
return
the index that is {@code codePointOffset} code points away from index.
throws
IndexOutOfBoundsException if {@code index} is negative or greater than {@link #length()} or if there aren't enough code points before or after {@code index} to match {@code codePointOffset}.
see
Character
see
Character#offsetByCodePoints(char[], int, int, int, int)

        return Character.offsetByCodePoints(value, 0, count, index,
                codePointOffset);
    
final voidreplace0(int start, int end, java.lang.String string)

        if (start >= 0) {
            if (end > count) {
                end = count;
            }
            if (end > start) {
                int stringLength = string.length();
                int diff = end - start - stringLength;
                if (diff > 0) { // replacing with fewer characters
                    if (!shared) {
                        // index == count case is no-op
                        System.arraycopy(value, end, value, start
                                + stringLength, count - end);
                    } else {
                        char[] newData = new char[value.length];
                        System.arraycopy(value, 0, newData, 0, start);
                        // index == count case is no-op
                        System.arraycopy(value, end, newData, start
                                + stringLength, count - end);
                        value = newData;
                        shared = false;
                    }
                } else if (diff < 0) {
                    // replacing with more characters...need some room
                    move(-diff, end);
                } else if (shared) {
                    value = value.clone();
                    shared = false;
                }
                // BEGIN android-changed
                string._getChars(0, stringLength, value, start);
                // END android-changed
                count -= diff;
                return;
            }
            if (start == end) {
                if (string == null) {
                    throw new NullPointerException();
                }
                insert0(start, string);
                return;
            }
        }
        throw new StringIndexOutOfBoundsException();
    
final voidreverse0()

        if (count < 2) {
            return;
        }
        if (!shared) {
            for (int i = 0, end = count, mid = count / 2; i < mid; i++) {
                char temp = value[--end];
                value[end] = value[i];
                value[i] = temp;
            }
        } else {
            char[] newData = new char[value.length];
            for (int i = 0, end = count; i < count; i++) {
                newData[--end] = value[i];
            }
            value = newData;
            shared = false;
        }
    
final voidset(char[] val, int len)

        if (val == null) {
            val = new char[0];
        }
        if (val.length < len) {
            throw new InvalidObjectException(Msg.getString("K0199")); //$NON-NLS-1$
        }

        shared = false;
        value = val;
        count = len;
    
public voidsetCharAt(int index, char ch)
Sets the character at the {@code index}.

param
index the zero-based index of the character to replace.
param
ch the character to set.
throws
IndexOutOfBoundsException if {@code index} is negative or greater than or equal to the current {@link #length()}.

        if (0 > index || index >= count) {
            throw new StringIndexOutOfBoundsException(index);
        }
        if (shared) {
            value = value.clone();
            shared = false;
        }
        value[index] = ch;
    
public voidsetLength(int length)
Sets the current length to a new value. If the new length is larger than the current length, then the new characters at the end of this object will contain the {@code char} value of {@code \u0000}.

param
length the new length of this StringBuffer.
exception
IndexOutOfBoundsException if {@code length < 0}.
see
#length

        if (length < 0) {
            throw new StringIndexOutOfBoundsException(length);
        }
        if (count < length) {
            if (length > value.length) {
                enlargeBuffer(length);
            } else {
                if (shared) {
                    char[] newData = new char[value.length];
                    System.arraycopy(value, 0, newData, 0, count);
                    value = newData;
                    shared = false;
                } else {
                    Arrays.fill(value, count, length, (char) 0);
                }
            }
        }
        count = length;
    
final char[]shareValue()

        shared = true;
        return value;
    
public java.lang.CharSequencesubSequence(int start, int end)
Returns a {@code CharSequence} of the subsequence from the {@code start} index to the {@code end} index.

param
start the inclusive start index to begin the subsequence.
param
end the exclusive end index to end the subsequence.
return
a CharSequence containing the subsequence.
throws
IndexOutOfBoundsException if {@code start} is negative, greater than {@code end} or if {@code end} is greater than the current {@link #length()}.

        return substring(start, end);
    
public java.lang.Stringsubstring(int start)
Returns the String value of the subsequence from the {@code start} index to the current end.

param
start the inclusive start index to begin the subsequence.
return
a String containing the subsequence.
throws
StringIndexOutOfBoundsException if {@code start} is negative or greater than the current {@link #length()}.

        if (0 <= start && start <= count) {
            if (start == count) {
                return ""; //$NON-NLS-1$
            }

            shared = true;
            return new String(start, count - start, value);
        }
        throw new StringIndexOutOfBoundsException(start);
    
public java.lang.Stringsubstring(int start, int end)
Returns the String value of the subsequence from the {@code start} index to the {@code end} index.

param
start the inclusive start index to begin the subsequence.
param
end the exclusive end index to end the subsequence.
return
a String containing the subsequence.
throws
StringIndexOutOfBoundsException if {@code start} is negative, greater than {@code end} or if {@code end} is greater than the current {@link #length()}.

        if (0 <= start && start <= end && end <= count) {
            if (start == end) {
                return ""; //$NON-NLS-1$
            }

            shared = true;
            return new String(value, start, end - start);
        }
        throw new StringIndexOutOfBoundsException();
    
public java.lang.StringtoString()
Returns the current String representation.

return
a String containing the characters in this instance.

        if (count == 0) {
            return ""; //$NON-NLS-1$
        }

        if (count >= 256 && count <= (value.length >> 1)) {
            return new String(value, 0, count);
        }
        shared = true;
        return new String(0, count, value);
    
public voidtrimToSize()
Trims off any extra capacity beyond the current length. Note, this method is NOT guaranteed to change the capacity of this object.

        if (count < value.length) {
            char[] newValue = new char[count];
            System.arraycopy(value, 0, newValue, 0, count);
            value = newValue;
            shared = false;
        }