Top Description Inners Fields Constructors Methods
com.ibm.icu.impl

public Class FormattedStringBuilder

extends Object
implements CharSequence, Appendable
Class Inheritance
All Implemented Interfaces
java.lang.Appendable, java.lang.CharSequence
Imports
java.util.Arrays, .HashMap, .Map, com.ibm.icu.text.NumberFormat

A StringBuilder optimized for formatting. It implements the following key features beyond a normal JDK StringBuilder:
  1. Efficient prepend as well as append.
  2. Keeps tracks of Fields in an efficient manner.
  3. String operations are fast-pathed to code point operations when possible.
See also FormattedValueStringBuilderImpl.
Author
sffc (Shane Carr)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface

Field Summary

Modifier and TypeField and Description
pack-priv Object
appendableField

Field applied when Appendable methods are used.

pack-priv int
appendOffset

Number of characters from the end where .append() operations insert.

pack-priv char[]
public static final FormattedStringBuilder
EMPTY

A constant, empty FormattedStringBuilder.

pack-priv Object[]
private static final Map<Object, Character>
pack-priv int
pack-priv int

Constructor Summary

AccessConstructor and Description
public
public
FormattedStringBuilder(int capacity)

public

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

The number of chars added, which is the length of CharSequence.
append
(CharSequence sequence, Object field)

Appends the specified CharSequence to the end of the string.

public int

Returns:

The number of chars added, which is the length of the char array.
append
(char[] chars, Object[] fields)

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.

public int

Returns:

The number of chars added, which is the length of the other FormattedStringBuilder.
append
(FormattedStringBuilder other)

Appends the contents of another FormattedStringBuilder to the end of this instance.

public Appendable
append(CharSequence
The character sequence to append. If csq is null, then the four characters "null" are appended to this Appendable.
csq
)

Implements java.lang.Appendable.append.

This method is provided for Java Appendable compatibility.
public Appendable
append(CharSequence
The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
csq
,
int
The index of the first character in the subsequence
start
,
int
The index of the character following the last character in the subsequence
end
)

Implements java.lang.Appendable.append.

This method is provided for Java Appendable compatibility.
public Appendable
append(char
The character to append
c
)

Implements java.lang.Appendable.append.

This method is provided for Java Appendable compatibility.
public int
appendChar16(char codeUnit, Object field)

public int

Returns:

The number of chars added: 1 if the code point is in the BMP, or 2 otherwise.
appendCodePoint
(int codePoint, Object field)

Appends the specified codePoint to the end of the string.

public char
charAt(int
the index of the char value to be returned
index
)

Implements java.lang.CharSequence.charAt.

Returns the char value at the specified index.
public FormattedStringBuilder
public int
codePointAt(int index)

public int
codePointBefore(int index)

public int
public boolean

Returns:

Whether the contents and field values of this string builder are equal to the given chars and fields.
contentEquals
(char[] chars, Object[] fields)

public boolean

Returns:

Whether the contents of this instance is currently equal to the given instance.
contentEquals
(FormattedStringBuilder
The instance to compare.
other
)

public void
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public Object
fieldAt(int index)

private int
public int
public int
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public int

Returns:

The number of chars added, which is the length of CharSequence.
insert
(int index, CharSequence sequence, Object field)

Inserts the specified CharSequence at the specified index in the string.

public int

Returns:

The number of chars added, which is the length of CharSequence.
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).

public int

Returns:

The number of chars added, which is the length of the char array.
insert
(int index, char[] chars, Object[] fields)

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.

public int

Returns:

The number of chars added, which is the length of the other FormattedStringBuilder.
insert
(int index, FormattedStringBuilder other)

Inserts the contents of another FormattedStringBuilder into this instance at the given index.

public int
insertChar16(int index, char codeUnit, Object field)

public int

Returns:

The number of chars added: 1 if the code point is in the BMP, or 2 otherwise.
insertCodePoint
(int index, int codePoint, Object field)

Inserts the specified codePoint at the specified index in the string.

public int
length()

Implements java.lang.CharSequence.length.

Returns the length of this character sequence.
private int

Returns:

The position in the char array to insert the chars.
prepareForInsert
(int
The location in the string where the operation is to take place.
index
,
int
The number of chars (UTF-16 code units) to be inserted at that location.
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".

public void
setAppendableField(Object
The field used when inserting strings.
field
)

Call this method before using any of the Appendable overrides.

public void
setAppendIndex(int
The index at which append operations should insert.
index
)

Sets the index at which append operations insert.

public int

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.
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.

public CharSequence
subSequence(int
the start index, inclusive
start
,
int
the end index, exclusive
end
)

Implements java.lang.CharSequence.subSequence.

Deprecated

Note

this returns a FormattedStringBuilder.

public String
subString(int start, int end)

Use this instead of subSequence if returning publicly.

public char[]

Returns:

A new array containing the contents of this string builder.
toCharArray
()

public String

Returns:

A string for debugging purposes.
toDebugString
()

Returns a string that includes field information, for debugging purposes.

public Object[]

Returns:

A new array containing the field values of this string builder.
toFieldArray
()

public String
toString()

Overrides java.lang.Object.toString.

Implements java.lang.CharSequence.toString.

Returns the string represented by the characters in this string builder.
public static Format.Field
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

appendableFieldback to summary
pack-priv Object appendableField

Field applied when Appendable methods are used.

appendOffsetback to summary
pack-priv int appendOffset

Number of characters from the end where .append() operations insert.

charsback to summary
pack-priv char[] chars
EMPTYback to summary
public static final FormattedStringBuilder EMPTY

A constant, empty FormattedStringBuilder. Do NOT call mutative operations on this.

fieldsback to summary
pack-priv Object[] fields
fieldToDebugCharback to summary
private static final Map<Object, Character> fieldToDebugChar
lengthback to summary
pack-priv int length
zeroback to summary
pack-priv int zero

Constructor Detail

FormattedStringBuilderback to summary
public FormattedStringBuilder()
FormattedStringBuilderback to summary
public FormattedStringBuilder(int capacity)
FormattedStringBuilderback to summary
public FormattedStringBuilder(FormattedStringBuilder source)

Method Detail

appendback to summary
public int append(CharSequence sequence, Object field)

Appends the specified CharSequence to the end of the string.

Returns:int

The number of chars added, which is the length of CharSequence.

appendback to summary
public int append(char[] chars, Object[] fields)

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:int

The number of chars added, which is the length of the char array.

appendback to summary
public int append(FormattedStringBuilder other)

Appends the contents of another FormattedStringBuilder to the end of this instance.

Returns:int

The number of chars added, which is the length of the other FormattedStringBuilder.

appendback to summary
public Appendable append(CharSequence csq)

Implements java.lang.Appendable.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 call setAppendableField first.

Parameters
csq:CharSequence

Doc from java.lang.Appendable.append.

The character sequence to append. If csq is null, then the four characters "null" are appended to this Appendable.

Returns:Appendable

Doc from java.lang.Appendable.append.

A reference to this Appendable

Annotations
@Override
appendback to summary
public Appendable append(CharSequence csq, int start, int end)

Implements java.lang.Appendable.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 call setAppendableField first.

Parameters
csq:CharSequence

Doc from java.lang.Appendable.append.

The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".

start:int

Doc from java.lang.Appendable.append.

The index of the first character in the subsequence

end:int

Doc from java.lang.Appendable.append.

The index of the character following the last character in the subsequence

Returns:Appendable

Doc from java.lang.Appendable.append.

A reference to this Appendable

Annotations
@Override
appendback to summary
public Appendable append(char c)

Implements java.lang.Appendable.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 call setAppendableField first.

Parameters
c:char

Doc from java.lang.Appendable.append.

The character to append

Returns:Appendable

Doc from java.lang.Appendable.append.

A reference to this Appendable

Annotations
@Override
appendChar16back to summary
public int appendChar16(char codeUnit, Object field)
appendCodePointback to summary
public int appendCodePoint(int codePoint, Object field)

Appends the specified codePoint to the end of the string.

Returns:int

The number of chars added: 1 if the code point is in the BMP, or 2 otherwise.

charAtback to summary
public char charAt(int index)

Implements java.lang.CharSequence.charAt.

Doc from java.lang.CharSequence.charAt.

Returns the char value at the specified index. An index ranges from zero to length() - 1. The first char value of the sequence is at index zero, the next at index one, and so on, as for array indexing.

If the char value specified by the index is a surrogate, the surrogate value is returned.

Parameters
index:int

the index of the char value to be returned

Returns:char

the specified char value

Annotations
@Override
clearback to summary
public FormattedStringBuilder clear()
codePointAtback to summary
public int codePointAt(int index)
codePointBeforeback to summary
public int codePointBefore(int index)
codePointCountback to summary
public int codePointCount()
contentEqualsback to summary
public boolean contentEquals(char[] chars, Object[] fields)
Returns:boolean

Whether the contents and field values of this string builder are equal to the given chars and fields.

See Also
toCharArray, toFieldArray
contentEqualsback to summary
public boolean contentEquals(FormattedStringBuilder other)
Parameters
other:FormattedStringBuilder

The instance to compare.

Returns:boolean

Whether the contents of this instance is currently equal to the given instance.

copyFromback to summary
public void copyFrom(FormattedStringBuilder source)
equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
fieldAtback to summary
public Object fieldAt(int index)
getCapacityback to summary
private int getCapacity()
getFirstCodePointback to summary
public int getFirstCodePoint()
getLastCodePointback to summary
public int getLastCodePoint()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
insertback to summary
public int insert(int index, CharSequence sequence, Object field)

Inserts the specified CharSequence at the specified index in the string.

Returns:int

The number of chars added, which is the length of CharSequence.

insertback to summary
public 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).

Returns:int

The number of chars added, which is the length of CharSequence.

insertback to summary
public int insert(int index, char[] chars, Object[] fields)

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:int

The number of chars added, which is the length of the char array.

insertback to summary
public int insert(int index, FormattedStringBuilder other)

Inserts the contents of another FormattedStringBuilder into this instance at the given index.

Returns:int

The number of chars added, which is the length of the other FormattedStringBuilder.

insertChar16back to summary
public int insertChar16(int index, char codeUnit, Object field)
insertCodePointback to summary
public int insertCodePoint(int index, int codePoint, Object field)

Inserts the specified codePoint at the specified index in the string.

Returns:int

The number of chars added: 1 if the code point is in the BMP, or 2 otherwise.

lengthback to summary
public int length()

Implements java.lang.CharSequence.length.

Doc from java.lang.CharSequence.length.

Returns the length of this character sequence. The length is the number of 16-bit chars in the sequence.

Returns:int

the number of chars in this sequence

Annotations
@Override
prepareForInsertback to summary
private int prepareForInsert(int index, int count)

Shifts around existing data if necessary to make room for new characters.

Parameters
index:int

The location in the string where the operation is to take place.

count:int

The number of chars (UTF-16 code units) to be inserted at that location.

Returns:int

The position in the char array to insert the chars.

prepareForInsertHelperback to summary
private int prepareForInsertHelper(int index, int count)
removeback to summary
private int remove(int index, int count)

Removes the "count" chars starting at "index". Returns the position at which the chars were removed.

setAppendableFieldback to summary
public void setAppendableField(Object field)

Call this method before using any of the Appendable overrides.

Parameters
field:Object

The field used when inserting strings.

setAppendIndexback to summary
public void setAppendIndex(int index)

Sets the index at which append operations insert. Defaults to the end.

Parameters
index:int

The index at which append operations should insert.

spliceback to summary
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:int

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.

subSequenceback to summary
public CharSequence subSequence(int start, int end)

Implements java.lang.CharSequence.subSequence.

Deprecated

Note

this returns a FormattedStringBuilder. Do not return publicly.

Parameters
start:int

Doc from java.lang.CharSequence.subSequence.

the start index, inclusive

end:int

Doc from java.lang.CharSequence.subSequence.

the end index, exclusive

Returns:CharSequence

Doc from java.lang.CharSequence.subSequence.

the specified subsequence

Annotations
@Override
@Deprecated
subStringback to summary
public String subString(int start, int end)

Use this instead of subSequence if returning publicly.

toCharArrayback to summary
public char[] toCharArray()
Returns:char[]

A new array containing the contents of this string builder.

toDebugStringback to summary
public String 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:String

A string for debugging purposes.

toFieldArrayback to summary
public Object[] toFieldArray()
Returns:Object[]

A new array containing the field values of this string builder.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implements java.lang.CharSequence.toString.

Returns the string represented by the characters in this string builder.

For a string intended be used for debugging, use toDebugString.

Returns:String

Doc from java.lang.CharSequence.toString.

a string consisting of exactly this sequence of characters

Annotations
@Override
unwrapFieldback to summary
public static Format.Field unwrapField(Object field)
com.ibm.icu.impl back to summary

public Interface FormattedStringBuilder.FieldWrapper

Known Direct Implementers
com.ibm.icu.impl.FormattedValueStringBuilderImpl.SpanFieldPlaceholder

Method Summary

Modifier and TypeMethod and Description
public Format.Field

Method Detail

unwrapback to summary
public Format.Field unwrap()