Class FormattedStringBuilder

java.lang.Object
com.ibm.icu.impl.FormattedStringBuilder
All Implemented Interfaces:
Appendable, CharSequence

public class FormattedStringBuilder extends Object implements CharSequence, Appendable
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.
  • Field Details

    • EMPTY

      public static final FormattedStringBuilder 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 appendOffset
      Number of characters from the end where .append() operations insert.
    • appendableField

      Object appendableField
      Field applied when Appendable methods are used.
    • fieldToDebugChar

      private static final Map<Object,Character> fieldToDebugChar
  • Constructor Details

    • FormattedStringBuilder

      public FormattedStringBuilder()
    • FormattedStringBuilder

      public FormattedStringBuilder(int capacity)
    • FormattedStringBuilder

      public FormattedStringBuilder(FormattedStringBuilder source)
  • Method Details

    • unwrapField

      public static Format.Field unwrapField(Object field)
    • copyFrom

      public void copyFrom(FormattedStringBuilder source)
    • length

      public int length()
      Specified by:
      length in interface CharSequence
    • codePointCount

      public int codePointCount()
    • charAt

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

      public Object fieldAt(int index)
    • getFirstCodePoint

      public int getFirstCodePoint()
    • getLastCodePoint

      public int getLastCodePoint()
    • codePointAt

      public int codePointAt(int index)
    • codePointBefore

      public int codePointBefore(int index)
    • clear

      public FormattedStringBuilder 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

      public int appendChar16(char codeUnit, Object field)
    • insertChar16

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

      public int appendCodePoint(int codePoint, Object field)
      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

      public int insertCodePoint(int index, int codePoint, Object field)
      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

      public int append(CharSequence sequence, Object field)
      Appends the specified CharSequence to the end of the string.
      Returns:
      The number of chars added, which is the length of CharSequence.
    • insert

      public int insert(int index, CharSequence sequence, Object field)
      Inserts the specified CharSequence at the specified index in the string.
      Returns:
      The number of chars added, which is the length of CharSequence.
    • insert

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

      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:
      The number of chars added, which is the length of the char array.
    • insert

      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:
      The number of chars added, which is the length of the char array.
    • append

      public int append(FormattedStringBuilder other)
      Appends the contents of another FormattedStringBuilder to the end of this instance.
      Returns:
      The number of chars added, which is the length of the other FormattedStringBuilder.
    • insert

      public int insert(int index, FormattedStringBuilder other)
      Inserts the contents of another FormattedStringBuilder 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 public CharSequence subSequence(int start, int end)
      Deprecated.
      Note: this returns a FormattedStringBuilder. Do not return publicly.
      Specified by:
      subSequence in interface CharSequence
    • subString

      public String subString(int start, int end)
      Use this instead of subSequence if returning publicly.
    • toString

      public String 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 interface CharSequence
      Overrides:
      toString in class Object
    • toDebugString

      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:
      A string for debugging purposes.
    • toCharArray

      public char[] toCharArray()
      Returns:
      A new array containing the contents of this string builder.
    • toFieldArray

      public Object[] toFieldArray()
      Returns:
      A new array containing the field values of this string builder.
    • setAppendableField

      public void setAppendableField(Object field)
      Call this method before using any of the Appendable overrides.
      Parameters:
      field - The field used when inserting strings.
    • append

      public Appendable append(CharSequence csq)
      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(java.lang.Object) first.
      Specified by:
      append in interface Appendable
    • append

      public Appendable append(CharSequence csq, int start, int end)
      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(java.lang.Object) first.
      Specified by:
      append in interface Appendable
    • append

      public Appendable append(char c)
      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(java.lang.Object) first.
      Specified by:
      append in interface Appendable
    • contentEquals

      public boolean contentEquals(char[] chars, Object[] fields)
      Returns:
      Whether the contents and field values of this string builder are equal to the given chars and fields.
      See Also:
    • contentEquals

      public boolean contentEquals(FormattedStringBuilder other)
      Parameters:
      other - The instance to compare.
      Returns:
      Whether the contents of this instance is currently equal to the given instance.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object