Class MessageTransferObject

java.lang.Object
cz.foresttech.forestredis.shared.models.MessageTransferObject

public class MessageTransferObject extends Object
DTO object used for sending data across network. Supports custom object serialization using Gson.
  • Constructor Details

    • MessageTransferObject

      public MessageTransferObject()
      Empty constructor in case of using setters to add data later
    • MessageTransferObject

      public MessageTransferObject(String senderIdentifier, String message, long timestamp)
      Constructs the instance with provided parameters
      Parameters:
      senderIdentifier - Identifier of sending server
      message - Message content
      timestamp - TimeStamp of the message
  • Method Details

    • toJson

      public String toJson()
      Converts current data to JSON using Gson
      Returns:
      Object serialized to JSON String
    • fromJson

      public static MessageTransferObject fromJson(String json)
      Obtains MessageTransferObject from provided JSON String.
      Parameters:
      json - Serialized MessageTransferObject in JSON String
      Returns:
      Deserialized MessageTransferObject
    • wrap

      public static MessageTransferObject wrap(String senderIdentifier, Object objectToWrap, long timestamp)
      Wraps the given object to MessageTransferObject object.
      Parameters:
      senderIdentifier - Identifier of the sending server
      objectToWrap - Object which shall be wrapped
      timestamp - TimeStamp of the message
      Returns:
      Instance of MessageTransferObject containing serialized object from input
    • parseMessageObject

      public <T> T parseMessageObject(Class<T> objectType)
      Parses the message string to provided object type.
      Type Parameters:
      T - Type of the object
      Parameters:
      objectType - Class of the object
      Returns:
      Parsed object or null if object cannot be parsed
    • getTimestamp

      public long getTimestamp()
      Obtains the timestamp of the message
      Returns:
      Date when message has been sent
    • setTimestamp

      public void setTimestamp(Long timestamp)
      Changes the timestamp of the message
      Parameters:
      timestamp - New date when the message has been sent
    • getSenderIdentifier

      public String getSenderIdentifier()
      Obtains the server sender identifier
      Returns:
      Server identifier of the sender
    • getMessage

      public String getMessage()
      Obtains the content of the transferred message
      Returns:
      Content of the message
    • setSenderIdentifier

      public void setSenderIdentifier(String senderIdentifier)
      Changes the server sender identifier
      Parameters:
      senderIdentifier - New server sender identifier
    • setMessage

      public void setMessage(String message)
      Updates the content of the transferred message
      Parameters:
      message - New transferred message content