pictionary.message
Class Message

java.lang.Object
  extended by pictionary.message.Message
All Implemented Interfaces:
java.io.Serializable

public final class Message
extends java.lang.Object
implements java.io.Serializable

This class defines the package of data that is sent between the server and clients.
Messages can contain data or not. If they do contain data, for example a chat message from a client, it is stored in messageData.

Author:
Kristoffer Nordkvist
See Also:
Serialized Form

Nested Class Summary
static class Message.MessageType
          Used in Message to determine the type of message.
 
Field Summary
 java.lang.String client
          The client.
private static java.util.EnumSet<Message.MessageType> drawDataMessages
          Contains all the message types that can contain draw data message.
private static java.util.EnumSet<Message.MessageType> gameStatusMessages
          Contains all the message types that can contain game status data.
private static java.util.Hashtable<java.lang.Class<?>,java.util.EnumSet<Message.MessageType>> legalCombinations
          HashTable which is used to check if a combination of data type and message type is legal.
 MessageData<?> messageData
          The data associated with this message, is null with some message types where data is not needed; for example Message.MessageType.StopGuessing where the message type alone conveys enough information.
 Message.MessageType messageType
          The type of message.
private static java.util.EnumSet<Message.MessageType> startDrawDataMessages
          Contains all the message types that can contain start draw data.
private static java.util.EnumSet<Message.MessageType> statusMessages
          Contains all the message types that can contain status message data.
private static java.util.EnumSet<Message.MessageType> stringDataMessages
          Contains all the message types that can contain string message data.
 
Constructor Summary
  Message(Message.MessageType messageType)
          Constructor for the simplest message.
  Message(Message.MessageType messageType, java.lang.String client)
          Constructor for a basic message without message data.
private Message(Message.MessageType messageType, java.lang.String client, MessageData<?> messageData)
          Creates a Message with the specified type, client and data.
 
Method Summary
static
<T> Message
CreateDataMessage(Message.MessageType messageType, java.lang.String client, T data)
          Creates and returns a Message object containing data.
static
<T> Message
CreateDataMessage(Message.MessageType messageType, T data)
          Convenience method.
private static boolean legalCombination(Message.MessageType messageType, java.lang.Class<?> klass)
          Determines whether a combination of message type and data is legal or not.
private static boolean shouldContainData(Message.MessageType messageType)
          Determines whether a message of the specified messageType should contain data or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

legalCombinations

private static final java.util.Hashtable<java.lang.Class<?>,java.util.EnumSet<Message.MessageType>> legalCombinations
HashTable which is used to check if a combination of data type and message type is legal.
For example: getting the values for the key String.class could return an EnumSet containing the legal types ServerMessage and ClientMessageBroadcast.
This table is used by legalCombination(MessageType, Class).


stringDataMessages

private static final java.util.EnumSet<Message.MessageType> stringDataMessages
Contains all the message types that can contain string message data.


startDrawDataMessages

private static final java.util.EnumSet<Message.MessageType> startDrawDataMessages
Contains all the message types that can contain start draw data.


statusMessages

private static final java.util.EnumSet<Message.MessageType> statusMessages
Contains all the message types that can contain status message data.


gameStatusMessages

private static final java.util.EnumSet<Message.MessageType> gameStatusMessages
Contains all the message types that can contain game status data.


drawDataMessages

private static final java.util.EnumSet<Message.MessageType> drawDataMessages
Contains all the message types that can contain draw data message.


messageType

public final Message.MessageType messageType
The type of message.


client

public final java.lang.String client
The client.
This variable can have different meanings depending on the type of message, for example the recipient ID or the sender ID.


messageData

public final MessageData<?> messageData
The data associated with this message, is null with some message types where data is not needed; for example Message.MessageType.StopGuessing where the message type alone conveys enough information.
See MessageData for details.

Constructor Detail

Message

public Message(Message.MessageType messageType)
Constructor for the simplest message.
messageType is set, but both client and messageData is null.
Performs checking to ensure that this a message of this type should not contain data.

Parameters:
messageType - The message's Message.MessageType.

Message

public Message(Message.MessageType messageType,
               java.lang.String client)
        throws java.lang.IllegalArgumentException
Constructor for a basic message without message data.
Both messageType and client is set, but messageData is null.

Parameters:
messageType - The Message.MessageType.
client - The client.
Throws:
java.lang.IllegalArgumentException - If messages of this message type should contain data, uses shouldContainData(MessageType) to perform this check.

Message

private Message(Message.MessageType messageType,
                java.lang.String client,
                MessageData<?> messageData)
         throws java.lang.IllegalArgumentException
Creates a Message with the specified type, client and data.

Parameters:
messageType - The message type.
client - The client, see client.
messageData - The data contained in this message, see messageData
Throws:
java.lang.IllegalArgumentException - If the combination of message type and message data is illegal, uses legalCombination(MessageType, Class) to perform this check.
Method Detail

CreateDataMessage

public static <T> Message CreateDataMessage(Message.MessageType messageType,
                                            java.lang.String client,
                                            T data)
Creates and returns a Message object containing data.
This factory method has built in checks to assure that the message type and the type of data is consistent.
This means that, for example, no one can create a Message with the MessageType of MessageType#ClientDrawData and a data object that is anything else but Point.
Wraps Message(MessageType, String, MessageData).

Type Parameters:
T - The type of data.
Parameters:
messageType - he type of message.
client - The client, see client.
data - This message's message data, see messageData.
Returns:
An instance of the class Message, or null if an IllegalArgumentException is caught.

CreateDataMessage

public static <T> Message CreateDataMessage(Message.MessageType messageType,
                                            T data)
Convenience method. Passes the arguments along to CreateDataMessage(MessageType, String, Object), using null for the String object.

Parameters:
messageType - See CreateDataMessage(MessageType, String, Object)
data - See CreateDataMessage(MessageType, String, Object).
See Also:
CreateDataMessage(MessageType, String, Object)

legalCombination

private static boolean legalCombination(Message.MessageType messageType,
                                        java.lang.Class<?> klass)
Determines whether a combination of message type and data is legal or not.

Parameters:
messageType - The Message.MessageType.
klass - The Class.
Returns:
True if legalCombinations containsan EnumSet which in turn contains the specified message type, else false.

shouldContainData

private static boolean shouldContainData(Message.MessageType messageType)
Determines whether a message of the specified messageType should contain data or not.
Iterates through the EnumSets contained in legalCombinations, looking for the specified message type.

Parameters:
messageType - The messageType.
Returns:
True if the messageType was found in any of the EnumSets, else false.