xv.text
Class StringMaker

java.lang.Object
  extended by xv.text.StringMaker
All Implemented Interfaces:
Appendable, CharSequence

public class StringMaker
extends Object
implements Appendable, CharSequence

A very simple class for creating strings. This class is somewhat lighter-weight than StringBuffer at the expense of being much, much less powerful. But if all you're doing is appending characters (or arrays of characters) this class is much simpler than StringBuffer. It's also not thread-safe.

The main purpose for this class is that it allows you to reuse the existing backing array indefinitely. clear() does not actually clear the buffer, it simply resets the offset to 0.

As of present, this class is slightly more efficient than StringBuilder in similar use cases.

All returned strings contain newly allocated char arrays and never share with the internal char array.

Author:
xv

Constructor Summary
StringMaker()
           
StringMaker(int initialSize)
           
 
Method Summary
 Appendable append(char c)
          Appends the given character.
 void append(char[] buf)
          Appends the given character array.
 void append(char[] buf, int offset, int length)
          Appends the given section of the given character array.
 Appendable append(CharSequence csq)
           
 Appendable append(CharSequence csq, int start, int end)
           
 void append(int c)
          Appends a code point.
 void append(String s)
           
 void append(String s, int offset, int length)
          Appends the given section of the given string.
 char charAt(int index)
           
 void clear()
          "Clears" the string by resetting the string offset to 0.
 int length()
          Gets the number of characters currently contained within this string maker.
 void setLength(int length)
          Sets the length of the string.
 CharSequence subSequence(int start, int end)
           
 String toString()
          Returns the contents of the string maker as a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringMaker

public StringMaker()

StringMaker

public StringMaker(int initialSize)
Method Detail

charAt

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

subSequence

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

append

public void append(int c)
Appends a code point. If the character is not a valid codepoint, then only the two least-significant bytes are appended as a char.

Parameters:
c - the code point to append

append

public Appendable append(char c)
Appends the given character.

Specified by:
append in interface Appendable
Parameters:
c - the character to append
Returns:
a reference to this Appendable

append

public Appendable append(CharSequence csq,
                         int start,
                         int end)
Specified by:
append in interface Appendable

append

public Appendable append(CharSequence csq)
Specified by:
append in interface Appendable

append

public void append(char[] buf)
Appends the given character array.

Parameters:
buf - the array of characters to append
Throws:
NullPointerException - if buf is null

append

public void append(char[] buf,
                   int offset,
                   int length)
Appends the given section of the given character array.

Parameters:
buf - the array of characters to append
offset - the offset into the array of characters to start appending from
length - the number of characters to append
Throws:
IndexOutOfBoundsException - if offset is less than 0, offset + length is greater than buf.length, or length is less than 0
NullPointerException - if buf is null

append

public void append(String s)

append

public void append(String s,
                   int offset,
                   int length)
Appends the given section of the given string.

Parameters:
s - the string to append
offset - the offset into the string to start appending from
length - the number of characters to append
Throws:
IndexOutOfBoundsException - if offset is less than 0, offset + length is greater than s.length(), or length is less than 0

clear

public void clear()
"Clears" the string by resetting the string offset to 0.


length

public int length()
Gets the number of characters currently contained within this string maker. This is the same value that toString().length() would return without the need to allocate a new string.

Specified by:
length in interface CharSequence
Returns:
the current string length

setLength

public void setLength(int length)
Sets the length of the string. If the new length is longer than the current string length, then the string will be padded with null characters (as in StringBuilder.setLength(int)).

Parameters:
length - the new length
Throws:
IndexOutOfBoundsException - if the new length is less than 0.

toString

public String toString()
Returns the contents of the string maker as a string.

Specified by:
toString in interface CharSequence
Overrides:
toString in class Object
Returns:
the string contents


Copyright © 2008-2011. All Rights Reserved.