Package net.sf.jguiraffe.resources
Class Message
- java.lang.Object
-
- net.sf.jguiraffe.resources.Message
-
public class Message extends Object
A convenience class for dealing with localized messages that can have parameters.
Instances of this class can be initialized with a resource group and a resource key of a message and optionally with a set of parameters. The text resource referred to can contain placeholders as supported by
java.text.MessageFormat
. When the message is to be displayed theresolve(ResourceManager, Locale)
method fetches the message text from the passed inResourceManager
and replaces the placeholders by actual parameter values.Instances of this class are immutable (provided that the parameters are immutable) and thus can be shared between multiple threads.
- Version:
- $Id: Message.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description Message(Object resKey)
Creates a new instance ofMessage
and initializes it with a resource key.Message(Object resGrp, Object resKey)
Creates a new instance ofMessage
and initializes it with a resource group and a resource key.Message(Object resGrp, Object resKey, Object param)
Creates a new instance ofMessage
and initializes it with a resource group, a resource key, and a single parameter.Message(Object resGrp, Object resKey, Object param1, Object param2)
Creates a new instance ofMessage
and initializes it with a resource group, a resource key, and two parameters.Message(Object resGrp, Object resKey, Object param1, Object param2, Object... params)
Creates a new instance ofMessage
and initializes it with a resource group, a resource key, and an arbitrary number of parameters that are passed as variable arguments.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Message
createFromMessage(Message msg, Object... params)
Creates a newMessage
instance with the same resource group and resource key as the passed in instance, but with different parameters.static Message
createWithParameters(Object resGrp, Object resKey, Object... params)
Creates an instance ofMessage
with an arbitrary number of parameters.boolean
equals(Object obj)
Tests if this message equals another object.Object[]
getParameters()
Returns an array with the parameters stored in thisMessage
instance.Object
getResourceGroup()
Returns the resource group.Object
getResourceKey()
Returns the resource key.int
hashCode()
Calculates a hash code for this message.String
resolve(ResourceManager resMan, Locale locale)
Resolves this message and returns its text value in the given locale.String
toString()
Returns a string representation of this object.
-
-
-
Constructor Detail
-
Message
public Message(Object resKey)
Creates a new instance ofMessage
and initializes it with a resource key. The default resource group is used. No parameters are set.- Parameters:
resKey
- the resource key (must not be null)- Throws:
IllegalArgumentException
- if the resource key is null
-
Message
public Message(Object resGrp, Object resKey)
Creates a new instance ofMessage
and initializes it with a resource group and a resource key. No parameters are set- Parameters:
resGrp
- the resource groupresKey
- the resource key (must not be null)- Throws:
IllegalArgumentException
- if the resource key is null
-
Message
public Message(Object resGrp, Object resKey, Object param)
Creates a new instance ofMessage
and initializes it with a resource group, a resource key, and a single parameter. This constructor can be used for the frequent case that only a single parameter is needed.- Parameters:
resGrp
- the resource group (can be null for the default resource group)resKey
- the resource key (must not be null)param
- the single parameter of thisMessage
- Throws:
IllegalArgumentException
- if the resource key is null
-
Message
public Message(Object resGrp, Object resKey, Object param1, Object param2)
Creates a new instance ofMessage
and initializes it with a resource group, a resource key, and two parameters. This constructor can be used for the case that exactly two parameters are needed.- Parameters:
resGrp
- the resource group (can be null for the default resource group)resKey
- the resource key (must not be null)param1
- the first parameter of thisMessage
param2
- the second parameter of thisMessage
- Throws:
IllegalArgumentException
- if the resource key is null
-
Message
public Message(Object resGrp, Object resKey, Object param1, Object param2, Object... params)
Creates a new instance ofMessage
and initializes it with a resource group, a resource key, and an arbitrary number of parameters that are passed as variable arguments. Use this constructor if the number of parameters is greater than 2. For one or two parameters there are specialized constructors that are more efficient as there is no need to create an array.- Parameters:
resGrp
- the resource groupresKey
- the resource key (must not be null)param1
- the first parameter of thisMessage
param2
- the second parameter of thisMessage
params
- an arbitrary number of additional parameters- Throws:
IllegalArgumentException
- if the resource key is null
-
-
Method Detail
-
createWithParameters
public static Message createWithParameters(Object resGrp, Object resKey, Object... params)
Creates an instance ofMessage
with an arbitrary number of parameters. This factory method can be used instead of the constructors if the parameters are already available asObject[]
.- Parameters:
resGrp
- the resource groupresKey
- the resource key (must not be null)params
- the parameters- Returns:
- the
Message
instance - Throws:
IllegalArgumentException
- if the resource key is null
-
createFromMessage
public static Message createFromMessage(Message msg, Object... params)
Creates a newMessage
instance with the same resource group and resource key as the passed in instance, but with different parameters.- Parameters:
msg
- the originalMessage
(must not be null)params
- the new parameters- Returns:
- the new
Message
instance - Throws:
IllegalArgumentException
- if the originalMessage
is null
-
getResourceGroup
public Object getResourceGroup()
Returns the resource group.- Returns:
- the resource group
-
getResourceKey
public Object getResourceKey()
Returns the resource key.- Returns:
- the resource key
-
getParameters
public Object[] getParameters()
Returns an array with the parameters stored in thisMessage
instance. These parameters are applied to the resource text when calling theresolve(ResourceManager, Locale)
method. The array returned by this method is a copy of the actual parameters, so modifying it does not affect thisMessage
instance.- Returns:
- an array with the parameter values
-
resolve
public String resolve(ResourceManager resMan, Locale locale)
Resolves this message and returns its text value in the given locale. If parameters are defined, they are replaced.- Parameters:
resMan
- the resource managerlocale
- the locale- Returns:
- the text of the message
- Throws:
MissingResourceException
- if the resource cannot be resolved
-
equals
public boolean equals(Object obj)
Tests if this message equals another object. This method returns true only if the other object is aMessage
object, too, and all of its properties are equal to the properties of this object.
-
hashCode
public int hashCode()
Calculates a hash code for this message.
-
-