Package com.ibm.icu.impl
Class FormattedStringBuilder
java.lang.Object
com.ibm.icu.impl.FormattedStringBuilder
- All Implemented Interfaces:
Appendable
,CharSequence
A StringBuilder optimized for formatting. It implements the following key features beyond a
normal JDK StringBuilder:
- Efficient prepend as well as append.
- Keeps tracks of Fields in an efficient manner.
- String operations are fast-pathed to code point operations when possible.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescription(package private) Object
Field applied when Appendable methods are used.(package private) int
Number of characters from the end where .append() operations insert.(package private) char[]
static final FormattedStringBuilder
A constant, empty FormattedStringBuilder.(package private) Object[]
(package private) int
(package private) int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappend
(char c) This method is provided for Java Appendable compatibility.int
Appends the chars in the specified char array to the end of the string, and associates them with the fields in the specified field array, which must have the same length as chars.int
append
(FormattedStringBuilder other) Appends the contents of anotherFormattedStringBuilder
to the end of this instance.append
(CharSequence csq) This method is provided for Java Appendable compatibility.append
(CharSequence csq, int start, int end) This method is provided for Java Appendable compatibility.int
append
(CharSequence sequence, Object field) Appends the specified CharSequence to the end of the string.int
appendChar16
(char codeUnit, Object field) int
appendCodePoint
(int codePoint, Object field) Appends the specified codePoint to the end of the string.char
charAt
(int index) clear()
int
codePointAt
(int index) int
codePointBefore
(int index) int
boolean
contentEquals
(char[] chars, Object[] fields) boolean
void
copyFrom
(FormattedStringBuilder source) boolean
fieldAt
(int index) private int
int
int
int
hashCode()
int
Inserts the chars in the specified char array at the specified index in the string, and associates them with the fields in the specified field array, which must have the same length as chars.int
insert
(int index, FormattedStringBuilder other) Inserts the contents of anotherFormattedStringBuilder
into this instance at the given index.int
insert
(int index, CharSequence sequence, int start, int end, Object field) Inserts the specified CharSequence at the specified index in the string, reading from the CharSequence from start (inclusive) to end (exclusive).int
insert
(int index, CharSequence sequence, Object field) Inserts the specified CharSequence at the specified index in the string.int
insertChar16
(int index, char codeUnit, Object field) int
insertCodePoint
(int index, int codePoint, Object field) Inserts the specified codePoint at the specified index in the string.int
length()
private int
prepareForInsert
(int index, int count) Shifts around existing data if necessary to make room for new characters.private int
prepareForInsertHelper
(int index, int count) private int
remove
(int index, int count) Removes the "count" chars starting at "index".void
setAppendableField
(Object field) Call this method before using any of the Appendable overrides.void
setAppendIndex
(int index) Sets the index at which append operations insert.int
splice
(int startThis, int endThis, CharSequence sequence, int startOther, int endOther, Object field) Replaces the chars between startThis and endThis with the chars between startOther and endOther of the given CharSequence.subSequence
(int start, int end) Deprecated.subString
(int start, int end) Use this instead of subSequence if returning publicly.char[]
Returns a string that includes field information, for debugging purposes.Object[]
toString()
Returns the string represented by the characters in this string builder.static Format.Field
unwrapField
(Object field) Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.CharSequence
chars, codePoints, isEmpty
-
Field Details
-
EMPTY
A constant, empty FormattedStringBuilder. Do NOT call mutative operations on this. -
chars
char[] chars -
fields
Object[] fields -
zero
int zero -
length
int length -
appendOffset
int appendOffsetNumber of characters from the end where .append() operations insert. -
appendableField
Object appendableFieldField applied when Appendable methods are used. -
fieldToDebugChar
-
-
Constructor Details
-
FormattedStringBuilder
public FormattedStringBuilder() -
FormattedStringBuilder
public FormattedStringBuilder(int capacity) -
FormattedStringBuilder
-
-
Method Details
-
unwrapField
-
copyFrom
-
length
public int length()- Specified by:
length
in interfaceCharSequence
-
codePointCount
public int codePointCount() -
charAt
public char charAt(int index) - Specified by:
charAt
in interfaceCharSequence
-
fieldAt
-
getFirstCodePoint
public int getFirstCodePoint() -
getLastCodePoint
public int getLastCodePoint() -
codePointAt
public int codePointAt(int index) -
codePointBefore
public int codePointBefore(int index) -
clear
-
setAppendIndex
public void setAppendIndex(int index) Sets the index at which append operations insert. Defaults to the end.- Parameters:
index
- The index at which append operations should insert.
-
appendChar16
-
insertChar16
-
appendCodePoint
Appends the specified codePoint to the end of the string.- Returns:
- The number of chars added: 1 if the code point is in the BMP, or 2 otherwise.
-
insertCodePoint
Inserts the specified codePoint at the specified index in the string.- Returns:
- The number of chars added: 1 if the code point is in the BMP, or 2 otherwise.
-
append
Appends the specified CharSequence to the end of the string.- Returns:
- The number of chars added, which is the length of CharSequence.
-
insert
Inserts the specified CharSequence at the specified index in the string.- Returns:
- The number of chars added, which is the length of CharSequence.
-
insert
Inserts the specified CharSequence at the specified index in the string, reading from the CharSequence from start (inclusive) to end (exclusive).- Returns:
- The number of chars added, which is the length of CharSequence.
-
splice
public int splice(int startThis, int endThis, CharSequence sequence, int startOther, int endOther, Object field) Replaces the chars between startThis and endThis with the chars between startOther and endOther of the given CharSequence. Calling this method with startThis == endThis is equivalent to calling insert.- Returns:
- The number of chars added, which may be negative if the removed segment is longer than the length of the CharSequence segment that was inserted.
-
append
Appends the chars in the specified char array to the end of the string, and associates them with the fields in the specified field array, which must have the same length as chars.- Returns:
- The number of chars added, which is the length of the char array.
-
insert
Inserts the chars in the specified char array at the specified index in the string, and associates them with the fields in the specified field array, which must have the same length as chars.- Returns:
- The number of chars added, which is the length of the char array.
-
append
Appends the contents of anotherFormattedStringBuilder
to the end of this instance.- Returns:
- The number of chars added, which is the length of the other
FormattedStringBuilder
.
-
insert
Inserts the contents of anotherFormattedStringBuilder
into this instance at the given index.- Returns:
- The number of chars added, which is the length of the other
FormattedStringBuilder
.
-
prepareForInsert
private int prepareForInsert(int index, int count) Shifts around existing data if necessary to make room for new characters.- Parameters:
index
- The location in the string where the operation is to take place.count
- The number of chars (UTF-16 code units) to be inserted at that location.- Returns:
- The position in the char array to insert the chars.
-
prepareForInsertHelper
private int prepareForInsertHelper(int index, int count) -
remove
private int remove(int index, int count) Removes the "count" chars starting at "index". Returns the position at which the chars were removed. -
getCapacity
private int getCapacity() -
subSequence
Deprecated.Note: this returns a FormattedStringBuilder. Do not return publicly.- Specified by:
subSequence
in interfaceCharSequence
-
subString
Use this instead of subSequence if returning publicly. -
toString
Returns the string represented by the characters in this string builder.For a string intended be used for debugging, use
toDebugString()
.- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
-
toDebugString
Returns a string that includes field information, for debugging purposes.For example, if the string is "-12.345", the debug string will be something like "<FormattedStringBuilder [-123.45] [-iii.ff]>"
- Returns:
- A string for debugging purposes.
-
toCharArray
public char[] toCharArray()- Returns:
- A new array containing the contents of this string builder.
-
toFieldArray
- Returns:
- A new array containing the field values of this string builder.
-
setAppendableField
Call this method before using any of the Appendable overrides.- Parameters:
field
- The field used when inserting strings.
-
append
This method is provided for Java Appendable compatibility. In most cases, please use the append methods that take a Field parameter. If you do use this method, you must callsetAppendableField(java.lang.Object)
first.- Specified by:
append
in interfaceAppendable
-
append
This method is provided for Java Appendable compatibility. In most cases, please use the append methods that take a Field parameter. If you do use this method, you must callsetAppendableField(java.lang.Object)
first.- Specified by:
append
in interfaceAppendable
-
append
This method is provided for Java Appendable compatibility. In most cases, please use the append methods that take a Field parameter. If you do use this method, you must callsetAppendableField(java.lang.Object)
first.- Specified by:
append
in interfaceAppendable
-
contentEquals
- Returns:
- Whether the contents and field values of this string builder are equal to the given chars and fields.
- See Also:
-
contentEquals
- Parameters:
other
- The instance to compare.- Returns:
- Whether the contents of this instance is currently equal to the given instance.
-
hashCode
public int hashCode() -
equals
-