@Realtime public final class Text extends Object implements CharSequence, Comparable<CharSequence>, XMLSerializable, ValueType<Text>
An immutable character sequence with fast concatenation
,
insertion
and
deletion
capabilities (O[Log(n)]) instead of
O[n] for StringBuffer/StringBuilder).
This class has the same methods as Java String and .NET String with the following benefits:
StringBuffer
/StringBuilder
in order to manipulate
textual documents (insertion, deletion or concatenation).String.substring(int)
memory leak bug (when small substrings prevent memory from
larger string from being garbage collected).java.lang.String
or CharSequence
. Text
literals should be explicitly interned
.
Unlike strings literals and strings-value constant expressions,
interning is not implicit. For example:
final static Text TRUE = new Text("true").intern(); final static Text FALSE = new Text("true").intern("false");
Implementation Note: To avoid expensive copy operations ,
Text
instances are broken down into smaller immutable
sequences, they form a minimal-depth binary tree.
The tree is maintained balanced automatically through tree rotations.
Insertion/deletions are performed in O[Log(n)]
instead of O[n]
for
StringBuffer/StringBuilder
.
Modifier and Type | Field and Description |
---|---|
static Text |
EMPTY
Holds an empty character sequence.
|
Constructor and Description |
---|
Text(String str)
Creates a text holding the characters from the specified
String
. |
Modifier and Type | Method and Description |
---|---|
char |
charAt(int index)
Returns the character at the specified index.
|
int |
compareTo(CharSequence csq)
Compares this text to another character sequence or string
lexicographically.
|
Text |
concat(Text that)
Concatenates the specified text to the end of this text.
|
boolean |
contentEquals(CharSequence csq)
Indicates if this text has the same character content as the specified
character sequence.
|
boolean |
contentEqualsIgnoreCase(CharSequence csq)
Indicates if this text has the same character contend as the specified
character sequence ignoring case considerations.
|
Text |
copy() |
Text |
delete(int start,
int end)
Returns the text without the characters between the specified indexes.
|
boolean |
endsWith(CharSequence suffix)
Indicates if this text ends with the specified suffix.
|
boolean |
equals(Object obj)
Compares this text against the specified object for equality.
|
void |
getChars(int start,
int end,
char[] dest,
int destPos)
Copies the characters from this text into the destination
character array.
|
int |
hashCode()
Returns the hash code for this text.
|
int |
indexOf(char c)
Returns the index within this text of the first occurrence of the
specified character, starting the search at the beginning.
|
int |
indexOf(char c,
int fromIndex)
Returns the index within this text of the first occurrence of the
specified character, starting the search at the specified index.
|
int |
indexOf(CharSequence csq)
Returns the index within this text of the first occurrence
of the specified character sequence searching forward.
|
int |
indexOf(CharSequence csq,
int fromIndex)
Returns the index within this text of the first occurrence
of the specified characters sequence searching forward from
the specified index.
|
int |
indexOfAny(CharSet charSet)
Returns the index within this text of the first occurrence
of any character in the specified character set.
|
int |
indexOfAny(CharSet charSet,
int start)
Returns the index within a region of this text of the first occurrence
of any character in the specified character set.
|
int |
indexOfAny(CharSet charSet,
int start,
int length)
Returns the index within a region of this text of the first occurrence
of any character in the specified character set.
|
Text |
insert(int index,
Text txt)
Returns the text having the specified text inserted at
the specified location.
|
Text |
intern()
Returns a text equals to this one from a pool of
unique text instances.
|
boolean |
isBlank()
Indicates if all characters of this text are whitespaces
(no characters greater than the space character).
|
boolean |
isBlank(int start,
int length)
Indicates if the specified sub-range of characters of this text
are whitespaces (no characters greater than the space character).
|
int |
lastIndexOf(char c,
int fromIndex)
Returns the index within this text of the first occurrence of the
specified character, searching backward and starting at the specified
index.
|
int |
lastIndexOf(CharSequence csq)
Returns the index within this text of the last occurrence of
the specified characters sequence searching backward.
|
int |
lastIndexOf(CharSequence csq,
int fromIndex)
Returns the index within this text of the last occurrence of
the specified character sequence searching backward from the specified
index.
|
int |
lastIndexOfAny(CharSet charSet)
Returns the index within this text of the last occurrence
of any character in the specified character set.
|
int |
lastIndexOfAny(CharSet charSet,
int start)
Returns the index within a region of this text of the last occurrence
of any character in the specified character set.
|
int |
lastIndexOfAny(CharSet charSet,
int start,
int length)
Returns the index within a region of this text of the last occurrence
of any character in the specified character set.
|
int |
length()
Returns the length of this text.
|
Text |
padLeft(int len)
Pads this text on the left with spaces to make the minimum total length
as specified.
|
Text |
padLeft(int len,
char c)
Pads this text on the left to make the minimum total length as specified.
|
Text |
padRight(int len)
Pads this text on the right with spaces to make the minimum total length
as specified.
|
Text |
padRight(int len,
char c)
Pads this text on the right to make the minimum total length as specified.
|
Text |
plus(Object obj)
Returns the concatenation of this text and the textual
representation of the specified object.
|
Text |
plus(String str)
Returns the concatenation of this text and the specified
String (optimization). |
void |
printStatistics(PrintStream out)
Prints the current statistics on this text tree structure.
|
Text |
replace(CharSequence target,
CharSequence replacement)
Replaces each character sequence of this text that matches the specified
target sequence with the specified replacement sequence.
|
Text |
replace(CharSet charSet,
CharSequence replacement)
Replaces the specified characters in this text with the specified
replacement sequence.
|
boolean |
startsWith(CharSequence prefix)
Indicates if this text starts with the specified prefix.
|
boolean |
startsWith(CharSequence prefix,
int index)
Indicates if this text starts with the specified prefix
at the specified index.
|
CharSequence |
subSequence(int start,
int end)
Returns
subtext(start, end) . |
Text |
subtext(int start)
Returns a portion of this text.
|
Text |
subtext(int start,
int end)
Returns a portion of this text.
|
Text |
toLowerCase()
Converts the characters of this text to lower case.
|
String |
toString()
Returns the
String representation of this text. |
Text |
toText()
Returns
this (implements
Realtime interface). |
Text |
toUpperCase()
Converts the characters of this text to upper case.
|
Text |
trim()
Returns a copy of this text, with leading and trailing
whitespace omitted.
|
Text |
trimEnd()
Returns a copy of this text, with trailing
whitespace omitted.
|
Text |
trimStart()
Returns a copy of this text, with leading whitespace omitted.
|
Text |
value()
Returns
this . |
static Text |
valueOf(boolean b)
Returns the text representation of the
boolean argument. |
static Text |
valueOf(char c)
Returns the text instance corresponding to the specified character.
|
static Text |
valueOf(char[] chars)
Returns the text that contains the characters from the specified
array.
|
static Text |
valueOf(char[] chars,
int offset,
int length)
Returns the text that contains the characters from the specified
subarray of characters.
|
static Text |
valueOf(char c,
int length)
Returns the text that contains a specific length sequence of the
character specified.
|
static Text |
valueOf(double d)
Returns the textual representation of the specified
double
argument. |
static Text |
valueOf(double d,
int digits,
boolean scientific,
boolean showZero)
Returns the textual representation of the specified
double
argument formatted as specified. |
static Text |
valueOf(float f)
Returns the textual representation of the specified
float
instance. |
static Text |
valueOf(int i)
Returns the decimal representation of the specified
int
argument. |
static Text |
valueOf(int i,
int radix)
Returns the radix representation of the specified
int
argument. |
static Text |
valueOf(long l)
Returns the decimal representation of the specified
long
argument. |
static Text |
valueOf(long l,
int radix)
Returns the radix representation of the specified
long
argument. |
static Text |
valueOf(Object obj)
Returns the text representing the specified object.
|
public static final Text EMPTY
public Text(String str)
String
.str
- the string holding the character content.public static Text valueOf(Object obj)
obj
- the object to represent as text.new TextBuilder().append(obj).toText()
public static Text valueOf(char[] chars)
chars
- the array source of the characters.public static Text valueOf(char[] chars, int offset, int length)
chars
- the source of the characters.offset
- the index of the first character in the data soure.length
- the length of the text returned.IndexOutOfBoundsException
- if (offset < 0) ||
(length < 0) || ((offset + length) > chars.length)
public static Text valueOf(boolean b)
boolean
argument.b
- a boolean
.true
, the text
"true"
is returned; otherwise, the text
"false"
is returned.public static Text valueOf(char c)
c
- a character.1
containing 'c'
.public static Text valueOf(int i)
int
argument.i
- the int
to format.public static Text valueOf(int i, int radix)
int
argument.i
- the int
to format.radix
- the radix (e.g. 16
for hexadecimal).public static Text valueOf(long l)
long
argument.l
- the long
to format.public static Text valueOf(long l, int radix)
long
argument.l
- the long
to format.radix
- the radix (e.g. 16
for hexadecimal).public static Text valueOf(float f)
float
instance.f
- the float
to format.public static Text valueOf(double d)
double
argument.d
- the double
to format.public static Text valueOf(double d, int digits, boolean scientific, boolean showZero)
double
argument formatted as specified.d
- the double
to format.digits
- the number of significative digits (excludes exponent) or
-1
to mimic the standard library (16 or 17 digits).scientific
- true
to forces the use of the scientific
notation (e.g. 1.23E3
); false
otherwise.showZero
- true
if trailing fractional zeros are
represented; false
otherwise.IllegalArgumentException
- if (digits > 19)
)public int length()
length
in interface CharSequence
public Text plus(Object obj)
obj
- the object whose textual representation is concatenated.this.concat(Text.valueOf(obj))
public Text plus(String str)
String
(optimization).str
- the string whose characters are concatenated.this.concat(Text.valueOf(obj))
public Text concat(Text that)
StringBuffer.append(String)
) and still returns
a text instance with an internal binary tree of minimal depth!that
- the text that is concatenated.this + that
public Text subtext(int start)
start
- the index of the first character inclusive.IndexOutOfBoundsException
- if (start < 0) ||
(start > this.length())
public Text insert(int index, Text txt)
index
- the insertion position.txt
- the text being inserted.subtext(0, index).concat(txt).concat(subtext(index))
IndexOutOfBoundsException
- if (index < 0) ||
(index > this.length())
public Text delete(int start, int end)
start
- the beginning index, inclusive.end
- the ending index, exclusive.subtext(0, start).concat(subtext(end))
IndexOutOfBoundsException
- if (start < 0) || (end < 0) ||
(start > end) || (end > this.length()
public Text replace(CharSequence target, CharSequence replacement)
target
- the character sequence to be replaced.replacement
- the replacement sequence.public Text replace(CharSet charSet, CharSequence replacement)
charSet
- the set of characters to be replaced.replacement
- the replacement sequence.public CharSequence subSequence(int start, int end)
subtext(start, end)
.subSequence
in interface CharSequence
start
- the index of the first character inclusive.end
- the index of the last character exclusive.this.subtext(start, end)
IndexOutOfBoundsException
- if (start < 0) || (end < 0) ||
(start > end) || (end > this.length())
public int indexOf(CharSequence csq)
csq
- a character sequence.-1
if the character sequence is not found.public int indexOf(CharSequence csq, int fromIndex)
csq
- a character sequence.fromIndex
- the index to start the search from.[fromIndex, length() - csq.length()]
or -1
if the character sequence is not found.public int lastIndexOf(CharSequence csq)
csq
- a character sequence.-1
if the character sequence is not found.public int lastIndexOf(CharSequence csq, int fromIndex)
csq
- a character sequence.fromIndex
- the index to start the backward search from.[0, fromIndex]
or
-1
if the character sequence is not found.public boolean startsWith(CharSequence prefix)
prefix
- the prefix.true
if the character sequence represented by the
argument is a prefix of the character sequence represented by
this text; false
otherwise.public boolean endsWith(CharSequence suffix)
suffix
- the suffix.true
if the character sequence represented by the
argument is a suffix of the character sequence represented by
this text; false
otherwise.public boolean startsWith(CharSequence prefix, int index)
prefix
- the prefix.index
- the index of the prefix location in this string.this.substring(index).startsWith(prefix)
public Text trim()
public Text intern()
public boolean contentEquals(CharSequence csq)
csq
- the character sequence to compare with.true
if the specified character sequence has the
same character content as this text; false
otherwise.public boolean contentEqualsIgnoreCase(CharSequence csq)
csq
- the CharSequence
to compare this text against.true
if the argument and this text are equal,
ignoring case; false
otherwise.public boolean equals(Object obj)
true
if the specified object is a text having
the same character sequence as this text.
For generic comparaison with any character sequence the
contentEquals(CharSequence)
should be used.public int hashCode()
public int compareTo(CharSequence csq)
compareTo
in interface Comparable<CharSequence>
csq
- the character sequence to be compared.TypeFormat.LEXICAL_COMPARATOR.compare(this, csq)
ClassCastException
- if the specifed object is not a
CharSequence
or a String
.public void printStatistics(PrintStream out)
out
- the stream to use for output (e.g. System.out
)public Text toLowerCase()
Character.toLowerCase(char)
public Text toUpperCase()
Character.toUpperCase(char)
public char charAt(int index)
charAt
in interface CharSequence
index
- the index of the character.IndexOutOfBoundsException
- if (index < 0) ||
(index >= this.length())
public int indexOf(char c)
c
- the character to search for.0
,
or -1
if the character does not occur.public int indexOf(char c, int fromIndex)
c
- the character to search for.fromIndex
- the index to start the search from.fromIndex
,
or -1
if the character does not occur.public int lastIndexOf(char c, int fromIndex)
c
- the character to search for.fromIndex
- the index to start the search backward from.fromIndex
,
or -1
if the character does not occur.public Text subtext(int start, int end)
start
- the index of the first character inclusive.end
- the index of the last character exclusive.IndexOutOfBoundsException
- if (start < 0) || (end < 0) ||
(start > end) || (end > this.length())
public void getChars(int start, int end, char[] dest, int destPos)
start
- the index of the first character to copy.end
- the index after the last character to copy.dest
- the destination array.destPos
- the start offset in the destination array.IndexOutOfBoundsException
- if (start < 0) || (end < 0) ||
(start > end) || (end > this.length())
public String toString()
String
representation of this text.toString
in interface CharSequence
toString
in class Object
java.lang.String
for this text.public Text copy()
public static Text valueOf(char c, int length)
c
- the character to fill this text with.length
- the length of the text returned.IndexOutOfBoundsException
- if (length < 0)
public boolean isBlank()
true
if this text contains only whitespace.public boolean isBlank(int start, int length)
start
- the start index.length
- the number of characters to inspect.public Text trimStart()
public Text trimEnd()
public Text padLeft(int len)
(length()-len)
spaces.len
- the total number of characters to make this text equal to.an
- IllegalArgumentException if the (len<0)
.public Text padLeft(int len, char c)
(length()-len)
pad characters.len
- the total number of characters to make this text equal to.c
- the character to pad using.an
- IllegalArgumentException if the (len<0)
.public Text padRight(int len)
(length()-len)
spaces.len
- the total number of characters to make this text equal to.an
- IllegalArgumentException if the (len<0)
.public Text padRight(int len, char c)
(length()-len)
pad characters.len
- the total number of characters to make this text equal to.c
- the character to pad using.an
- IllegalArgumentException if the (len<0)
.public int indexOfAny(CharSet charSet)
charSet
- the character set.-1
if none.public int indexOfAny(CharSet charSet, int start)
charSet
- the character set.start
- the index of the start of the search region in this text.-1
if none.public int indexOfAny(CharSet charSet, int start, int length)
charSet
- the character set.start
- the index of the start of the search region in this text.length
- the length of the region to search.-1
if none.public int lastIndexOfAny(CharSet charSet)
charSet
- the character set.-1
if none.public int lastIndexOfAny(CharSet charSet, int start)
charSet
- the character set.start
- the index of the start of the search region in this text.-1
if none.public int lastIndexOfAny(CharSet charSet, int start, int length)
charSet
- the character set.start
- the index of the start of the search region in this text.length
- the length of the region to search.-1
if none.Copyright © 2005-2013 Javolution. All Rights Reserved.