Class LocalizableMessageBuilder

  • All Implemented Interfaces:
    Serializable, Appendable, CharSequence

    public final class LocalizableMessageBuilder
    extends Object
    implements Appendable, CharSequence, Serializable
    A mutable sequence of localizable messages and their parameters. As messages are appended they are translated to their string representation for storage using the locale specified in the constructor.

    Note that before you use this class you should consider whether it is appropriate. In general composing messages by appending message to each other may not produce a message that is formatted appropriately for all locales.

    It is usually better to create messages by composition. In other words you should create a base message that contains one or more string argument specifiers (%s) and define other message objects to use as replacement variables. In this way language translators have a change to reformat the message for a particular locale if necessary.

    See Also:
    LocalizableMessage, Serialized Form
    • Constructor Detail

      • LocalizableMessageBuilder

        public LocalizableMessageBuilder()
        Creates a new message builder whose content is initially empty.
      • LocalizableMessageBuilder

        public LocalizableMessageBuilder​(LocalizableMessage message)
        Creates a new message builder whose content is initially equal to the provided message.
        Parameters:
        message - The initial content of the message builder.
        Throws:
        NullPointerException - If message was null.
      • LocalizableMessageBuilder

        public LocalizableMessageBuilder​(LocalizableMessageBuilder builder)
        Creates a new message builder whose content is initially equal to the provided message builder.
        Parameters:
        builder - The initial content of the message builder.
        Throws:
        NullPointerException - If builder was null.
      • LocalizableMessageBuilder

        public LocalizableMessageBuilder​(Object object)
        Creates a new message builder whose content is initially equal to the String representation of the provided Object.
        Parameters:
        object - The initial content of the message builder, may be null.
    • Method Detail

      • append

        public LocalizableMessageBuilder append​(char c)
        Appends the provided character to this message builder.
        Specified by:
        append in interface Appendable
        Parameters:
        c - The character to be appended.
        Returns:
        A reference to this message builder.
      • append

        public LocalizableMessageBuilder append​(CharSequence cs,
                                                int start,
                                                int end)
        Appends a subsequence of the provided character sequence to this message builder.

        An invocation of this method of the form append(cs, start, end), behaves in exactly the same way as the invocation

         append(cs.subSequence(start, end))
         
        Specified by:
        append in interface Appendable
        Parameters:
        cs - The character sequence to be appended.
        start - The index of the first character in the subsequence.
        end - The index of the character following the last character in the subsequence.
        Returns:
        A reference to this message builder.
        Throws:
        IndexOutOfBoundsException - If start or end are negative, start is greater than end, or end is greater than csq.length().
        NullPointerException - If cs was null.
      • append

        public LocalizableMessageBuilder append​(int value)
        Appends the provided integer to this message builder.
        Parameters:
        value - The integer to be appended.
        Returns:
        A reference to this message builder.
      • append

        public LocalizableMessageBuilder append​(Object object)
        Appends the String representation of the provided Object to this message builder.
        Parameters:
        object - The object to be appended, may be null.
        Returns:
        A reference to this message builder.
      • charAt

        public char charAt​(int index)
        Returns the char value at the specified index of the String representation of this message builder in the default locale.
        Specified by:
        charAt in interface CharSequence
        Parameters:
        index - The index of the char value to be returned.
        Returns:
        The specified char value.
        Throws:
        IndexOutOfBoundsException - If the index argument is negative or not less than length().
      • charAt

        public char charAt​(Locale locale,
                           int index)
        Returns the char value at the specified index of the String representation of this message builder in the specified locale.
        Parameters:
        locale - The locale.
        index - The index of the char value to be returned.
        Returns:
        The specified char value.
        Throws:
        IndexOutOfBoundsException - If the index argument is negative or not less than length().
        NullPointerException - If locale was null.
      • length

        public int length()
        Returns the length of the String representation of this message builder in the default locale.
        Specified by:
        length in interface CharSequence
        Returns:
        The length of the String representation of this message builder in the default locale.
      • length

        public int length​(Locale locale)
        Returns the length of the String representation of this message builder in the specified locale.
        Parameters:
        locale - The locale.
        Returns:
        The length of the String representation of this message builder in the specified locale.
        Throws:
        NullPointerException - If locale was null.
      • subSequence

        public CharSequence subSequence​(int start,
                                        int end)
        Returns a new CharSequence which is a subsequence of the String representation of this message builder in the default locale. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1 . The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.
        Specified by:
        subSequence in interface CharSequence
        Parameters:
        start - The start index, inclusive.
        end - The end index, exclusive.
        Returns:
        The specified subsequence.
        Throws:
        IndexOutOfBoundsException - If start or end are negative, if end is greater than length(), or if start is greater than end.
      • subSequence

        public CharSequence subSequence​(Locale locale,
                                        int start,
                                        int end)
        Returns a new CharSequence which is a subsequence of the String representation of this message builder in the specified locale. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1 . The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.
        Parameters:
        locale - The locale.
        start - The start index, inclusive.
        end - The end index, exclusive.
        Returns:
        The specified subsequence.
        Throws:
        IndexOutOfBoundsException - If start or end are negative, if end is greater than length(), or if start is greater than end.
        NullPointerException - If locale was null.
      • toMessage

        public LocalizableMessage toMessage()
        Returns the LocalizableMessage representation of this message builder. Subsequent changes to this message builder will not modify the returned LocalizableMessage.
        Returns:
        The LocalizableMessage representation of this message builder.
      • toString

        public String toString()
        Returns the String representation of this message builder in the default locale.
        Specified by:
        toString in interface CharSequence
        Overrides:
        toString in class Object
        Returns:
        The String representation of this message builder.
      • toString

        public String toString​(Locale locale)
        Returns the String representation of this message builder in the specified locale.
        Parameters:
        locale - The locale.
        Returns:
        The String representation of this message builder.
        Throws:
        NullPointerException - If locale was null.