org.mmbase.util
Class StringObject

java.lang.Object
  extended by org.mmbase.util.StringObject

public final class StringObject
extends Object

This Class is a growable buffer for characters. It is mainly used to create Strings. The compiler uses it to implement the "+" operator. For example:

        "a" + 4 + "c"
 
is compiled to:
        new StringBuffer().append("a").append(4).append("c").toString()
 
Note that the method toString() does not create a copy of the internal buffer. Instead the buffer is marked as shared. Any further changes to the buffer will cause a copy to be made.

this is based on StringBuffer code, we have a seperate class since sun doesn't allow us to extend StringBuffer for some reason and we want methods like replace over the whole buffer.

Version:
$Id: StringObject.java 41055 2010-02-15 23:59:16Z michiel $
Author:
Daniel Ockeloen, Johannes Verelst (bugfix), Arthur van Hoff
See Also:
String
License:
Sun license

Constructor Summary
StringObject()
          Constructs an empty String buffer.
StringObject(int length)
          Constructs an empty String buffer with the specified initial length.
StringObject(String str)
          Constructs a String buffer with the specified initial value.
 
Method Summary
 StringObject append(boolean b)
          Appends a boolean to the end of this buffer.
 StringObject append(char c)
          Appends a character to the end of this buffer.
 StringObject append(char[] str)
          Appends an array of characters to the end of this buffer.
 StringObject append(char[] str, int offset, int len)
          Appends a part of an array of characters to the end of this buffer.
 StringObject append(double d)
          Appends a double to the end of this buffer.
 StringObject append(float f)
          Appends a float to the end of this buffer.
 StringObject append(int i)
          Appends an integer to the end of this buffer.
 StringObject append(long l)
          Appends a long to the end of this buffer.
 StringObject append(Object obj)
          Appends an object to the end of this buffer.
 StringObject append(String str)
          Appends a String to the end of this buffer.
 int capacity()
          Returns the current capacity of the String buffer.
 char charAt(int index)
          Returns the character at the specified index.
 StringObject delete(int offset, int len)
          delete part of the buffer
 void ensureCapacity(int minimumCapacity)
          Ensures that the capacity of the buffer is at least equal to the specified minimum.
 byte[] getBytes()
           
 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
          Copies the characters of the specified substring (determined by srcBegin and srcEnd) into the character array, starting at the array's dstBegin location.
(package private)  char[] getValue()
           
 int indexOf(String str)
           
 int indexOf(String str, int fromIndex)
           
 StringObject insert(int offset, boolean b)
          Inserts a boolean into the String buffer.
 StringObject insert(int offset, char c)
          Inserts a character into the String buffer.
 StringObject insert(int offset, char[] str)
          Inserts an array of characters into the String buffer.
 StringObject insert(int offset, double d)
          Inserts a double into the String buffer.
 StringObject insert(int offset, float f)
          Inserts a float into the String buffer.
 StringObject insert(int offset, int i)
          Inserts an integer into the String buffer.
 StringObject insert(int offset, long l)
          Inserts a long into the String buffer.
 StringObject insert(int offset, Object obj)
          Inserts an object into the String buffer.
 StringObject insert(int offset, String str)
          Inserts a String into the String buffer.
 StringObject insertLinks(String oldstart, String oldend, String newstart, String newend, String startend)
          inserts links
 int length()
          Returns the length (character count) of the buffer.
 StringObject replace(int offset, int len, String str)
          replace
 StringObject replace(String oldstr, String newstr)
          replace
 StringObject replace(String oldstart, String oldend, String newstart, String newend)
          Does a replace/insert.
 StringObject replaceFirst(String oldstr, String newstr)
          replace
 StringObject reverse()
          Reverse the order of the characters in the String buffer.
 void setCharAt(int index, char ch)
          Changes the character at the specified index to be ch.
 void setLength(int newLength)
          Sets the length of the String.
(package private)  void setShared()
           
 String toString()
          Converts to a String representing the data in the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringObject

public StringObject()
Constructs an empty String buffer.


StringObject

public StringObject(int length)
Constructs an empty String buffer with the specified initial length.

Parameters:
length - the initial length

StringObject

public StringObject(String str)
Constructs a String buffer with the specified initial value.

Parameters:
str - the initial value of the buffer
Method Detail

length

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


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.


ensureCapacity

public void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum.

Parameters:
minimumCapacity - the minimum desired capacity

setLength

public void setLength(int newLength)
Sets the length of the String. If the length is reduced, characters are lost. If the length is extended, the values of the new characters are set to 0.

Parameters:
newLength - the new length of the buffer
Throws:
StringIndexOutOfBoundsException - If the length is invalid.

charAt

public char charAt(int index)
Returns the character at the specified index. An index ranges from 0..length()-1.

Parameters:
index - the index of the desired character
Throws:
StringIndexOutOfBoundsException - If the index is invalid.

getChars

public void getChars(int srcBegin,
                     int srcEnd,
                     char[] dst,
                     int dstBegin)
Copies the characters of the specified substring (determined by srcBegin and srcEnd) into the character array, starting at the array's dstBegin location. Both srcBegin and srcEnd must be legal indexes into the buffer.

Parameters:
srcBegin - begin copy at this offset in the String
srcEnd - stop copying at this offset in the String
dst - the array to copy the data into
dstBegin - offset into dst
Throws:
StringIndexOutOfBoundsException - If there is an invalid index into the buffer.

setCharAt

public void setCharAt(int index,
                      char ch)
Changes the character at the specified index to be ch.

Parameters:
index - the index of the character
ch - the new character
Throws:
StringIndexOutOfBoundsException - If the index is invalid.

append

public StringObject append(Object obj)
Appends an object to the end of this buffer.

Parameters:
obj - the object to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(String str)
Appends a String to the end of this buffer.

Parameters:
str - the String to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(char[] str)
Appends an array of characters to the end of this buffer.

Parameters:
str - the characters to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(char[] str,
                           int offset,
                           int len)
Appends a part of an array of characters to the end of this buffer.

Parameters:
str - the characters to be appended
offset - where to start
len - the number of characters to add
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(boolean b)
Appends a boolean to the end of this buffer.

Parameters:
b - the boolean to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(char c)
Appends a character to the end of this buffer.

Parameters:
c - the character to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(int i)
Appends an integer to the end of this buffer.

Parameters:
i - the integer to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(long l)
Appends a long to the end of this buffer.

Parameters:
l - the long to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(float f)
Appends a float to the end of this buffer.

Parameters:
f - the float to be appended
Returns:
the StringBuffer itself, NOT a new one.

append

public StringObject append(double d)
Appends a double to the end of this buffer.

Parameters:
d - the double to be appended
Returns:
the StringBuffer itself, NOT a new one.

insert

public StringObject insert(int offset,
                           Object obj)
Inserts an object into the String buffer.

Parameters:
offset - the offset at which to insert
obj - the object to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringObject insert(int offset,
                           String str)
Inserts a String into the String buffer.

Parameters:
offset - the offset at which to insert
str - the String to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringObject insert(int offset,
                           char[] str)
Inserts an array of characters into the String buffer.

Parameters:
offset - the offset at which to insert
str - the characters to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringObject insert(int offset,
                           boolean b)
Inserts a boolean into the String buffer.

Parameters:
offset - the offset at which to insert
b - the boolean to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringObject insert(int offset,
                           char c)
Inserts a character into the String buffer.

Parameters:
offset - the offset at which to insert
c - the character to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset invalid.

insert

public StringObject insert(int offset,
                           int i)
Inserts an integer into the String buffer.

Parameters:
offset - the offset at which to insert
i - the integer to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringObject insert(int offset,
                           long l)
Inserts a long into the String buffer.

Parameters:
offset - the offset at which to insert
l - the long to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringObject insert(int offset,
                           float f)
Inserts a float into the String buffer.

Parameters:
offset - the offset at which to insert
f - the float to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

insert

public StringObject insert(int offset,
                           double d)
Inserts a double into the String buffer.

Parameters:
offset - the offset at which to insert
d - the double to insert
Returns:
the StringBuffer itself, NOT a new one.
Throws:
StringIndexOutOfBoundsException - If the offset is invalid.

reverse

public StringObject reverse()
Reverse the order of the characters in the String buffer.


toString

public String toString()
Converts to a String representing the data in the buffer.

Overrides:
toString in class Object

setShared

final void setShared()

getValue

final char[] getValue()

delete

public StringObject delete(int offset,
                           int len)
delete part of the buffer


replace

public StringObject replace(int offset,
                            int len,
                            String str)
replace


replaceFirst

public StringObject replaceFirst(String oldstr,
                                 String newstr)
replace


replace

public StringObject replace(String oldstr,
                            String newstr)
replace


replace

public StringObject replace(String oldstart,
                            String oldend,
                            String newstart,
                            String newend)
Does a replace/insert. Like make bold:bla into <:b>:bla<:/b>:


insertLinks

public StringObject insertLinks(String oldstart,
                                String oldend,
                                String newstart,
                                String newend,
                                String startend)
inserts links


indexOf

public int indexOf(String str)

indexOf

public int indexOf(String str,
                   int fromIndex)

getBytes

public byte[] getBytes()


MMBase 2.0-SNAPSHOT - null