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 the
resolve(ResourceManager, Locale)
method fetches the message text
from the passed in ResourceManager
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.
Constructor and Description |
---|
Message(Object resKey)
Creates a new instance of
Message and initializes it with a
resource key. |
Message(Object resGrp,
Object resKey)
Creates a new instance of
Message and initializes it with a
resource group and a resource key. |
Message(Object resGrp,
Object resKey,
Object param)
Creates a new instance of
Message 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 of
Message 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 of
Message and initializes it with a
resource group, a resource key, and an arbitrary number of parameters
that are passed as variable arguments. |
Modifier and Type | Method and Description |
---|---|
static Message |
createFromMessage(Message msg,
Object... params)
Creates a new
Message 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 of
Message 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 this
Message
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.
|
public Message(Object resKey)
Message
and initializes it with a
resource key. The default resource group is used. No parameters are set.resKey
- the resource key (must not be null)IllegalArgumentException
- if the resource key is nullpublic Message(Object resGrp, Object resKey)
Message
and initializes it with a
resource group and a resource key. No parameters are setresGrp
- the resource groupresKey
- the resource key (must not be null)IllegalArgumentException
- if the resource key is nullpublic Message(Object resGrp, Object resKey, Object param)
Message
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.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 this Message
IllegalArgumentException
- if the resource key is nullpublic Message(Object resGrp, Object resKey, Object param1, Object param2)
Message
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.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 this Message
param2
- the second parameter of this Message
IllegalArgumentException
- if the resource key is nullpublic Message(Object resGrp, Object resKey, Object param1, Object param2, Object... params)
Message
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.resGrp
- the resource groupresKey
- the resource key (must not be null)param1
- the first parameter of this Message
param2
- the second parameter of this Message
params
- an arbitrary number of additional parametersIllegalArgumentException
- if the resource key is nullpublic static Message createWithParameters(Object resGrp, Object resKey, Object... params)
Message
with an arbitrary number of
parameters. This factory method can be used instead of the constructors
if the parameters are already available as Object[]
.resGrp
- the resource groupresKey
- the resource key (must not be null)params
- the parametersMessage
instanceIllegalArgumentException
- if the resource key is nullpublic static Message createFromMessage(Message msg, Object... params)
Message
instance with the same resource group and
resource key as the passed in instance, but with different parameters.msg
- the original Message
(must not be null)params
- the new parametersMessage
instanceIllegalArgumentException
- if the original Message
is
nullpublic Object getResourceGroup()
public Object getResourceKey()
public Object[] getParameters()
Message
instance. These parameters are applied to the resource text when calling
the resolve(ResourceManager, Locale)
method. The array returned
by this method is a copy of the actual parameters, so modifying it does
not affect this Message
instance.public String resolve(ResourceManager resMan, Locale locale)
resMan
- the resource managerlocale
- the localeMissingResourceException
- if the resource cannot be resolvedpublic boolean equals(Object obj)
Message
object,
too, and all of its properties are equal to the properties of this
object.public int hashCode()
Copyright © 2016 The JGUIraffe Team. All rights reserved.