Class LocalizableMessage
- All Implemented Interfaces:
Serializable,CharSequence,Comparable<LocalizableMessage>,Formattable
String representation can be retrieved in
one or more locales. A message is localized each time it is converted to a
String using one of its toString() 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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final LocalizableMessageRepresents an empty message string. -
Method Summary
Modifier and TypeMethodDescriptioncharcharAt(int index) Returns thecharvalue at the specified index of theStringrepresentation of this message in the default locale.charReturns thecharvalue at the specified index of theStringrepresentation of this message in the specified locale.intcompareTo(LocalizableMessage message) Compares this message with the specified message for order in the default locale.booleanReturnstrueif the provided object is a message whoseStringrepresentation is equal to theStringrepresentation of this message in the default locale.voidFormats this message using the providedFormatter.inthashCode()Returns the hash code value for this message calculated using the hash code of theStringrepresentation of this message in the default locale.booleanisEmpty()Returns whether theStringrepresentation of this message is empty.intlength()Returns the length of theStringrepresentation of this message in the default locale.intReturns the length of theStringrepresentation of this message in the specified locale.intordinal()Returns the ordinal associated with this message, or-1if undefined.static LocalizableMessageraw(CharSequence string) Creates an non-localizable message whoseStringrepresentation is always the same regardless of the requested locale.static LocalizableMessageraw(CharSequence formatString, Object... args) Creates an non-localizable message whoseStringrepresentation is always the same regardless of the requested locale.Returns the name of the resource in which this message is defined.subSequence(int start, int end) Returns a newCharSequencewhich is a subsequence of theStringrepresentation of this message in the default locale.subSequence(Locale locale, int start, int end) Returns a newCharSequencewhich is a subsequence of theStringrepresentation of this message in the specified locale.toString()Returns theStringrepresentation of this message in the default locale.Returns theStringrepresentation of this message in the specified locale.static LocalizableMessageCreates a new message whose content is theStringrepresentation of the providedObject.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
chars, codePoints
-
Field Details
-
EMPTY
Represents an empty message string.
-
-
Method Details
-
raw
Creates an non-localizable message whoseStringrepresentation is always the same regardless of the requested locale.- Parameters:
string- The raw message string.- Returns:
- A non-localizable messages whose
Stringrepresentation is always the same regardless of the requested locale. - Throws:
NullPointerException- Ifstringwasnull.
-
raw
Creates an non-localizable message whoseStringrepresentation is always the same regardless of the requested locale.Note that the types for
argsmust be consistent with any argument specifiers appearing informatStringaccording 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
CharSequenceneeds to be converted directly toLocalizableMessageas 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
Stringrepresentation is always the same regardless of the requested locale. - Throws:
NullPointerException- IfformatStringwasnull.
-
valueOf
Creates a new message whose content is theStringrepresentation 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 thecharvalue at the specified index of theStringrepresentation of this message in the default locale.- Specified by:
charAtin interfaceCharSequence- Parameters:
index- The index of thecharvalue to be returned.- Returns:
- The specified
charvalue. - Throws:
IndexOutOfBoundsException- If theindexargument is negative or not less thanlength().
-
charAt
Returns thecharvalue at the specified index of theStringrepresentation of this message in the specified locale.- Parameters:
locale- The locale.index- The index of thecharvalue to be returned.- Returns:
- The specified
charvalue. - Throws:
IndexOutOfBoundsException- If theindexargument is negative or not less thanlength().NullPointerException- Iflocalewasnull.
-
compareTo
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:
compareToin 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
Returnstrueif the provided object is a message whoseStringrepresentation is equal to theStringrepresentation of this message in the default locale. -
formatTo
Formats this message using the providedFormatter.- Specified by:
formatToin 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 thewidththen 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_JUSTIFYflag is set then the padding will be at the end. Ifwidthis-1then 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 toprecisioncharacters even if thewidthis greater than theprecision. Ifprecisionis-1then 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 theStringrepresentation of this message in the default locale. -
length
public int length()Returns the length of theStringrepresentation of this message in the default locale.- Specified by:
lengthin interfaceCharSequence- Returns:
- The length of the
Stringrepresentation of this message in the default locale.
-
isEmpty
public boolean isEmpty()Returns whether theStringrepresentation of this message is empty.- Specified by:
isEmptyin interfaceCharSequence- Returns:
- whether the
Stringrepresentation of this message is empty.
-
length
Returns the length of theStringrepresentation of this message in the specified locale.- Parameters:
locale- The locale.- Returns:
- The length of the
Stringrepresentation of this message in the specified locale. - Throws:
NullPointerException- Iflocalewasnull.
-
ordinal
public int ordinal()Returns the ordinal associated with this message, or-1if 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
-1if undefined. - See Also:
-
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
nullif this message is a raw message and its source is undefined. - See Also:
-
subSequence
Returns a newCharSequencewhich is a subsequence of theStringrepresentation of this message in the default locale. The subsequence starts with thecharvalue at the specified index and ends with thecharvalue at indexend - 1. The length (inchars) of the returned sequence isend - start, so ifstart == endthen an empty sequence is returned.- Specified by:
subSequencein interfaceCharSequence- Parameters:
start- The start index, inclusive.end- The end index, exclusive.- Returns:
- The specified subsequence.
- Throws:
IndexOutOfBoundsException- Ifstartorendare negative, ifendis greater thanlength(), or ifstartis greater thanend.
-
subSequence
Returns a newCharSequencewhich is a subsequence of theStringrepresentation of this message in the specified locale. The subsequence starts with thecharvalue at the specified index and ends with thecharvalue at indexend - 1. The length (inchars) of the returned sequence isend - start, so ifstart == endthen 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- Ifstartorendare negative, ifendis greater thanlength(), or ifstartis greater thanend.NullPointerException- Iflocalewasnull.
-
toString
Returns theStringrepresentation of this message in the default locale.- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject- Returns:
- The
Stringrepresentation of this message.
-
toString
Returns theStringrepresentation of this message in the specified locale.- Parameters:
locale- The locale.- Returns:
- The
Stringrepresentation of this message. - Throws:
NullPointerException- Iflocalewasnull.
-