|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.plumtree.openfoundation.util.XPStringBuilder
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:
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.x = new StringBuffer().append("a").append(4).append("c") .toString()
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 |
public XPStringBuilder()
public XPStringBuilder(int capacity)
capacity
- -
the initial capacity.public XPStringBuilder(java.lang.String str)
str
- -
the initial content of the XPStringBuilderpublic XPStringBuilder(java.lang.StringBuffer stringBuffer)
stringBuffer
- -
an instance of StringBufferMethod Detail |
public XPStringBuilder Append(boolean _value)
_value
- -
A Boolean value
public XPStringBuilder Append(char _value)
_value
- -
A Unicode character
public XPStringBuilder Append(char[] _value)
_value
- An array of characters to append.
public XPStringBuilder Append(char[] _value, int offset, int length)
_value
- The array of characters to draw input from.offset
- The starting position in _value.length
- The number of characters to append.
public XPStringBuilder Append(double _value)
_value
- The double-precision floating point number to append in a character representation.
public XPStringBuilder Append(int _value)
_value
- The integer value to append in a character representation.
public XPStringBuilder Append(long _value)
_value
- A long integer value to append in a character representation.
public XPStringBuilder Append(java.lang.Object _value)
_value
- An object to append as its String representation.
public XPStringBuilder Append(java.lang.String _value)
_value
- An instance of String to append to the builder.
public XPStringBuilder Append(java.lang.String _value, int offset, int length)
_value
- An instance of String to draw the input substring from.offset
- The starting position in value.length
- The number of characters to append.
public char CharAt(int index)
index
- -
The position of the character.
public int EnsureCapacity(int minimumCapacity)
minimumCapacity
- -
The minimum capacity to ensure.
public int GetCapacity()
public int GetLength()
public XPStringBuilder Insert(int offset, boolean _value)
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.
offset
- the offset._value
- a boolean
.
XPStringBuilder
object.
java.lang.StringIndexOutOfBoundsException
- if the offset is invalid.public XPStringBuilder Insert(int offset, char _value)
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.
offset
- the offset._value
- a char
.
XPStringBuilder
object.
java.lang.IndexOutOfBoundsException
- if the offset is invalid.public XPStringBuilder Insert(int offset, char[] _value)
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
.
offset
- the offset._value
- a character array.
XPStringBuilder
object.
java.lang.StringIndexOutOfBoundsException
- if the offset is invalid.public XPStringBuilder Insert(int index, char[] _value, int offset, int length)
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.
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.
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
.public XPStringBuilder Insert(int offset, double _value)
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.
offset
- the offset._value
- a double
.
XPStringBuilder
object.
java.lang.StringIndexOutOfBoundsException
- if the offset is invalid.public XPStringBuilder Insert(int offset, int _value)
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.
offset
- the offset._value
- an int
.
XPStringBuilder
object.
java.lang.StringIndexOutOfBoundsException
- if the offset is invalid.public XPStringBuilder Insert(int offset, long _value)
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.
offset
- the offset._value
- a long
.
XPStringBuilder
object.
java.lang.StringIndexOutOfBoundsException
- if the offset is invalid.public XPStringBuilder Insert(int offset, java.lang.Object _value)
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.
offset
- the offset._value
- an Object
.
XPStringBuilder
object.
java.lang.StringIndexOutOfBoundsException
- if the offset is invalid.public XPStringBuilder Insert(int offset, java.lang.String _value)
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:
offset
-offset
in the
argument str
, if k is not less than
offset
but is less than offset+str.length()
-str.length()
in the
old character sequence, if k is not less than
offset+str.length()
The offset argument must be greater than or equal to
0
, and less than or equal to the length of this
string buffer.
offset
- the offset._value
- a string.
XPStringBuilder
object.
java.lang.StringIndexOutOfBoundsException
- if the offset is invalid.public XPStringBuilder Insert(int offset, java.lang.String _value, int index, int length)
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.
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.
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
.public XPStringBuilder Remove(int start, int end)
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.
start
- The beginning index, inclusive.end
- The ending index, exclusive.
java.lang.StringIndexOutOfBoundsException
- if start
is negative, greater than length()
, or
greater than end
.public XPStringBuilder RemoveCharAt(int start)
StringBuffer
(shortening the StringBuffer
by one character).
start
- Index of character to remove
java.lang.StringIndexOutOfBoundsException
- if the index
is negative or greater than or equal to
length()
.public XPStringBuilder Replace(int start, int end, java.lang.String _value)
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.)
start
- The beginning index, inclusive.end
- The ending index, exclusive._value
- String that will replace previous contents.
java.lang.StringIndexOutOfBoundsException
- if start
is negative, greater than length()
, or
greater than end
.public XPStringBuilder Reverse()
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.
XPStringBuilder
object.public void SetCharAt(int index, char ch)
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.
index
- the index of the character to modify.ch
- the new character.
java.lang.IndexOutOfBoundsException
- if index
is
negative or greater than or equal to length()
.public void SetLength(int length)
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
.
length
- the new length of the buffer.
java.lang.IndexOutOfBoundsException
- if the
newLength
argument is negative.public java.lang.String Substring(int start)
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
.
start
- The beginning index, inclusive.
java.lang.StringIndexOutOfBoundsException
- if start
is
less than zero, or greater than the length of this
XPStringBuilder
.public java.lang.String Substring(int start, int end)
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
start
- The beginning index, inclusive.end
- The ending index, exclusive.
java.lang.StringIndexOutOfBoundsException
- if start
or end
are negative or greater than
length()
, or start
is
greater than end
.public java.lang.String ToString()
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.
public java.lang.String toString()
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |