Class FormattedString


  • public class FormattedString
    extends java.lang.Object
    Represents a String with formatting instructions. Each formatted string consists of a number of AbstractFormattedStringElements and can be transformed in a standard String either with or without the formatting instructions. If formatting info is included in that String, this is done by adding the "[FORMATTED]" at the start of the string to inform any reader that there are formatting tokens to be found. These formatting tokens start with '[', followed by a token name, followed optionally by '|' plus a replacement string value (to be inserted into the string instead of the token if the formatting info is to be stripped from the string instead of being interpreted), and finally closed by ']'.

    Note that other than the standard String, this "formatted string" is a mutable object, hence it behaves more like a StringBuilder in practice!

    Author:
    Rene Schneider - initial API and implementation
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(AbstractFormattedStringElement anElement)
      Adds the element to the formatted string.
      void add​(AbstractFormattedStringElement... someElements)
      Adds the provided elements to the formatted string.
      void add​(FormattedString aFormattedString)
      Adds all elements of the provided formatted string to this formatted string.
      void add​(java.lang.String aString)
      Adds a plaintext element without formatting flags to the formatted string.
      void add​(java.lang.String... someStrings)
      Adds multiple plaintext elements without formatting flags to the formatted string.
      void add​(java.lang.String aString, FormattedStringElement.FormatFlag... someFlags)
      Adds a text element with the provided formatting flags to the formatted string.
      void addMultiple​(AbstractFormattedStringElement anElement, int aNumber)
      Adds the provided element multiple times to the formatted string.
      protected void clearCache()
      Clears any cached serialized String values.
      boolean equals​(java.lang.Object anOtherObject)  
      int getElementCount()  
      int hashCode()  
      java.lang.String toFormattedString()
      Serializes the formatted string elements into a single string with formatting tags embedded.
      java.lang.String toFormattedStringLengthLimited​(int aMaxLength)
      Serializes the formatted string elements into a single string with formatting tags embedded, but only if the resulting string is below a certain length.
      java.lang.String toString()  
      java.lang.String toUnformattedString()
      Serializes the formatted string elements into a single string without any formatting tags.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • FORMATTED_STRING_START_TOKEN

        public static final java.lang.String FORMATTED_STRING_START_TOKEN
        This token starts a formatted string.
        See Also:
        Constant Field Values
      • unformattedValueCache

        protected java.lang.String unformattedValueCache
        The unformatted serialized value of this formatted string. Used for caching purposes.
      • formattedValueCache

        protected java.lang.String formattedValueCache
        The formatted serialized value of this formatted string. Used for caching purposes.
    • Constructor Detail

      • FormattedString

        public FormattedString()
        Creates a new, empty formatted string.
      • FormattedString

        public FormattedString​(java.lang.String aText)
        Creates a new formatted string with one plaintext element.
        Parameters:
        aText - the new string content
      • FormattedString

        public FormattedString​(java.lang.String aText,
                               FormattedStringElement.FormatFlag... someFlags)
        Creates a new formatted string with one text element, to which the provided format flags are added.
        Parameters:
        aText - the new string content
        someFlags - the format flags to apply to the string content
      • FormattedString

        public FormattedString​(AbstractFormattedStringElement... someElements)
        Creates a new formatted string with the supplied elements.
        Parameters:
        someElements - the elements to add to the string
    • Method Detail

      • add

        public void add​(AbstractFormattedStringElement anElement)
        Adds the element to the formatted string. Elements are always added at the end.
        Parameters:
        anElement - the element to be added
      • addMultiple

        public void addMultiple​(AbstractFormattedStringElement anElement,
                                int aNumber)
        Adds the provided element multiple times to the formatted string. The element is NOT cloned, but the same instance is added multiple times.
        Parameters:
        anElement - the element to be added
        aNumber - the number of times that the element is to be added
      • add

        public void add​(java.lang.String aString)
        Adds a plaintext element without formatting flags to the formatted string.
        Parameters:
        aString - the text for the plaintext element
      • add

        public void add​(java.lang.String aString,
                        FormattedStringElement.FormatFlag... someFlags)
        Adds a text element with the provided formatting flags to the formatted string.
        Parameters:
        aString - the text for the element
        someFlags - the formatting flags to apply
      • add

        public void add​(AbstractFormattedStringElement... someElements)
        Adds the provided elements to the formatted string.
        Parameters:
        someElements - the elements to add
      • add

        public void add​(FormattedString aFormattedString)
        Adds all elements of the provided formatted string to this formatted string.
        Parameters:
        aFormattedString - the formatted string whose elements are to be added
      • add

        public void add​(java.lang.String... someStrings)
        Adds multiple plaintext elements without formatting flags to the formatted string.
        Parameters:
        someStrings - the strings to add as plaintext elements
      • clearCache

        protected void clearCache()
        Clears any cached serialized String values.
      • toFormattedString

        public java.lang.String toFormattedString()
        Serializes the formatted string elements into a single string with formatting tags embedded.
        Returns:
        the string with formatting tags
      • toFormattedStringLengthLimited

        public java.lang.String toFormattedStringLengthLimited​(int aMaxLength)
        Serializes the formatted string elements into a single string with formatting tags embedded, but only if the resulting string is below a certain length. If it exceeds this length, an unformatted string is returned.
        Parameters:
        aMaxLength - the maximum length in Unicode code units.
        Returns:
        either a formatted or unformatted string
      • toUnformattedString

        public java.lang.String toUnformattedString()
        Serializes the formatted string elements into a single string without any formatting tags.
        Returns:
        the string without formatting tags
      • getElementCount

        public int getElementCount()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object anOtherObject)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object