Class 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 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.

    Version:
    $Id: Message.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • Message

        public Message​(Object resKey)
        Creates a new instance of Message 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 of Message and initializes it with a resource group and a resource key. No parameters are set
        Parameters:
        resGrp - the resource group
        resKey - 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 of 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.
        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 this Message
        Throws:
        IllegalArgumentException - if the resource key is null
      • Message

        public 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. 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 this Message
        param2 - the second parameter of this Message
        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 of 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.
        Parameters:
        resGrp - the resource group
        resKey - 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 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 of 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[].
        Parameters:
        resGrp - the resource group
        resKey - 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 new Message instance with the same resource group and resource key as the passed in instance, but with different parameters.
        Parameters:
        msg - the original Message (must not be null)
        params - the new parameters
        Returns:
        the new Message instance
        Throws:
        IllegalArgumentException - if the original Message 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 this 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.
        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 manager
        locale - 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 a Message object, too, and all of its properties are equal to the properties of this object.
        Overrides:
        equals in class Object
        Parameters:
        obj - the other object
        Returns:
        a flag whether these objects are equal
      • hashCode

        public int hashCode()
        Calculates a hash code for this message.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code
      • toString

        public String toString()
        Returns a string representation of this object. This string contains the resource key and the resource group and the values of the parameters.
        Overrides:
        toString in class Object
        Returns:
        a string for this object