Class TextBuilder

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Appendable, java.lang.CharSequence

    public class TextBuilder
    extends java.lang.Object
    implements java.lang.Appendable, java.lang.CharSequence, java.io.Serializable

    An Appendable text whose capacity expands gently without incurring expensive resize/copy operations ever.

    This class is not intended for large documents manipulations.

    Version:
    5.3, January 20, 2008
    Author:
    Jean-Marie Dautelle
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      TextBuilder()
      Creates a text builder of small initial capacity.
      TextBuilder​(int capacity)
      Creates a text builder of specified initial capacity.
      TextBuilder​(java.lang.String str)
      Creates a text builder holding the specified String (convenience method).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      TextBuilder append​(boolean b)
      Appends the textual representation of the specified boolean argument.
      TextBuilder append​(char c)
      Appends the specified character.
      TextBuilder append​(char[] chars)
      Appends the characters from the char array argument.
      TextBuilder append​(char[] chars, int offset, int length)
      Appends the characters from a subarray of the char array argument.
      TextBuilder append​(int i)
      Appends the decimal representation of the specified int argument.
      TextBuilder append​(int i, int radix)
      Appends the radix representation of the specified int argument.
      TextBuilder append​(long l)
      Appends the decimal representation of the specified long argument.
      TextBuilder append​(long l, int radix)
      Appends the radix representation of the specified long argument.
      TextBuilder append​(java.lang.CharSequence csq)
      Appends the specified character sequence.
      TextBuilder append​(java.lang.CharSequence csq, int start, int end)
      Appends a subsequence of the specified character sequence.
      TextBuilder append​(java.lang.String str)
      Appends the specified string to this text builder.
      TextBuilder append​(java.lang.String str, int start, int end)
      Appends a subsequence of the specified string.
      char charAt​(int index)
      Returns the character at the specified index.
      TextBuilder clear()
      Removes all the characters of this text builder (equivalent to this.delete(start, this.length())).
      boolean contentEquals​(java.lang.CharSequence csq)
      Indicates if this text builder has the same character content as the specified character sequence.
      TextBuilder delete​(int start, int end)
      Removes the characters between the specified indices.
      boolean equals​(java.lang.Object obj)
      Compares this text builder against the specified object for equality.
      int hashCode()
      Returns the hash code for this text builder.
      TextBuilder insert​(int index, java.lang.CharSequence csq)
      Inserts the specified character sequence at the specified location.
      int length()
      Returns the length (character count) of this text builder.
      TextBuilder reverse()
      Reverses this character sequence.
      void setCharAt​(int index, char c)
      Sets the character at the specified position.
      void setLength​(int newLength)
      Convenience method equivalent to setLength(newLength, '').
      void setLength​(int newLength, char fillChar)
      Sets the length of this character builder.
      java.lang.CharSequence subSequence​(int start, int end)
      Returns a CharSequence corresponding to the character sequence between the specified indexes.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.CharSequence

        chars, codePoints, toString
    • Constructor Detail

      • TextBuilder

        public TextBuilder()
        Creates a text builder of small initial capacity.
      • TextBuilder

        public TextBuilder​(java.lang.String str)
        Creates a text builder holding the specified String (convenience method).
        Parameters:
        str - the initial string content of this text builder.
      • TextBuilder

        public TextBuilder​(int capacity)
        Creates a text builder of specified initial capacity. Unless the text length exceeds the specified capacity, operations on this text builder will not allocate memory.
        Parameters:
        capacity - the initial capacity.
    • Method Detail

      • length

        public final int length()
        Returns the length (character count) of this text builder.
        Specified by:
        length in interface java.lang.CharSequence
        Returns:
        the number of characters (16-bits Unicode).
      • charAt

        public final char charAt​(int index)
        Returns the character at the specified index.
        Specified by:
        charAt in interface java.lang.CharSequence
        Parameters:
        index - the index of the character.
        Returns:
        the character at the specified index.
        Throws:
        java.lang.IndexOutOfBoundsException - if (index < 0) || (index >= this.length()).
      • setCharAt

        public final void setCharAt​(int index,
                                    char c)
        Sets the character at the specified position.
        Parameters:
        index - the index of the character to modify.
        c - the new character.
        Throws:
        java.lang.IndexOutOfBoundsException - if (index < 0) || (index >= this.length())
      • setLength

        public final void setLength​(int newLength)
        Convenience method equivalent to setLength(newLength, '').
        Parameters:
        newLength - the new length of this builder.
        Throws:
        java.lang.IndexOutOfBoundsException - if (newLength < 0)
      • setLength

        public final void setLength​(int newLength,
                                    char fillChar)
        Sets the length of this character builder. If the length is greater than the current length; the specified character is inserted.
        Parameters:
        newLength - the new length of this builder.
        fillChar - the character to be appended if required.
        Throws:
        java.lang.IndexOutOfBoundsException - if (newLength < 0)
      • subSequence

        public final java.lang.CharSequence subSequence​(int start,
                                                        int end)
        Returns a CharSequence corresponding to the character sequence between the specified indexes.
        Specified by:
        subSequence in interface java.lang.CharSequence
        Parameters:
        start - the index of the first character inclusive.
        end - the index of the last character exclusive.
        Returns:
        a character sequence.
        Throws:
        java.lang.IndexOutOfBoundsException - if (start < 0) || (end < 0) || (start > end) || (end > this.length())
      • append

        public final TextBuilder append​(char c)
        Appends the specified character.
        Specified by:
        append in interface java.lang.Appendable
        Parameters:
        c - the character to append.
        Returns:
        this
      • append

        public final TextBuilder append​(java.lang.CharSequence csq)
        Appends the specified character sequence. If the specified character sequence is null this method is equivalent to append("null").
        Specified by:
        append in interface java.lang.Appendable
        Parameters:
        csq - the character sequence to append or null.
        Returns:
        this
      • append

        public final TextBuilder append​(java.lang.CharSequence csq,
                                        int start,
                                        int end)
        Appends a subsequence of the specified character sequence. If the specified character sequence is null this method is equivalent to append("null").
        Specified by:
        append in interface java.lang.Appendable
        Parameters:
        csq - the character sequence to append or null.
        start - the index of the first character to append.
        end - the index after the last character to append.
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException - if (start < 0) || (end < 0) || (start > end) || (end > csq.length())
      • append

        public final TextBuilder append​(java.lang.String str)
        Appends the specified string to this text builder. If the specified string is null this method is equivalent to append("null").
        Parameters:
        str - the string to append or null.
        Returns:
        this
      • append

        public final TextBuilder append​(java.lang.String str,
                                        int start,
                                        int end)
        Appends a subsequence of the specified string. If the specified character sequence is null this method is equivalent to append("null").
        Parameters:
        str - the string to append or null.
        start - the index of the first character to append.
        end - the index after the last character to append.
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException - if (start < 0) || (end < 0) || (start > end) || (end > str.length())
      • append

        public final TextBuilder append​(char[] chars)
        Appends the characters from the char array argument.
        Parameters:
        chars - the character array source.
        Returns:
        this
      • append

        public final TextBuilder append​(char[] chars,
                                        int offset,
                                        int length)
        Appends the characters from a subarray of the char array argument.
        Parameters:
        chars - the character array source.
        offset - the index of the first character to append.
        length - the number of character to append.
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException - if (offset < 0) || (length < 0) || ((offset + length) > chars.length)
      • append

        public final TextBuilder append​(boolean b)
        Appends the textual representation of the specified boolean argument.
        Parameters:
        b - the boolean to format.
        Returns:
        this
      • append

        public final TextBuilder append​(int i)
        Appends the decimal representation of the specified int argument.
        Parameters:
        i - the int to format.
        Returns:
        this
      • append

        public final TextBuilder append​(int i,
                                        int radix)
        Appends the radix representation of the specified int argument.
        Parameters:
        i - the int to format.
        radix - the radix (e.g. 16 for hexadecimal).
        Returns:
        this
      • append

        public final TextBuilder append​(long l)
        Appends the decimal representation of the specified long argument.
        Parameters:
        l - the long to format.
        Returns:
        this
      • append

        public final TextBuilder append​(long l,
                                        int radix)
        Appends the radix representation of the specified long argument.
        Parameters:
        l - the long to format.
        radix - the radix (e.g. 16 for hexadecimal).
        Returns:
        this
      • insert

        public final TextBuilder insert​(int index,
                                        java.lang.CharSequence csq)
        Inserts the specified character sequence at the specified location.
        Parameters:
        index - the insertion position.
        csq - the character sequence being inserted.
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException - if (index < 0) || (index > this.length())
      • clear

        public final TextBuilder clear()
        Removes all the characters of this text builder (equivalent to this.delete(start, this.length())).
        Returns:
        this.delete(0, this.length())
      • delete

        public final TextBuilder delete​(int start,
                                        int end)
        Removes the characters between the specified indices.
        Parameters:
        start - the beginning index, inclusive.
        end - the ending index, exclusive.
        Returns:
        this
        Throws:
        java.lang.IndexOutOfBoundsException - if (start < 0) || (end < 0) || (start > end) || (end > this.length())
      • reverse

        public final TextBuilder reverse()
        Reverses this character sequence.
        Returns:
        this
      • hashCode

        public final int hashCode()
        Returns the hash code for this text builder.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code value.
      • equals

        public final boolean equals​(java.lang.Object obj)
        Compares this text builder against the specified object for equality. Returns true if the specified object is a text builder having the same character content.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to compare with or null.
        Returns:
        true if that is a text builder with the same character content as this text; false otherwise.
      • contentEquals

        public final boolean contentEquals​(java.lang.CharSequence csq)
        Indicates if this text builder has the same character content as the specified character sequence.
        Parameters:
        csq - the character sequence to compare with.
        Returns:
        true if the specified character sequence has the same character content as this text; false otherwise.