com.plumtree.openfoundation.util
Class XPStringBuilder

java.lang.Object
  extended bycom.plumtree.openfoundation.util.XPStringBuilder

public final class XPStringBuilder
extends java.lang.Object

A string builder implements a mutable sequence of characters. A string builder is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String builders are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

String builders are used by the compiler to implement the binary string concatenation operator +. For example, the code:

     x = "a" + 4 + "c"
 

is compiled to the equivalent of:

     x = new StringBuffer().append("a").append(4).append("c")
                           .toString()
 
which creates a new string builder (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.

The principal operations on a Stringbuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.

For example, if z refers to a string buffer object whose current contents are "start", then the method call z.append("le") would cause the string buffer to contain "startle", whereas z.insert(4, "le") would alter the string buffer to contain "starlet".

In general, if sb refers to an instance of a Stringbuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Every string builder has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.


Constructor Summary
XPStringBuilder()
          Initializes an instance of XPStringBuilder with no characters in it and the capacity of 16 characters.
XPStringBuilder(int capacity)
          Initializes an instance of XPStringBuilder with the given capacity of characters.
XPStringBuilder(java.lang.String str)
          Initializes an instance of XPStringBuilder using the specified string.
XPStringBuilder(java.lang.StringBuffer stringBuffer)
          Copy constructor that initializes the XPStringBuilder with the given StringBuilder object.
 
Method Summary
 XPStringBuilder Append(boolean _value)
          Appends the string representation of a specified Boolean value to the end of this instance.
 XPStringBuilder Append(char _value)
          Appends the string representation of a specified Unicode character to the end of this instance.
 XPStringBuilder Append(char[] _value)
          Appends the string representation of the Unicode characters in a specified array to the end of this instance.
 XPStringBuilder Append(char[] _value, int offset, int length)
          Appends the string representation of a specified subarray of Unicode characters to the end of this instance.
 XPStringBuilder Append(double _value)
          Appends the string representation of a specified double-precision floating-point number to the end of this instance.
 XPStringBuilder Append(int _value)
          Appends the string representation of a specified 32-bit signed integer to the end of this instance.
 XPStringBuilder Append(long _value)
          Appends the string representation of a specified 64-bit signed integer to the end of this instance.
 XPStringBuilder Append(java.lang.Object _value)
          Appends the string representation of a specified object to the end of this instance.
 XPStringBuilder Append(java.lang.String _value)
          Appends a copy of the specified string to the end of this instance.
 XPStringBuilder Append(java.lang.String _value, int offset, int length)
          Appends a copy of a specified substring to the end of this instance.
 char CharAt(int index)
          Returns the character at the specified position in this instance.
 int EnsureCapacity(int minimumCapacity)
          Ensures that the capacity of this instance of XPStringBuilder is at least the specified value.
 int GetCapacity()
          Returns the current capacity of the String buffer.
 int GetLength()
          Returns the length (character count) of this string buffer.
 XPStringBuilder Insert(int offset, boolean _value)
          Inserts the string representation of the boolean argument into this string buffer.
 XPStringBuilder Insert(int offset, char _value)
          Inserts the string representation of the char argument into this string buffer.
 XPStringBuilder Insert(int offset, char[] _value)
          Inserts the string representation of the char array argument into this string buffer.
 XPStringBuilder Insert(int index, char[] _value, int offset, int length)
          Inserts the string representation of a subarray of the str array argument into this string buffer.
 XPStringBuilder Insert(int offset, double _value)
          Inserts the string representation of the double argument into this string buffer.
 XPStringBuilder Insert(int offset, int _value)
          Inserts the string representation of the second int argument into this string buffer.
 XPStringBuilder Insert(int offset, long _value)
          Inserts the string representation of the long argument into this XPStringBuilder.
 XPStringBuilder Insert(int offset, java.lang.Object _value)
          Inserts the string representation of the Object argument into this XPStringBuilder.
 XPStringBuilder Insert(int offset, java.lang.String _value)
          Inserts the string into this XPStringBuilder.
 XPStringBuilder Insert(int offset, java.lang.String _value, int index, int length)
          Inserts the string representation of a subarray of the str array argument into this XPStringBuilder.
 XPStringBuilder Remove(int start, int end)
          Removes the characters in a substring of this XPStringBuilder.
 XPStringBuilder RemoveCharAt(int start)
          Removes the character at the specified position in this StringBuffer (shortening the StringBuffer by one character).
 XPStringBuilder Replace(int start, int end, java.lang.String _value)
          Replaces the characters in a substring of this XPStringBuilder with characters in the specified String.
 XPStringBuilder Reverse()
          The character sequence contained in this XPStringBuilder is replaced by the reverse of the sequence.
 void SetCharAt(int index, char ch)
          The character at the specified index of this XPStringBuilder is set to ch.
 void SetLength(int length)
          Sets the length of this XPStringBuilder.
 java.lang.String Substring(int start)
          Returns a new String that contains a subsequence of characters currently contained in this XPStringBuilder.The substring begins at the specified index and extends to the end of the XPStringBuilder.
 java.lang.String Substring(int start, int end)
          Returns a new String that contains a subsequence of characters currently contained in this XPStringBuilder.
 java.lang.String toString()
          Converts to a string representing the data in this XPStringBuilder.
 java.lang.String ToString()
          Converts to a string representing the data in this XPStringBuilder.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XPStringBuilder

public XPStringBuilder()
Initializes an instance of XPStringBuilder with no characters in it and the capacity of 16 characters.


XPStringBuilder

public XPStringBuilder(int capacity)
Initializes an instance of XPStringBuilder with the given capacity of characters.

Parameters:
capacity - - the initial capacity.

XPStringBuilder

public XPStringBuilder(java.lang.String str)
Initializes an instance of XPStringBuilder using the specified string.

Parameters:
str - - the initial content of the XPStringBuilder

XPStringBuilder

public XPStringBuilder(java.lang.StringBuffer stringBuffer)
Copy constructor that initializes the XPStringBuilder with the given StringBuilder object.

Parameters:
stringBuffer - - an instance of StringBuffer
Method Detail

Append

public XPStringBuilder Append(boolean _value)
Appends the string representation of a specified Boolean value to the end of this instance.

Parameters:
_value - - A Boolean value
Returns:
A reference to this instance after the append operation has occurred

Append

public XPStringBuilder Append(char _value)
Appends the string representation of a specified Unicode character to the end of this instance.

Parameters:
_value - - A Unicode character
Returns:
A reference to this instance after the append operation has occurred

Append

public XPStringBuilder Append(char[] _value)
Appends the string representation of the Unicode characters in a specified array to the end of this instance.

Parameters:
_value - An array of characters to append.
Returns:
A reference to this instance after the append operation has occurred

Append

public XPStringBuilder Append(char[] _value,
                              int offset,
                              int length)
Appends the string representation of a specified subarray of Unicode characters to the end of this instance.

Parameters:
_value - The array of characters to draw input from.
offset - The starting position in _value.
length - The number of characters to append.
Returns:
A reference to this instance after the append operation has occurred

Append

public XPStringBuilder Append(double _value)
Appends the string representation of a specified double-precision floating-point number to the end of this instance. Note: the naive implementation stringBuffer.append(_value) is not system-locale sensitive, since it eventually calls through to FloatingDecimal(_value).toJavaFormatString().

Parameters:
_value - The double-precision floating point number to append in a character representation.
Returns:
A reference to this instance after the append operation has occurred

Append

public XPStringBuilder Append(int _value)
Appends the string representation of a specified 32-bit signed integer to the end of this instance.

Parameters:
_value - The integer value to append in a character representation.
Returns:
A reference to this instance after the append operation has occurred

Append

public XPStringBuilder Append(long _value)
Appends the string representation of a specified 64-bit signed integer to the end of this instance.

Parameters:
_value - A long integer value to append in a character representation.
Returns:
A reference to this instance after the append operation has occurred

Append

public XPStringBuilder Append(java.lang.Object _value)
Appends the string representation of a specified object to the end of this instance.

Parameters:
_value - An object to append as its String representation.
Returns:
A reference to this instance after the append operation has occurred\

Append

public XPStringBuilder Append(java.lang.String _value)
Appends a copy of the specified string to the end of this instance.

Parameters:
_value - An instance of String to append to the builder.
Returns:
A reference to this instance after the append operation has occurred

Append

public XPStringBuilder Append(java.lang.String _value,
                              int offset,
                              int length)
Appends a copy of a specified substring to the end of this instance.

Parameters:
_value - An instance of String to draw the input substring from.
offset - The starting position in value.
length - The number of characters to append.
Returns:
A reference to this instance after the append operation has occurred.

CharAt

public char CharAt(int index)
Returns the character at the specified position in this instance.

Parameters:
index - - The position of the character.
Returns:
The Unicode character at position index

EnsureCapacity

public int EnsureCapacity(int minimumCapacity)
Ensures that the capacity of this instance of XPStringBuilder is at least the specified value.

Parameters:
minimumCapacity - - The minimum capacity to ensure.
Returns:
The current capacity of this instance

GetCapacity

public int GetCapacity()
Returns the current capacity of the String buffer. The capacity is the amount of storage available for newly inserted characters; beyond which an allocation will occur.

Returns:
the current capacity of this string buffer.

GetLength

public int GetLength()
Returns the length (character count) of this string buffer.

Returns:
the length of the sequence of characters currently represented by this string buffer.

Insert

public XPStringBuilder Insert(int offset,
                              boolean _value)
Inserts the string representation of the boolean argument into this string buffer.

The second argument is converted to a string as if by the method String.valueOf, and the characters of that string are then inserted into this string buffer at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer.

Parameters:
offset - the offset.
_value - a boolean.
Returns:
a reference to this XPStringBuilder object.
Throws:
java.lang.StringIndexOutOfBoundsException - if the offset is invalid.

Insert

public XPStringBuilder Insert(int offset,
                              char _value)
Inserts the string representation of the char argument into this string buffer.

The second argument is inserted into the contents of this string buffer at the position indicated by offset. The length of this string buffer increases by one.

The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(char) and the character in that string were then inserted into this XPStringBuilder object at the position indicated by offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer.

Parameters:
offset - the offset.
_value - a char.
Returns:
a reference to this XPStringBuilder object.
Throws:
java.lang.IndexOutOfBoundsException - if the offset is invalid.

Insert

public XPStringBuilder Insert(int offset,
                              char[] _value)
Inserts the string representation of the char array argument into this string buffer.

The characters of the array argument are inserted into the contents of this string buffer at the position indicated by offset. The length of this string buffer increases by the length of the argument.

The overall effect is exactly as if the argument were converted to a string by the method String.valueOf(char[]) and the characters of that string were then inserted into this XPStringBuilder object at the position indicated by offset.

Parameters:
offset - the offset.
_value - a character array.
Returns:
a reference to this XPStringBuilder object.
Throws:
java.lang.StringIndexOutOfBoundsException - if the offset is invalid.

Insert

public XPStringBuilder Insert(int index,
                              char[] _value,
                              int offset,
                              int length)
Inserts the string representation of a subarray of the str array argument into this string buffer. The subarray begins at the specified offset and extends len characters. The characters of the subarray are inserted into this string buffer at the position indicated by index. The length of this XPStringBuilder increases by len characters.

Parameters:
index - position at which to insert subarray.
_value - A character array.
offset - the index of the first character in subarray to to be inserted.
length - the number of characters in the subarray to to be inserted.
Returns:
This XPStringBuilder.
Throws:
java.lang.StringIndexOutOfBoundsException - if index is negative or greater than length(), or offset or len are negative, or (offset+len) is greater than str.length.
Since:
1.2

Insert

public XPStringBuilder Insert(int offset,
                              double _value)
Inserts the string representation of the double argument into this string buffer.

The second argument is converted to a string as if by the method String.valueOf, and the characters of that string are then inserted into this string buffer at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer.

Parameters:
offset - the offset.
_value - a double.
Returns:
a reference to this XPStringBuilder object.
Throws:
java.lang.StringIndexOutOfBoundsException - if the offset is invalid.

Insert

public XPStringBuilder Insert(int offset,
                              int _value)
Inserts the string representation of the second int argument into this string buffer.

The second argument is converted to a string as if by the method String.valueOf, and the characters of that string are then inserted into this string buffer at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer.

Parameters:
offset - the offset.
_value - an int.
Returns:
a reference to this XPStringBuilder object.
Throws:
java.lang.StringIndexOutOfBoundsException - if the offset is invalid.

Insert

public XPStringBuilder Insert(int offset,
                              long _value)
Inserts the string representation of the long argument into this XPStringBuilder.

The second argument is converted to a string as if by the method String.valueOf, and the characters of that string are then inserted into this string buffer at the position indicated by offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer.

Parameters:
offset - the offset.
_value - a long.
Returns:
a reference to this XPStringBuilder object.
Throws:
java.lang.StringIndexOutOfBoundsException - if the offset is invalid.

Insert

public XPStringBuilder Insert(int offset,
                              java.lang.Object _value)
Inserts the string representation of the Object argument into this XPStringBuilder.

The second argument is converted to a string as if by the method String.valueOf, and the characters of that string are then inserted into this string buffer at the indicated offset.

The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer.

Parameters:
offset - the offset.
_value - an Object.
Returns:
a reference to this XPStringBuilder object.
Throws:
java.lang.StringIndexOutOfBoundsException - if the offset is invalid.

Insert

public XPStringBuilder Insert(int offset,
                              java.lang.String _value)
Inserts the string into this XPStringBuilder.

The characters of the String argument are inserted, in order, into this string buffer at the indicated offset, moving up any characters originally above that position and increasing the length of this string buffer by the length of the argument. If str is null, then the four characters "null" are inserted into this string buffer.

The character at index k in the new character sequence is equal to:

The offset argument must be greater than or equal to 0, and less than or equal to the length of this string buffer.

Parameters:
offset - the offset.
_value - a string.
Returns:
a reference to this XPStringBuilder object.
Throws:
java.lang.StringIndexOutOfBoundsException - if the offset is invalid.

Insert

public XPStringBuilder Insert(int offset,
                              java.lang.String _value,
                              int index,
                              int length)
Inserts the string representation of a subarray of the str array argument into this XPStringBuilder. The subarray begins at the specified offset and extends len characters. The characters of the subarray are inserted into this string buffer at the position indicated by index. The length of this XPStringBuilder increases by len characters.

Parameters:
offset - position at which to insert subarray.
_value - A character array.
index - the index of the first character in subarray to to be inserted.
length - the number of characters in the subarray to to be inserted.
Returns:
This XPStringBuilder.
Throws:
java.lang.StringIndexOutOfBoundsException - if index is negative or greater than length(), or offset or len are negative, or (offset+len) is greater than str.length.

Remove

public XPStringBuilder Remove(int start,
                              int end)
Removes the characters in a substring of this XPStringBuilder. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the XPStringBuilder if no such character exists. If start is equal to end, no changes are made.

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
Returns:
This XPStringBuilder.
Throws:
java.lang.StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.

RemoveCharAt

public XPStringBuilder RemoveCharAt(int start)
Removes the character at the specified position in this StringBuffer (shortening the StringBuffer by one character).

Parameters:
start - Index of character to remove
Returns:
This string buffer.
Throws:
java.lang.StringIndexOutOfBoundsException - if the index is negative or greater than or equal to length().

Replace

public XPStringBuilder Replace(int start,
                               int end,
                               java.lang.String _value)
Replaces the characters in a substring of this XPStringBuilder with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (The XPStringBuilder will be lengthened to accommodate the specified String if necessary.)

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
_value - String that will replace previous contents.
Returns:
This XPStringBuilder.
Throws:
java.lang.StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.

Reverse

public XPStringBuilder Reverse()
The character sequence contained in this XPStringBuilder is replaced by the reverse of the sequence.

Let n be the length of the old character sequence, the one contained in the string buffer just prior to execution of the reverse method. Then the character at index k in the new character sequence is equal to the character at index n-k-1 in the old character sequence.

Returns:
a reference to this XPStringBuilder object.

SetCharAt

public void SetCharAt(int index,
                      char ch)
The character at the specified index of this XPStringBuilder is set to ch. The string buffer is altered to represent a new character sequence that is identical to the old character sequence, except that it contains the character ch at position index.

The index argument must be greater than or equal to 0, and less than the length of this string buffer.

Parameters:
index - the index of the character to modify.
ch - the new character.
Throws:
java.lang.IndexOutOfBoundsException - if index is negative or greater than or equal to length().

SetLength

public void SetLength(int length)
Sets the length of this XPStringBuilder. This XPStringBuilder is altered to represent a new character sequence whose length is specified by the argument. For every nonnegative index k less than newLength, the character at index k in the new character sequence is the same as the character at index k in the old sequence if k is less than the length of the old character sequence; otherwise, it is the null character '\u0000'. In other words, if the newLength argument is less than the current length of the string buffer, the string buffer is truncated to contain exactly the number of characters given by the newLength argument.

If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended to the string buffer so that length becomes the newLength argument.

The newLength argument must be greater than or equal to 0.

Parameters:
length - the new length of the buffer.
Throws:
java.lang.IndexOutOfBoundsException - if the newLength argument is negative.

Substring

public java.lang.String Substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this XPStringBuilder.The substring begins at the specified index and extends to the end of the XPStringBuilder.

Parameters:
start - The beginning index, inclusive.
Returns:
The new string.
Throws:
java.lang.StringIndexOutOfBoundsException - if start is less than zero, or greater than the length of this XPStringBuilder.

Substring

public java.lang.String Substring(int start,
                                  int end)
Returns a new String that contains a subsequence of characters currently contained in this XPStringBuilder. The substring begins at the specified start and extends to the character at index end - 1. An exception is thrown if

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
Returns:
The new string.
Throws:
java.lang.StringIndexOutOfBoundsException - if start or end are negative or greater than length(), or start is greater than end.

ToString

public java.lang.String ToString()
Converts to a string representing the data in this XPStringBuilder. A new String object is allocated and initialized to contain the character sequence currently represented by this XPStringBuilder. This String is then returned. Subsequent changes to the string buffer do not affect the contents of the String.

Implementation advice: This method can be coded so as to create a new String object without allocating new memory to hold a copy of the character sequence. Instead, the string can share the memory used by the XPStringBuilder. Any subsequent operation that alters the content or capacity of the XPStringBuilder must then make a copy of the internal buffer at that time. This strategy is effective for reducing the amount of memory allocated by a string concatenation operation when it is implemented using a XPStringBuilder.

Returns:
a string representation of the XPStringBuilder.

toString

public java.lang.String toString()
Converts to a string representing the data in this XPStringBuilder. A new String object is allocated and initialized to contain the character sequence currently represented by this XPStringBuilder. This String is then returned. Subsequent changes to the string buffer do not affect the contents of the String.

Implementation advice: This method can be coded so as to create a new String object without allocating new memory to hold a copy of the character sequence. Instead, the string can share the memory used by the XPStringBuilder. Any subsequent operation that alters the content or capacity of the XPStringBuilder must then make a copy of the internal buffer at that time. This strategy is effective for reducing the amount of memory allocated by a string concatenation operation when it is implemented using a XPStringBuilder.

Returns:
a string representation of the XPStringBuilder.


Copyright © 2002, 2003, 2004 Plumtree Software Inc. All Rights Reserved.