Package org.forgerock.i18n
Class LocalizableMessageBuilder
- java.lang.Object
-
- org.forgerock.i18n.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 Summary
Constructors Constructor Description LocalizableMessageBuilder()
Creates a new message builder whose content is initially empty.LocalizableMessageBuilder(Object object)
Creates a new message builder whose content is initially equal to theString
representation of the providedObject
.LocalizableMessageBuilder(LocalizableMessage message)
Creates a new message builder whose content is initially equal to the provided message.LocalizableMessageBuilder(LocalizableMessageBuilder builder)
Creates a new message builder whose content is initially equal to the provided message builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description LocalizableMessageBuilder
append(char c)
Appends the provided character to this message builder.LocalizableMessageBuilder
append(int value)
Appends the provided integer to this message builder.LocalizableMessageBuilder
append(CharSequence cs)
Appends the provided character sequence to this message builder.LocalizableMessageBuilder
append(CharSequence cs, int start, int end)
Appends a subsequence of the provided character sequence to this message builder.LocalizableMessageBuilder
append(Object object)
Appends theString
representation of the providedObject
to this message builder.LocalizableMessageBuilder
append(LocalizableMessage message)
Appends the provided message to this message builder.char
charAt(int index)
Returns thechar
value at the specified index of theString
representation of this message builder in the default locale.char
charAt(Locale locale, int index)
Returns thechar
value at the specified index of theString
representation of this message builder in the specified locale.int
length()
Returns the length of theString
representation of this message builder in the default locale.int
length(Locale locale)
Returns the length of theString
representation of this message builder in the specified locale.CharSequence
subSequence(int start, int end)
Returns a newCharSequence
which is a subsequence of theString
representation of this message builder in the default locale.CharSequence
subSequence(Locale locale, int start, int end)
Returns a newCharSequence
which is a subsequence of theString
representation of this message builder in the specified locale.LocalizableMessage
toMessage()
Returns theLocalizableMessage
representation of this message builder.String
toString()
Returns theString
representation of this message builder in the default locale.String
toString(Locale locale)
Returns theString
representation of this message builder in the specified locale.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.CharSequence
chars, codePoints
-
-
-
-
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
- Ifmessage
wasnull
.
-
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
- Ifbuilder
wasnull
.
-
LocalizableMessageBuilder
public LocalizableMessageBuilder(Object object)
Creates a new message builder whose content is initially equal to theString
representation of the providedObject
.- Parameters:
object
- The initial content of the message builder, may benull
.
-
-
Method Detail
-
append
public LocalizableMessageBuilder append(char c)
Appends the provided character to this message builder.- Specified by:
append
in interfaceAppendable
- Parameters:
c
- The character to be appended.- Returns:
- A reference to this message builder.
-
append
public LocalizableMessageBuilder append(CharSequence cs)
Appends the provided character sequence to this message builder.- Specified by:
append
in interfaceAppendable
- Parameters:
cs
- The character sequence to be appended.- Returns:
- A reference to this message builder.
- Throws:
NullPointerException
- Ifcs
wasnull
.
-
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 invocationappend(cs.subSequence(start, end))
- Specified by:
append
in interfaceAppendable
- 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
- Ifstart
orend
are negative,start
is greater thanend
, orend
is greater thancsq.length()
.NullPointerException
- Ifcs
wasnull
.
-
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(LocalizableMessage message)
Appends the provided message to this message builder.- Parameters:
message
- The message to be appended.- Returns:
- A reference to this message builder.
- Throws:
NullPointerException
- Ifmessage
wasnull
.
-
append
public LocalizableMessageBuilder append(Object object)
Appends theString
representation of the providedObject
to this message builder.- Parameters:
object
- The object to be appended, may benull
.- Returns:
- A reference to this message builder.
-
charAt
public char charAt(int index)
Returns thechar
value at the specified index of theString
representation of this message builder in the default locale.- Specified by:
charAt
in interfaceCharSequence
- Parameters:
index
- The index of thechar
value to be returned.- Returns:
- The specified
char
value. - Throws:
IndexOutOfBoundsException
- If theindex
argument is negative or not less thanlength()
.
-
charAt
public char charAt(Locale locale, int index)
Returns thechar
value at the specified index of theString
representation of this message builder in the specified locale.- Parameters:
locale
- The locale.index
- The index of thechar
value to be returned.- Returns:
- The specified
char
value. - Throws:
IndexOutOfBoundsException
- If theindex
argument is negative or not less thanlength()
.NullPointerException
- Iflocale
wasnull
.
-
length
public int length()
Returns the length of theString
representation of this message builder in the default locale.- Specified by:
length
in interfaceCharSequence
- 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 theString
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
- Iflocale
wasnull
.
-
subSequence
public CharSequence subSequence(int start, int end)
Returns a newCharSequence
which is a subsequence of theString
representation of this message builder in the default locale. The subsequence starts with thechar
value at the specified index and ends with thechar
value at indexend - 1
. The length (inchar
s) of the returned sequence isend - start
, so ifstart == end
then an empty sequence is returned.- Specified by:
subSequence
in interfaceCharSequence
- Parameters:
start
- The start index, inclusive.end
- The end index, exclusive.- Returns:
- The specified subsequence.
- Throws:
IndexOutOfBoundsException
- Ifstart
orend
are negative, ifend
is greater thanlength()
, or ifstart
is greater thanend
.
-
subSequence
public CharSequence subSequence(Locale locale, int start, int end)
Returns a newCharSequence
which is a subsequence of theString
representation of this message builder in the specified locale. The subsequence starts with thechar
value at the specified index and ends with thechar
value at indexend - 1
. The length (inchar
s) of the returned sequence isend - start
, so ifstart == 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
- Ifstart
orend
are negative, ifend
is greater thanlength()
, or ifstart
is greater thanend
.NullPointerException
- Iflocale
wasnull
.
-
toMessage
public LocalizableMessage toMessage()
Returns theLocalizableMessage
representation of this message builder. Subsequent changes to this message builder will not modify the returnedLocalizableMessage
.- Returns:
- The
LocalizableMessage
representation of this message builder.
-
toString
public String toString()
Returns theString
representation of this message builder in the default locale.- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
- Returns:
- The
String
representation of this message builder.
-
toString
public String toString(Locale locale)
Returns theString
representation of this message builder in the specified locale.- Parameters:
locale
- The locale.- Returns:
- The
String
representation of this message builder. - Throws:
NullPointerException
- Iflocale
wasnull
.
-
-