Class LocalizableMessage
- java.lang.Object
-
- org.forgerock.i18n.LocalizableMessage
-
- All Implemented Interfaces:
Serializable
,CharSequence
,Comparable<LocalizableMessage>
,Formattable
public final class LocalizableMessage extends Object implements CharSequence, Formattable, Comparable<LocalizableMessage>, Serializable
A localizable message whoseString
representation can be retrieved in one or more locales. A message is localized each time it is converted to aString
using one of itstoString()
methods.Localizable messages are particularly useful in situations where a message a destined for multiple recipients, potentially in different locales. For example, a server application may record a message in its log file using its default locale, but also send the same message to the client using the client's locale (if known).
In most cases messages are intended for use in a locale-sensitive manner although this class defines convenience methods for creating non-localizable messages whose
String
representation is always the same regardless of the requested locale.This class implements
CharSequence
so that messages can be supplied as arguments to other messages. This way messages can be composed of fragments of other messages if necessary.- See Also:
LocalizableMessageBuilder
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static LocalizableMessage
EMPTY
Represents an empty message string.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description char
charAt(int index)
Returns thechar
value at the specified index of theString
representation of this message in the default locale.char
charAt(Locale locale, int index)
Returns thechar
value at the specified index of theString
representation of this message in the specified locale.int
compareTo(LocalizableMessage message)
Compares this message with the specified message for order in the default locale.boolean
equals(Object o)
Returnstrue
if the provided object is a message whoseString
representation is equal to theString
representation of this message in the default locale.void
formatTo(Formatter formatter, int flags, int width, int precision)
Formats this message using the providedFormatter
.int
hashCode()
Returns the hash code value for this message calculated using the hash code of theString
representation of this message in the default locale.boolean
isEmpty()
Returns whether theString
representation of this message is empty.int
length()
Returns the length of theString
representation of this message in the default locale.int
length(Locale locale)
Returns the length of theString
representation of this message in the specified locale.int
ordinal()
Returns the ordinal associated with this message, or-1
if undefined.static LocalizableMessage
raw(CharSequence string)
Creates an non-localizable message whoseString
representation is always the same regardless of the requested locale.static LocalizableMessage
raw(CharSequence formatString, Object... args)
Creates an non-localizable message whoseString
representation is always the same regardless of the requested locale.String
resourceName()
Returns the name of the resource in which this message is defined.CharSequence
subSequence(int start, int end)
Returns a newCharSequence
which is a subsequence of theString
representation of this message 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 in the specified locale.String
toString()
Returns theString
representation of this message in the default locale.String
toString(Locale locale)
Returns theString
representation of this message in the specified locale.static LocalizableMessage
valueOf(Object object)
Creates a new message whose content is theString
representation of the providedObject
.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.CharSequence
chars, codePoints
-
-
-
-
Field Detail
-
EMPTY
public static final LocalizableMessage EMPTY
Represents an empty message string.
-
-
Method Detail
-
raw
public static LocalizableMessage raw(CharSequence string)
Creates an non-localizable message whoseString
representation is always the same regardless of the requested locale.- Parameters:
string
- The raw message string.- Returns:
- A non-localizable messages whose
String
representation is always the same regardless of the requested locale. - Throws:
NullPointerException
- Ifstring
wasnull
.
-
raw
public static LocalizableMessage raw(CharSequence formatString, Object... args)
Creates an non-localizable message whoseString
representation is always the same regardless of the requested locale.Note that the types for
args
must be consistent with any argument specifiers appearing informatString
according to the rules ofFormatter
. A mismatch in type information will cause this message to render without argument substitution. Before using this method you should be sure that the message you are creating is not locale sensitive. If it is locale sensitive consider defining an appropriateLocalizableMessageDescriptor
.This method handles the special case where a
CharSequence
needs to be converted directly toLocalizableMessage
as follows:String s = ...; // Both of these are equivalent: LocalizableMessage m = LocalizableMessage.raw(s); LocalizableMessage m = LocalizableMessage.raw("%s", s);
- Parameters:
formatString
- The raw message format string.args
- The raw message parameters.- Returns:
- A non-localizable messages whose
String
representation is always the same regardless of the requested locale. - Throws:
NullPointerException
- IfformatString
wasnull
.
-
valueOf
public static LocalizableMessage valueOf(Object object)
Creates a new message whose content is theString
representation of the providedObject
.- Parameters:
object
- The object to be converted to a message, may benull
.- Returns:
- The new message.
-
charAt
public char charAt(int index)
Returns thechar
value at the specified index of theString
representation of this message 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 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
.
-
compareTo
public int compareTo(LocalizableMessage message)
Compares this message with the specified message for order in the default locale. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.- Specified by:
compareTo
in interfaceComparable<LocalizableMessage>
- Parameters:
message
- The message to be compared.- Returns:
- A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
-
equals
public boolean equals(Object o)
Returnstrue
if the provided object is a message whoseString
representation is equal to theString
representation of this message in the default locale.
-
formatTo
public void formatTo(Formatter formatter, int flags, int width, int precision)
Formats this message using the providedFormatter
.- Specified by:
formatTo
in interfaceFormattable
- Parameters:
formatter
- TheFormatter
.flags
- The flags modify the output format. The value is interpreted as a bitmask. Any combination of the following flags may be set:FormattableFlags.LEFT_JUSTIFY
,FormattableFlags.UPPERCASE
, andFormattableFlags.ALTERNATE
. If no flags are set, the default formatting of the implementing class will apply.width
- The minimum number of characters to be written to the output. If the length of the converted value is less than thewidth
then the output will be padded by white space until the total number of characters equals width. The padding is at the beginning by default. If theFormattableFlags.LEFT_JUSTIFY
flag is set then the padding will be at the end. Ifwidth
is-1
then there is no minimum.precision
- The maximum number of characters to be written to the output. The precision is applied before the width, thus the output will be truncated toprecision
characters even if thewidth
is greater than theprecision
. Ifprecision
is-1
then there is no explicit limit on the number of characters.- Throws:
IllegalFormatException
- If any of the parameters are invalid. For specification of all possible formatting errors, see the Details section of the formatter class specification.
-
hashCode
public int hashCode()
Returns the hash code value for this message calculated using the hash code of theString
representation of this message in the default locale.
-
length
public int length()
Returns the length of theString
representation of this message in the default locale.- Specified by:
length
in interfaceCharSequence
- Returns:
- The length of the
String
representation of this message in the default locale.
-
isEmpty
public boolean isEmpty()
Returns whether theString
representation of this message is empty.- Returns:
- whether the
String
representation of this message is empty.
-
length
public int length(Locale locale)
Returns the length of theString
representation of this message in the specified locale.- Parameters:
locale
- The locale.- Returns:
- The length of the
String
representation of this message in the specified locale. - Throws:
NullPointerException
- Iflocale
wasnull
.
-
ordinal
public int ordinal()
Returns the ordinal associated with this message, or-1
if undefined. A message can be uniquely identified by its resource name and ordinal.This may be useful when an application wishes to identify the source of a message. For example, a logging implementation could log the resource name in addition to the ordinal in order to unambiguously identify a message in a locale independent way.
- Returns:
- The ordinal associated with this descriptor, or
-1
if undefined. - See Also:
resourceName()
-
resourceName
public String resourceName()
Returns the name of the resource in which this message is defined. A message can be uniquely identified by its resource name and ordinal.This may be useful when an application wishes to identify the source of a message. For example, a logging implementation could log the resource name in addition to the ordinal in order to unambiguously identify a message in a locale independent way.
The resource name may be used for obtaining named loggers, e.g. using SLF4J's
org.slf4j.LoggerFactory#getLogger(String name)
.- Returns:
- The name of the resource in which this message is defined, or
null
if this message is a raw message and its source is undefined. - See Also:
ordinal()
-
subSequence
public CharSequence subSequence(int start, int end)
Returns a newCharSequence
which is a subsequence of theString
representation of this message 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 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
.
-
toString
public String toString()
Returns theString
representation of this message in the default locale.- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
- Returns:
- The
String
representation of this message.
-
toString
public String toString(Locale locale)
Returns theString
representation of this message in the specified locale.- Parameters:
locale
- The locale.- Returns:
- The
String
representation of this message. - Throws:
NullPointerException
- Iflocale
wasnull
.
-
-