com.waveset.util
Class StringAppender

java.lang.Object
  extended bycom.waveset.util.StringAppender
All Implemented Interfaces:
java.lang.CharSequence

public class StringAppender
extends java.lang.Object
implements java.lang.CharSequence

StringAppender is built for 'append' speed and scalability it uses block based storage to reduce the number and amount of allocations.


Field Summary
static int DEFAULT_BLOCKSIZE
           
static int DEFAULT_CAPACITY
           
 
Constructor Summary
StringAppender()
          Constructs a string buffer with no characters in it and an initial capacity of 1024 characters.
StringAppender(char[] str)
          Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string.
StringAppender(int capacity)
          Constructs a string buffer with no characters in it and an initial capacity specified by the length argument.
StringAppender(int capacity, int blocksize)
          Constructs a string buffer with no characters in it and an initial capacity specified by the length argument.
StringAppender(java.lang.String str)
          Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string.
 
Method Summary
 StringAppender append(boolean b)
          Appends the string representation of the boolean argument to the string buffer.
 StringAppender append(char c)
          Appends the string representation of the char argument to this string buffer.
 StringAppender append(char[] src)
          Appends the string representation of the char array argument to this string buffer.
 StringAppender append(char[] str, int offset, int len)
          Appends the string representation of a subarray of the char array argument to this string buffer.
 StringAppender append(double d)
          Appends the string representation of the double argument to this string buffer.
 StringAppender append(float f)
          Appends the string representation of the float argument to this string buffer.
 StringAppender append(int i)
          Appends the string representation of the int argument to this string buffer.
 StringAppender append(long l)
          Appends the string representation of the long argument to this string buffer.
 StringAppender append(java.lang.Object obj)
          Appends the string representation of the Object argument to this string buffer.
 StringAppender append(java.lang.String src)
          Appends the string to this string buffer.
 int blocksize()
          Returns the current size of the blocks in the buffer.
 int capacity()
          Returns the current capacity of the String buffer.
 char charAt(int index)
           
 void ensureCapacity(int mincap)
          Ensures that the capacity of the buffer is at least equal to the specified minimum.
 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
          Characters are copied from this string buffer into the destination character array dst.
 int length()
           
 void setCharAt(int index, char ch)
          The character at the specified index of this string buffer is set to ch.
 void setLength(int len)
           
 java.lang.CharSequence subSequence(int start, int end)
           
 java.lang.String substring(int start)
          Returns a new String that contains a subsequence of characters currently contained in this OptimizedStringBuffer .The substring begins at the specified index and extends to the end of the OptimizedStringBuffer.
 java.lang.String substring(int start, int end)
          Returns a new String that contains a subsequence of characters currently contained in this OptimizedStringBuffer.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_CAPACITY

public static final int DEFAULT_CAPACITY
See Also:
Constant Field Values

DEFAULT_BLOCKSIZE

public static final int DEFAULT_BLOCKSIZE
See Also:
Constant Field Values
Constructor Detail

StringAppender

public StringAppender()
Constructs a string buffer with no characters in it and an initial capacity of 1024 characters.


StringAppender

public StringAppender(int capacity)
Constructs a string buffer with no characters in it and an initial capacity specified by the length argument.

Throws:
java.lang.NegativeArraySizeException - if the length argument is less than 0.

StringAppender

public StringAppender(int capacity,
                      int blocksize)
Constructs a string buffer with no characters in it and an initial capacity specified by the length argument.

Parameters:
capacity - the initial capacity.
blocksize - the allocation size of each block
Throws:
java.lang.NegativeArraySizeException - if the length argument is less than 0.

StringAppender

public StringAppender(java.lang.String str)
Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string. The initial capacity of the string buffer is 1024 plus the length of the string argument.

Parameters:
str - the initial contents of the buffer.

StringAppender

public StringAppender(char[] str)
Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string. The initial capacity of the string buffer is 1024 plus the length of the string argument.

Parameters:
str - the initial contents of the buffer.
Method Detail

capacity

public int capacity()
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.

blocksize

public int blocksize()
Returns the current size of the blocks in the buffer.

Returns:
the current size of each block in the buffer.

ensureCapacity

public void ensureCapacity(int mincap)
Ensures that the capacity of the buffer is at least equal to the specified minimum. If the current capacity of this string buffer is less than the argument, then a new internal buffer is allocated with greater capacity. The new capacity is the larger of: If the mincap argument is nonpositive, this method takes no action and simply returns. TODO: manually inline the code for faster performance.


setLength

public void setLength(int len)

getChars

public void getChars(int srcBegin,
                     int srcEnd,
                     char[] dst,
                     int dstBegin)
Characters are copied from this string buffer into the destination character array dst. The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1. The total number of characters to be copied is srcEnd-srcBegin. The characters are copied into the subarray of dst starting at index dstBegin and ending at index:

 dstbegin + (srcEnd - srcBegin) - 1
 

Parameters:
srcBegin - start copying at this offset in the string buffer.
srcEnd - stop copying at this offset in the string buffer.
dst - the array to copy the data into.
dstBegin - offset into dst.
Throws:
java.lang.NullPointerException - if dst is null.
java.lang.IndexOutOfBoundsException - if any of the following is true:
  • srcBegin is negative
  • dstBegin is negative
  • the srcBegin argument is greater than the srcEnd argument.
  • srcEnd is greater than this.length(), the current length of this string buffer.
  • dstBegin+srcEnd-srcBegin is greater than dst.length

setCharAt

public void setCharAt(int index,
                      char ch)
The character at the specified index of this string buffer 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 offset 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().
See Also:
java.lang.StringAppender#length()

append

public StringAppender append(java.lang.Object obj)
Appends the string representation of the Object argument to this string buffer.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string buffer.

Parameters:
obj - an Object.
Returns:
a reference to this OptimizedStringBuffer object.
See Also:
String.valueOf(java.lang.Object), java.lang.StringAppender#append(java.lang.String)

append

public StringAppender append(java.lang.String src)
Appends the string to this string buffer.

The characters of the String argument are appended, in order, to the contents of this string buffer, increasing the length of this string buffer by the length of the argument. If str is null, then the four characters "null" are appended to this string buffer.

Let n be the length of the old character sequence, the one contained in the string buffer just prior to execution of the append method. Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n ; otherwise, it is equal to the character at index k-n in the argument str.

Returns:
a reference to this OptimizedStringBuffer.

append

public StringAppender append(char[] src)
Appends the string representation of the char array argument to this string buffer.

The characters of the array argument are appended, in order, to the contents of this string buffer. 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 appendedto this OptimizedStringBuffer object.

Returns:
a reference to this OptimizedStringBuffer object.

append

public StringAppender append(char[] str,
                             int offset,
                             int len)
Appends the string representation of a subarray of the char array argument to this string buffer.

Characters of the character array str, starting at index offset, are appended, in order, to the contents of this string buffer. The length of this string buffer increases by the value of len.

The overall effect is exactly as if the arguments were converted to a string by the method String.valueOf(char[],int,int)and the characters of that string were then appendedto this OptimizedStringBuffer object.

Parameters:
str - the characters to be appended.
offset - the index of the first character to append.
len - the number of characters to append.
Returns:
a reference to this OptimizedStringBuffer object.

append

public StringAppender append(boolean b)
Appends the string representation of the boolean argument to the string buffer.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string buffer.

Parameters:
b - a boolean.
Returns:
a reference to this OptimizedStringBuffer.
See Also:
String.valueOf(boolean), java.lang.StringAppender#append(java.lang.String)

append

public StringAppender append(char c)
Appends the string representation of the char argument to this string buffer.

The argument is appended to the contents of this string buffer. The length of this string buffer increases by 1.

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 appendedto this OptimizedStringBuffer object.

Parameters:
c - a char.
Returns:
a reference to this OptimizedStringBuffer object.

append

public StringAppender append(int i)
Appends the string representation of the int argument to this string buffer.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string buffer.

Parameters:
i - an int.
Returns:
a reference to this OptimizedStringBuffer object.
See Also:
String.valueOf(int), java.lang.StringAppender#append(java.lang.String)

append

public StringAppender append(long l)
Appends the string representation of the long argument to this string buffer.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string buffer.

Parameters:
l - a long.
Returns:
a referenct to this OptimizedStringBuffer object.
See Also:
String.valueOf(long), java.lang.StringAppender#append(java.lang.String)

append

public StringAppender append(float f)
Appends the string representation of the float argument to this string buffer.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string buffer.

Parameters:
f - a float.
Returns:
a reference to this OptimizedStringBuffer object.
See Also:
String.valueOf(float), java.lang.StringAppender#append(java.lang.String)

append

public StringAppender append(double d)
Appends the string representation of the double argument to this string buffer.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string buffer.

Parameters:
d - a double.
Returns:
a reference to this OptimizedStringBuffer object.
See Also:
String.valueOf(double), java.lang.StringAppender#append(java.lang.String)

substring

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

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 OptimizedStringBuffer.

substring

public java.lang.String substring(int start,
                                  int end)
Returns a new String that contains a subsequence of characters currently contained in this OptimizedStringBuffer. 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.

length

public int length()
Specified by:
length in interface java.lang.CharSequence

charAt

public char charAt(int index)
Specified by:
charAt in interface java.lang.CharSequence

subSequence

public java.lang.CharSequence subSequence(int start,
                                          int end)
Specified by:
subSequence in interface java.lang.CharSequence

toString

public java.lang.String toString()
Specified by:
toString in interface java.lang.CharSequence