net.ranides.assira.text
Class AbstractStrBuilder<Self extends AbstractStrBuilder<Self>>

java.lang.Object
  extended by net.ranides.assira.text.AbstractStrBuilder<Self>
All Implemented Interfaces:
Appendable, CharSequence
Direct Known Subclasses:
StrBuilder

public abstract class AbstractStrBuilder<Self extends AbstractStrBuilder<Self>>
extends Object
implements CharSequence, Appendable


Constructor Summary
AbstractStrBuilder()
           
AbstractStrBuilder(int initialCapacity)
           
AbstractStrBuilder(String str)
           
 
Method Summary
 Self append(AbstractStrBuilder str)
          Appends another string builder to this string builder.
 Self append(AbstractStrBuilder str, int startIndex, int length)
          Appends part of a string builder to this string builder.
 Self append(boolean value)
          Appends a boolean value to the string builder.
 Self append(char ch)
          Appends a char value to the string builder.
 Self append(char[] chars)
          Appends a char array to the string builder.
 Self append(char[] chars, int startIndex, int length)
          Appends a char array to the string builder.
 Self append(CharSequence seq)
          Appends a CharSequence to this string builder.
 Self append(CharSequence seq, int start, int length)
          Appends part of a CharSequence to this string builder.
 Self append(double value)
          Appends a double value to the string builder using String.valueOf.
 Self append(float value)
          Appends a float value to the string builder using String.valueOf.
 Self append(int value)
          Appends an int value to the string builder using String.valueOf.
 Self append(long value)
          Appends a long value to the string builder using String.valueOf.
 Self append(Object obj)
          Appends an object to this string builder.
 Self append(String str)
          Appends a string to this string builder.
 Self append(StringBuffer str)
          Appends a string buffer to this string builder.
 Self append(StringBuffer str, int startIndex, int length)
          Appends part of a string buffer to this string builder.
 Self append(String str, int startIndex, int length)
          Appends part of a string to this string builder.
 Self appendNull()
           
 Reader asReader()
          Gets the contents of this builder as a Reader.
 Writer asWriter()
          Gets this builder as a Writer that can be written to.
 int capacity()
           
 char charAt(int index)
           
 Self clear()
           
 Self ensureCapacity(int capacity)
           
 boolean equals(Object object)
           
 char[] getChars()
          Copies the builder's character array into a new character array.
 char[] getChars(char[] destination)
          Copies the character array into the specified array.
 char[] getChars(int startIndex, int endIndex)
          Copies part of the builder's character array into a new character array.
 void getChars(int startIndex, int endIndex, char[] destination, int destinationIndex)
          Copies the character array into the specified array.
 int hashCode()
           
 int length()
           
 Self minimizeCapacity()
           
 Self resize(int length)
          Updates the length of the builder by either dropping the last characters or adding filler of Unicode zero.
 Self resize(int length, char c)
           
 Self reverse()
          Reverses the string builder placing each character in the opposite index.
 Self setCharAt(int index, char ch)
           
 Self setNullText(String nullText)
           
 CharSequence subSequence(int startIndex, int endIndex)
           
 String substring(int start)
           
 String substring(int startIndex, int endIndex)
           
 StringBuilder toBuilder()
          Gets a StringBuffer version of the string builder, creating a new instance each time the method is called.
 String toSharedString()
          Gets a String version of the string builder by calling the internal constructor of String by reflection.
 String toString()
           
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractStrBuilder

public AbstractStrBuilder()

AbstractStrBuilder

public AbstractStrBuilder(int initialCapacity)

AbstractStrBuilder

public AbstractStrBuilder(String str)
Method Detail

setNullText

public Self setNullText(String nullText)

length

public int length()
Specified by:
length in interface CharSequence

resize

public Self resize(int length)
Updates the length of the builder by either dropping the last characters or adding filler of Unicode zero.

Parameters:
length - the length to set to, must be zero or positive
Returns:
this, to enable chaining
Throws:
IndexOutOfBoundsException - if the length is negative

resize

public Self resize(int length,
                   char c)

capacity

public int capacity()

ensureCapacity

public Self ensureCapacity(int capacity)

minimizeCapacity

public Self minimizeCapacity()

clear

public Self clear()

charAt

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

setCharAt

public Self setCharAt(int index,
                      char ch)

getChars

public char[] getChars()
Copies the builder's character array into a new character array.

Returns:
a new array that represents the contents of the builder

getChars

public char[] getChars(int startIndex,
                       int endIndex)
Copies part of the builder's character array into a new character array.

Parameters:
startIndex - the start index, inclusive, must be valid
endIndex - the end index, exclusive, must be valid except that if too large it is treated as end of string
Returns:
a new array that holds part of the contents of the builder
Throws:
IndexOutOfBoundsException - if startIndex is invalid, or if endIndex is invalid (but endIndex greater than size is valid)

getChars

public char[] getChars(char[] destination)
Copies the character array into the specified array.

Parameters:
destination - the destination array, null will cause an array to be created
Returns:
the input array, unless that was null or too small

getChars

public void getChars(int startIndex,
                     int endIndex,
                     char[] destination,
                     int destinationIndex)
Copies the character array into the specified array.

Parameters:
startIndex - first index to copy, inclusive, must be valid
endIndex - last index, exclusive, must be valid
destination - the destination array, must not be null or too small
destinationIndex - the index to start copying in destination
Throws:
NullPointerException - if the array is null
IndexOutOfBoundsException - if any index is invalid

appendNull

public Self appendNull()

append

public Self append(Object obj)
Appends an object to this string builder. Appending null will call appendNull().

Parameters:
obj - the object to append
Returns:
this, to enable chaining

append

public Self append(CharSequence seq)
Appends a CharSequence to this string builder. Appending null will call appendNull().

Specified by:
append in interface Appendable
Parameters:
seq - the CharSequence to append
Returns:
this, to enable chaining

append

public Self append(CharSequence seq,
                   int start,
                   int length)
Appends part of a CharSequence to this string builder. Appending null will call appendNull().

Specified by:
append in interface Appendable
Parameters:
seq - the CharSequence to append
startIndex - the start index, inclusive, must be valid
length - the length to append, must be valid
Returns:
this, to enable chaining

append

public Self append(String str)
Appends a string to this string builder. Appending null will call appendNull().

Parameters:
str - the string to append
Returns:
this, to enable chaining

append

public Self append(String str,
                   int startIndex,
                   int length)
Appends part of a string to this string builder. Appending null will call appendNull().

Parameters:
str - the string to append
startIndex - the start index, inclusive, must be valid
length - the length to append, must be valid
Returns:
this, to enable chaining

append

public Self append(StringBuffer str)
Appends a string buffer to this string builder. Appending null will call appendNull().

Parameters:
str - the string buffer to append
Returns:
this, to enable chaining

append

public Self append(StringBuffer str,
                   int startIndex,
                   int length)
Appends part of a string buffer to this string builder. Appending null will call appendNull().

Parameters:
str - the string to append
startIndex - the start index, inclusive, must be valid
length - the length to append, must be valid
Returns:
this, to enable chaining

append

public Self append(AbstractStrBuilder str)
Appends another string builder to this string builder. Appending null will call appendNull().

Parameters:
str - the string builder to append
Returns:
this, to enable chaining

append

public Self append(AbstractStrBuilder str,
                   int startIndex,
                   int length)
Appends part of a string builder to this string builder. Appending null will call appendNull().

Parameters:
str - the string to append
startIndex - the start index, inclusive, must be valid
length - the length to append, must be valid
Returns:
this, to enable chaining

append

public Self append(char[] chars)
Appends a char array to the string builder. Appending null will call appendNull().

Parameters:
chars - the char array to append
Returns:
this, to enable chaining

append

public Self append(char[] chars,
                   int startIndex,
                   int length)
Appends a char array to the string builder. Appending null will call appendNull().

Parameters:
chars - the char array to append
startIndex - the start index, inclusive, must be valid
length - the length to append, must be valid
Returns:
this, to enable chaining

append

public Self append(boolean value)
Appends a boolean value to the string builder.

Parameters:
value - the value to append
Returns:
this, to enable chaining

append

public Self append(char ch)
Appends a char value to the string builder.

Specified by:
append in interface Appendable
Parameters:
ch - the value to append
Returns:
this, to enable chaining
Since:
3.0

append

public Self append(int value)
Appends an int value to the string builder using String.valueOf.

Parameters:
value - the value to append
Returns:
this, to enable chaining

append

public Self append(long value)
Appends a long value to the string builder using String.valueOf.

Parameters:
value - the value to append
Returns:
this, to enable chaining

append

public Self append(float value)
Appends a float value to the string builder using String.valueOf.

Parameters:
value - the value to append
Returns:
this, to enable chaining

append

public Self append(double value)
Appends a double value to the string builder using String.valueOf.

Parameters:
value - the value to append
Returns:
this, to enable chaining

reverse

public Self reverse()
Reverses the string builder placing each character in the opposite index.

Returns:
this, to enable chaining

subSequence

public CharSequence subSequence(int startIndex,
                                int endIndex)
Specified by:
subSequence in interface CharSequence

substring

public String substring(int start)

substring

public String substring(int startIndex,
                        int endIndex)

equals

public boolean equals(Object object)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Specified by:
toString in interface CharSequence
Overrides:
toString in class Object

toBuilder

public StringBuilder toBuilder()
Gets a StringBuffer version of the string builder, creating a new instance each time the method is called.

Returns:
the builder as a StringBuilder

toSharedString

public String toSharedString()
Gets a String version of the string builder by calling the internal constructor of String by reflection.

WARNING: You must not use the StrBuilder after calling this method as the buffer is now shared with the String object. To ensure this, the internal character array is set to null, so you will get NullPointerExceptions on all method calls.

Returns:
the builder as a String

asReader

public Reader asReader()
Gets the contents of this builder as a Reader.

This method allows the contents of the builder to be read using any standard method that expects a Reader.

To use, simply create a StrBuilder, populate it with data, call asReader, and then read away.

The internal character array is shared between the builder and the reader. This allows you to append to the builder after creating the reader, and the changes will be picked up. Note however, that no synchronization occurs, so you must perform all operations with the builder and the reader in one thread.

The returned reader supports marking, and ignores the flush method.

Returns:
a reader that reads from this builder

asWriter

public Writer asWriter()
Gets this builder as a Writer that can be written to.

This method allows you to populate the contents of the builder using any standard method that takes a Writer.

To use, simply create a StrBuilder, call asWriter, and populate away. The data is available at any time using the methods of the StrBuilder.

The internal character array is shared between the builder and the writer. This allows you to intermix calls that append to the builder and write using the writer and the changes will be occur correctly. Note however, that no synchronization occurs, so you must perform all operations with the builder and the writer in one thread.

The returned writer ignores the close and flush methods.

Returns:
a writer that populates this builder


Copyright © 2013. All Rights Reserved.