Class Message

  • Direct Known Subclasses:
    TrivialMessage

    public abstract class Message
    extends java.lang.Object
    Base abstract class for all messages.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.String,​java.lang.reflect.Constructor<? extends Message>> builders
      A map of message name to message constructors, built on the fly as new messages are encountered and suitable constructors found.
      protected static java.util.logging.Logger logger  
      static java.util.Comparator<Message> messagePriorityComparator
      Comparator comparing by message priority.
      private static java.lang.Class[] readClasses
      Classes used by Message.read()
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Message()
      Deliberately trivial constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract void aiHandler​(FreeColServer freeColServer, AIPlayer aiPlayer)
      AI-side handler for this message.
      protected abstract <T extends FreeColObject>
      void
      appendChild​(T fco)
      Append a new child.
      protected abstract <T extends FreeColObject>
      void
      appendChildren​(java.util.Collection<T> fcos)
      Append a multiple new children.
      boolean canMerge()
      Does this message consist only of mergeable attributes?
      protected void clientGeneric​(FreeColClient freeColClient)
      Create a runnable to execute generic message actions.
      abstract void clientHandler​(FreeColClient freeColClient)
      Client-side handler for this message.
      abstract boolean currentPlayerMessage()
      Should this message only be sent to a server by the current player?
      protected void expected​(java.lang.String wanted, java.lang.String got)
      Complain about finding the wrong XML element.
      protected java.util.List<java.lang.String> getArrayAttributes()
      Get the array attributes of this message.
      protected java.lang.Boolean getBooleanAttribute​(java.lang.String key, java.lang.Boolean defaultValue)
      Get a boolean attribute value.
      protected <T extends FreeColObject>
      T
      getChild​(int index, java.lang.Class<T> returnClass)
      Get a child object.
      protected abstract int getChildCount()
      Get the number of child objects.
      protected abstract java.util.List<FreeColObject> getChildren()
      Get the child objects of this message.
      protected <T extends FreeColObject>
      java.util.List<T>
      getChildren​(java.lang.Class<T> returnClass)
      Get the child objects.
      protected <T extends java.lang.Enum<T>>
      T
      getEnumAttribute​(java.lang.String key, java.lang.Class<T> returnClass, T defaultValue)
      Gets an enum attribute value.
      protected java.lang.Integer getIntegerAttribute​(java.lang.String key, int defaultValue)
      Get an integer attribute value.
      abstract Message.MessagePriority getPriority()
      Get the priority of this type of message.
      int getPriorityLevel()
      Get the priority level of this type of message.
      protected abstract java.lang.String getStringAttribute​(java.lang.String key)
      Get a string attribute value.
      protected abstract java.util.Map<java.lang.String,​java.lang.String> getStringAttributeMap()
      Get a map of all the attributes in this message.
      abstract java.lang.String getType()
      Get the message tag.
      protected abstract boolean hasAttribute​(java.lang.String key)
      Checks if an attribute is present in this message.
      protected InGameController igc​(FreeColClient freeColClient)  
      protected InGameController igc​(FreeColServer freeColServer)  
      protected void invokeAndWait​(FreeColClient freeColClient, java.lang.Runnable runnable)  
      protected void invokeLater​(FreeColClient freeColClient, java.lang.Runnable runnable)  
      protected boolean isEmpty()
      Is this message vacuous?
      boolean isType​(java.lang.String type)
      Checks if this message is of a given type.
      boolean merge​(Message message)
      Merge another message into this message if possible.
      protected PreGameController pgc​(FreeColClient freeColClient)  
      protected PreGameController pgc​(FreeColServer freeColServer)  
      protected static void pretty​(java.lang.StringBuilder sb, java.lang.String type, java.util.Map<java.lang.String,​java.lang.String> attributeMap, java.util.List<FreeColObject> children)
      Pretty print the components of a message to a string builder.
      static Message read​(Game game, FreeColXMLReader xr)
      Read a new message from a stream.
      abstract ChangeSet serverHandler​(FreeColServer freeColServer, ServerPlayer serverPlayer)
      Server-side handler for this message.
      protected void setArrayAttributes​(java.lang.String[] attributes)
      Set an array of attributes.
      protected void setArrayAttributes​(java.util.List<java.lang.String> attributes)
      Set a list of attributes as an array.
      protected void setBooleanAttribute​(java.lang.String key, java.lang.Boolean value)
      Sets an attribute in this message with a boolean value.
      protected abstract void setChildren​(java.util.List<? extends FreeColObject> fcos)
      Set the list of objects attached to this message.
      protected void setEnumAttribute​(java.lang.String key, java.lang.Enum<?> value)
      Sets an attribute in this message with an enum value.
      protected void setIntegerAttribute​(java.lang.String key, int value)
      Sets an attribute in this message with an integer value.
      protected abstract void setStringAttribute​(java.lang.String key, java.lang.String value)
      Sets an attribute in this message.
      protected void setStringAttributeMap​(java.util.Map<java.lang.String,​java.lang.String> attributeMap)
      Set all the attributes in a map.
      protected void setStringAttributes​(java.lang.String[] attributes)
      Set all the attributes from an array.
      protected void setStringAttributes​(java.util.List<java.lang.String> attributes)
      Set all the attributes from a list.
      protected abstract void setType​(java.lang.String type)
      Set the message tag.
      java.lang.String toString()
      void toXML​(FreeColXMLWriter xw)
      Write this message as XML.
      protected void writeAttributes​(FreeColXMLWriter xw)
      Write any attributes of this message.
      protected void writeChildren​(FreeColXMLWriter xw)
      Write any children of this message.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • logger

        protected static final java.util.logging.Logger logger
      • builders

        private static final java.util.Map<java.lang.String,​java.lang.reflect.Constructor<? extends Message>> builders
        A map of message name to message constructors, built on the fly as new messages are encountered and suitable constructors found.
      • readClasses

        private static final java.lang.Class[] readClasses
        Classes used by Message.read()
      • messagePriorityComparator

        public static final java.util.Comparator<Message> messagePriorityComparator
        Comparator comparing by message priority.
    • Constructor Detail

      • Message

        protected Message()
        Deliberately trivial constructor.
    • Method Detail

      • getType

        public abstract java.lang.String getType()
        Get the message tag.
        Returns:
        The message tag.
      • setType

        protected abstract void setType​(java.lang.String type)
        Set the message tag.
        Parameters:
        type - The new message tag.
      • hasAttribute

        protected abstract boolean hasAttribute​(java.lang.String key)
        Checks if an attribute is present in this message.
        Parameters:
        key - The attribute to look for.
        Returns:
        True if the attribute is present.
      • getStringAttribute

        protected abstract java.lang.String getStringAttribute​(java.lang.String key)
        Get a string attribute value.
        Parameters:
        key - The attribute to look for.
        Returns:
        The string value found, or null if the attribute was absent.
      • setStringAttribute

        protected abstract void setStringAttribute​(java.lang.String key,
                                                   java.lang.String value)
        Sets an attribute in this message.
        Parameters:
        key - The attribute to set.
        value - The new value of the attribute.
      • getStringAttributeMap

        protected abstract java.util.Map<java.lang.String,​java.lang.String> getStringAttributeMap()
        Get a map of all the attributes in this message.
        Returns:
        A Map of the attributes.
      • getChildCount

        protected abstract int getChildCount()
        Get the number of child objects.
        Returns:
        The child count.
      • getChildren

        protected abstract java.util.List<FreeColObject> getChildren()
        Get the child objects of this message.
        Returns:
        A list of child FreeColObjects.
      • setChildren

        protected abstract void setChildren​(java.util.List<? extends FreeColObject> fcos)
        Set the list of objects attached to this message.
        Parameters:
        fcos - The new list of attached FreeColObjects.
      • appendChild

        protected abstract <T extends FreeColObject> void appendChild​(T fco)
        Append a new child.
        Type Parameters:
        T - The child type.
        Parameters:
        fco - The new child object.
      • appendChildren

        protected abstract <T extends FreeColObject> void appendChildren​(java.util.Collection<T> fcos)
        Append a multiple new children.
        Type Parameters:
        T - The child type.
        Parameters:
        fcos - The new child objects.
      • currentPlayerMessage

        public abstract boolean currentPlayerMessage()
        Should this message only be sent to a server by the current player?
        Returns:
        True if this is a current-player-only message.
      • getPriority

        public abstract Message.MessagePriority getPriority()
        Get the priority of this type of message.
        Returns:
        The message priority.
      • canMerge

        public boolean canMerge()
        Does this message consist only of mergeable attributes?
        Returns:
        True if this message is trivially mergeable.
      • aiHandler

        public abstract void aiHandler​(FreeColServer freeColServer,
                                       AIPlayer aiPlayer)
                                throws FreeColException
        AI-side handler for this message. AI handlers always return null. FIXME: One day the FreeColServer should devolve to AIMain.
        Parameters:
        freeColServer - The FreeColServer handling the request.
        aiPlayer - The AIPlayer the message was sent to.
        Throws:
        FreeColException - if there is a problem handling the message.
      • clientHandler

        public abstract void clientHandler​(FreeColClient freeColClient)
                                    throws FreeColException
        Client-side handler for this message. Client handlers always return null.
        Parameters:
        freeColClient - The FreeColClient to handle this message.
        Throws:
        FreeColException - if there is a problem building the message.
      • serverHandler

        public abstract ChangeSet serverHandler​(FreeColServer freeColServer,
                                                ServerPlayer serverPlayer)
        Server-side handler for this message.
        Parameters:
        freeColServer - The FreeColServer handling the request.
        serverPlayer - The ServerPlayer that sent the request.
        Returns:
        A ChangeSet defining the response.
      • isType

        public boolean isType​(java.lang.String type)
        Checks if this message is of a given type.
        Parameters:
        type - The type you wish to test against.
        Returns:
        True if the type of this message equals the given type.
      • getBooleanAttribute

        protected java.lang.Boolean getBooleanAttribute​(java.lang.String key,
                                                        java.lang.Boolean defaultValue)
        Get a boolean attribute value.
        Parameters:
        key - The attribute to look for.
        defaultValue - The fallback result.
        Returns:
        The boolean value, or the default value if no boolean is found.
      • getIntegerAttribute

        protected java.lang.Integer getIntegerAttribute​(java.lang.String key,
                                                        int defaultValue)
        Get an integer attribute value.
        Parameters:
        key - The attribute to look for.
        defaultValue - The fallback result.
        Returns:
        The integer value, or the default value if no integer is found.
      • getEnumAttribute

        protected <T extends java.lang.Enum<T>> T getEnumAttribute​(java.lang.String key,
                                                                   java.lang.Class<T> returnClass,
                                                                   T defaultValue)
        Gets an enum attribute value.
        Type Parameters:
        T - The expected enum type.
        Parameters:
        key - The attribute name.
        returnClass - The class of the return value.
        defaultValue - The default value.
        Returns:
        The enum attribute value, or the default value if none found.
      • setBooleanAttribute

        protected void setBooleanAttribute​(java.lang.String key,
                                           java.lang.Boolean value)
        Sets an attribute in this message with a boolean value.
        Parameters:
        key - The attribute to set.
        value - The value of the attribute.
      • setEnumAttribute

        protected void setEnumAttribute​(java.lang.String key,
                                        java.lang.Enum<?> value)
        Sets an attribute in this message with an enum value.
        Parameters:
        key - The attribute to set.
        value - The value of the attribute.
      • setIntegerAttribute

        protected void setIntegerAttribute​(java.lang.String key,
                                           int value)
        Sets an attribute in this message with an integer value.
        Parameters:
        key - The attribute to set.
        value - The value of the attribute.
      • setStringAttributeMap

        protected void setStringAttributeMap​(java.util.Map<java.lang.String,​java.lang.String> attributeMap)
        Set all the attributes in a map.
        Parameters:
        attributeMap - The map of key,value pairs to set.
      • setStringAttributes

        protected void setStringAttributes​(java.util.List<java.lang.String> attributes)
        Set all the attributes from a list.
        Parameters:
        attributes - A list of alternating key,value pairs.
      • setStringAttributes

        protected void setStringAttributes​(java.lang.String[] attributes)
        Set all the attributes from an array.
        Parameters:
        attributes - An array of alternating key,value pairs.
      • getArrayAttributes

        protected java.util.List<java.lang.String> getArrayAttributes()
        Get the array attributes of this message.
        Returns:
        A list of the array attributes found.
      • setArrayAttributes

        protected void setArrayAttributes​(java.util.List<java.lang.String> attributes)
        Set a list of attributes as an array.
        Parameters:
        attributes - A list of attribute values.
      • setArrayAttributes

        protected void setArrayAttributes​(java.lang.String[] attributes)
        Set an array of attributes.
        Parameters:
        attributes - The array of attributes.
      • getChild

        protected <T extends FreeColObject> T getChild​(int index,
                                                       java.lang.Class<T> returnClass)
        Get a child object.
        Type Parameters:
        T - The actual class of FreeColObject to get.
        Parameters:
        index - The index of the child to get.
        returnClass - The expected class of child.
        Returns:
        The child object found, or null if the index is invalid or return class incorrect.
      • getChildren

        protected <T extends FreeColObject> java.util.List<T> getChildren​(java.lang.Class<T> returnClass)
        Get the child objects.
        Type Parameters:
        T - The actual class of FreeColObject to get.
        Parameters:
        returnClass - The expected class of children.
        Returns:
        The children with the expected class.
      • isEmpty

        protected boolean isEmpty()
        Is this message vacuous?
        Returns:
        True if there are no attributes or children present.
      • getPriorityLevel

        public final int getPriorityLevel()
        Get the priority level of this type of message.
        Returns:
        The message priority level.
      • merge

        public boolean merge​(Message message)
        Merge another message into this message if possible.
        Parameters:
        message - The Message to merge.
        Returns:
        True if the other message was merged.
      • writeAttributes

        protected void writeAttributes​(FreeColXMLWriter xw)
                                throws javax.xml.stream.XMLStreamException
        Write any attributes of this message.
        Parameters:
        xw - The FreeColXMLWriter to write to.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem writing the stream.
      • writeChildren

        protected void writeChildren​(FreeColXMLWriter xw)
                              throws javax.xml.stream.XMLStreamException
        Write any children of this message.
        Parameters:
        xw - The FreeColXMLWriter to write to.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem writing the stream.
      • toXML

        public void toXML​(FreeColXMLWriter xw)
                   throws javax.xml.stream.XMLStreamException
        Write this message as XML.
        Parameters:
        xw - The FreeColXMLWriter to write with.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem writing the stream.
      • invokeAndWait

        protected void invokeAndWait​(FreeColClient freeColClient,
                                     java.lang.Runnable runnable)
      • invokeLater

        protected void invokeLater​(FreeColClient freeColClient,
                                   java.lang.Runnable runnable)
      • clientGeneric

        protected void clientGeneric​(FreeColClient freeColClient)
        Create a runnable to execute generic message actions.
        Parameters:
        freeColClient - The client.
      • expected

        protected void expected​(java.lang.String wanted,
                                java.lang.String got)
                         throws javax.xml.stream.XMLStreamException
        Complain about finding the wrong XML element.
        Parameters:
        wanted - The tag we wanted.
        got - The tag we got.
        Throws:
        javax.xml.stream.XMLStreamException - is always thrown.
      • read

        public static Message read​(Game game,
                                   FreeColXMLReader xr)
                            throws FreeColException
        Read a new message from a stream.
        Parameters:
        game - The Game within which to construct the message.
        xr - A FreeColXMLReader to read from.
        Returns:
        The new Message.
        Throws:
        FreeColException - if there is problem reading the message.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • pretty

        protected static void pretty​(java.lang.StringBuilder sb,
                                     java.lang.String type,
                                     java.util.Map<java.lang.String,​java.lang.String> attributeMap,
                                     java.util.List<FreeColObject> children)
        Pretty print the components of a message to a string builder.
        Parameters:
        sb - The StringBuilder to print to.
        type - The type of the message.
        attributeMap - A map of key,value attribute pairs.
        children - The attached child FreeColObjects.