Class MagellanMessageImpl

  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Comparable<java.lang.Object>, Identifiable, Message, Unique

    public class MagellanMessageImpl
    extends MagellanIdentifiableImpl
    implements Message
    A class for representing a message.

    The new format of messages in Eressea CR versions >= 41 made it necessary to reconsider this class. Mainly, messages can now have an id and attributes.

    Two special attributes are available directly via the corresponding get/set methods.

    First, this is the type attribute (tag ;type in the cr) denoting the type of the message. It is transformed into a MessageType object.

    Second, there is the text attribute (tag ;rendered in the cr). By design this attribute should actually be created by rendering the message according to the message type's pattern and the other attributes. This class does contain a render() method, still, it is too primitive to yield acceptable results, so it is preferable to take the rendered message text directly from the cr.

    Note, that for historic reasons, a Message object might have no type or attributes and an invalid id of -1.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      java.util.Map<java.lang.String,​java.lang.String> attributes
      The attributes of this message.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addAttribute​(java.lang.String key, java.lang.String value)  
      int compareTo​(java.lang.Object o)
      Imposes a natural ordering on Identifiable objects.
      boolean containsAttribute​(java.lang.String key)  
      boolean equals​(java.lang.Object o)
      Indicates whether this Message object is equal to another object.
      java.lang.String getAttribute​(java.lang.String key)  
      java.util.List<java.lang.String> getAttributeKeys()  
      java.util.Map<java.lang.String,​java.lang.String> getAttributes()
      The attributes of this message.
      int getAttributeSize()  
      IntegerID getID()
      Returns an ID for this message.
      MessageType getMessageType()
      Returns the MessageType of this message.
      java.lang.String getText()
      Gets the rendered message text. 1.
      int hashCode()
      As we want to use the hashCode/equals contract we need to force the implementation of hashCode.
      boolean isAcknowledged()  
      void render​(GameData data)
      Renders the message and updates the message text.
      static java.lang.String render​(GameData data, java.lang.String pattern, java.util.Map<java.lang.String,​java.lang.String> attributes)
      Renders a message text from the given pattern and attributes.
      void setAcknowledged​(boolean ack)
      Sets the new acknowledged status.
      void setAttributes​(java.util.Map<java.lang.String,​java.lang.String> attributes)
      The attributes of this message.
      void setText​(java.lang.String text)
      Sets the text of this message to text.
      void setType​(MessageType type)
      Sets the MessageType of this message.
      java.lang.String toString()
      Fiete 20080121 toString is also copied to clipboard when using ctrl+c changing this now to the text value.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • attributes

        public java.util.Map<java.lang.String,​java.lang.String> attributes
        The attributes of this message. The keys are the keys of the attribute, the values object pairs of the attributes' keys and values.
    • Constructor Detail

      • MagellanMessageImpl

        public MagellanMessageImpl​(java.lang.String text)
        Creates a new Message object.
      • MagellanMessageImpl

        public MagellanMessageImpl​(IntegerID id)
        Creates a new Message object.
      • MagellanMessageImpl

        public MagellanMessageImpl​(ID id,
                                   java.lang.String text)
        Creates a new Message object.
      • MagellanMessageImpl

        public MagellanMessageImpl​(ID id,
                                   MessageType type,
                                   java.util.Map<java.lang.String,​java.lang.String> attributes)
        Creates a new Message object.
      • MagellanMessageImpl

        public MagellanMessageImpl​(Message msg)
        Creates a new Message object.
      • MagellanMessageImpl

        public MagellanMessageImpl​(ID id,
                                   java.lang.String text,
                                   MessageType type,
                                   java.util.Map<java.lang.String,​java.lang.String> attributes)
        Creates a new Message object. If text is not null the text is set directly; if it is null, it is rendered from the type and attributes.
        Parameters:
        id - id ID of the Message
        text - The message text
        type - The message type
        attributes - The attributes
    • Method Detail

      • getAttributes

        public java.util.Map<java.lang.String,​java.lang.String> getAttributes()
        The attributes of this message. The keys are the keys of the attribute, the values object pairs of the attributes' keys and values.
        Specified by:
        getAttributes in interface Message
      • setAttributes

        public void setAttributes​(java.util.Map<java.lang.String,​java.lang.String> attributes)
        The attributes of this message. The keys are the keys of the attribute, the values object pairs of the attributes' keys and values.
        Specified by:
        setAttributes in interface Message
      • getText

        public java.lang.String getText()
        Gets the rendered message text. 1. If a rendered version of the msg is available use it. 2. If not rendered but msgtype available render the msg.
        Specified by:
        getText in interface Message
        Returns:
        The message text
      • setText

        public void setText​(java.lang.String text)
        Sets the text of this message to text.
        Specified by:
        setText in interface Message
        Parameters:
        text - The new text
      • getMessageType

        public MessageType getMessageType()
        Returns the MessageType of this message.
        Specified by:
        getMessageType in interface Message
        Returns:
        The message type
      • setType

        public void setType​(MessageType type)
        Sets the MessageType of this message.
        Specified by:
        setType in interface Message
        Parameters:
        type - The new message type
      • render

        public static java.lang.String render​(GameData data,
                                              java.lang.String pattern,
                                              java.util.Map<java.lang.String,​java.lang.String> attributes)
        Renders a message text from the given pattern and attributes.

        Expects the tokens of the form {name1 name2} and replaces them by the attribute values for name1, name2 etc. If these values are unit names or region coordinates, their names are taken from the provided GameData.

        WARNING! This does not work for the current cr format (41) which expects tokens of the form $unit($unit).

        Parameters:
        data - The game for replacing unit IDs and region coordinates
        pattern - The pattern to render
        attributes - A map of (String,Value)-pairs for replacing tokens in the pattern
        Returns:
        The rendered text as string
      • render

        public void render​(GameData data)
        Renders the message and updates the message text.
        Specified by:
        render in interface Message
        Parameters:
        data - The GameData for replacing unit IDs and region coordinates
      • equals

        public boolean equals​(java.lang.Object o)
        Indicates whether this Message object is equal to another object. Returns true only if o is not null and an instance of class Message and o's id is equal to the id of this Message object. 2002.02.21 pavkovic: Also the message text has to be the same for Messages with ambiguous IntegerID(-1).
        Specified by:
        equals in interface Identifiable
        Specified by:
        equals in interface Message
        Overrides:
        equals in class MagellanIdentifiableImpl
        See Also:
        Object.equals(java.lang.Object)
      • toString

        public java.lang.String toString()
        Fiete 20080121 toString is also copied to clipboard when using ctrl+c changing this now to the text value.
        Specified by:
        toString in interface Message
        Overrides:
        toString in class java.lang.Object