This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/data/Message.java

241 lines
6.2 KiB
Java
Raw Normal View History

package envoy.data;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Represents a unique message with a unique, numeric ID. Further metadata
* includes the sender and recipient {@link User}s, as well as the creation
* date and the current {@link MessageStatus}.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>Message.java</strong><br>
* Created: <strong>28.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @author Leon Hofmeister
* @since Envoy Common v0.2-alpha
*/
2019-12-31 10:16:52 +01:00
public class Message implements Serializable {
/**
2019-12-31 10:16:52 +01:00
* This enumeration defines all possible statuses a {link Message} can have.
*
* @since Envoy Common v0.2-alpha
*/
public static enum MessageStatus {
/**
* is selected, if a message was sent but not received by the server yet.
*/
WAITING,
/**
* is selected, if a sent message was received by the server.
*/
SENT,
/**
* is selected, if a message was delivered from the server to the recipient, but
* has not been read yet.
*/
RECEIVED,
/**
* is selected, if a recipient opened the corresponding chat of said message.
*/
READ
}
private long id, senderId, recipientId;
private transient User sender, recipient;
private Date date;
private String text;
private MessageAttachment<?> attachment;
private MessageStatus status;
2019-12-31 10:16:52 +01:00
private static final long serialVersionUID = -4393477412979594435L;
/**
2019-12-31 10:16:52 +01:00
* Initializes a {@link Message} with values for all of its properties. The use
* of this constructor is only intended for the {@link MessageBuilder} class, as
* this class provides {@code null} checks and default values for all
* properties.
*
* @param id unique ID
* @param sender the user who sends the message
* @param recipient the user who receives the message
2019-12-31 10:16:52 +01:00
* @param date the creation date of the message
* @param text the text content of the message
* @param attachment the attachment of the message, if present
2019-12-31 10:16:52 +01:00
* @param status the current {@link MessageStatus} of the message
* @since Envoy Common v0.2-alpha
*/
2019-12-31 10:16:52 +01:00
Message(long id, User sender, User recipient, Date date, String text, MessageAttachment<?> attachment, MessageStatus status) {
this.id = id;
this.sender = sender;
this.recipient = recipient;
this.date = date;
this.text = text;
this.attachment = attachment;
this.status = status;
senderId = sender.getId();
recipientId = recipient.getId();
}
2019-12-31 10:20:35 +01:00
/**
* Changes the current {@link MessageStatus} to the next logical status.<br>
* <br>
* The underlying order is as follows:
* <ol>
* <li>{@link MessageStatus#WAITING}
* <li>{@link MessageStatus#SENT}
* <li>{@link MessageStatus#RECEIVED}
* <li>{@link MessageStatus#READ}
* </ol>
*
* @since Envoy Common v0.2-alpha
*/
public void nextStatus() {
if (status == MessageStatus.READ) throw new IllegalStateException("Message status READ is already reached");
status = MessageStatus.values()[status.ordinal() + 1];
}
@Override
public String toString() {
return String.format("TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s]",
id,
sender,
recipient,
new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date),
status,
text);
}
/**
* @return the ID of this message
* @since Envoy Common v0.2-alpha
*/
public long getId() { return id; }
/**
2019-12-31 10:16:52 +01:00
* @return the sender of this message
* @since Envoy Common v0.2-alpha
*/
2019-12-31 10:16:52 +01:00
public User getSender() { return sender; }
/**
* @return the sender ID of this message
* @since Envoy Common v0.2-alpha
*/
public long getSenderId() { return senderId; }
/**
* @return the recipient of this message
* @since Envoy Common v0.2-alpha
*/
public User getRecipient() { return recipient; }
/**
* @return the recipient ID of this message
* @since Envoy Common v0.2-alpha
*/
public long getRecipientId() { return recipientId; }
/**
2019-12-31 10:16:52 +01:00
* @return the date at which this message was created
* @since Envoy Common v0.2-alpha
*/
2019-12-31 10:16:52 +01:00
public Date getDate() { return date; }
/**
2019-12-31 10:16:52 +01:00
* @return the text content of this message
* @since Envoy Common v0.2-alpha
*/
2019-12-31 10:16:52 +01:00
public String getText() { return text; }
/**
2019-12-31 10:16:52 +01:00
* @return the messageAttachment
* @since Envoy Common v0.2-alpha
*/
2019-12-31 10:16:52 +01:00
public MessageAttachment<?> getAttachment() { return attachment; }
/**
* @return the current status of this message
* @since Envoy Common v0.2-alpha
*/
public MessageStatus getStatus() { return status; }
/**
* Only used for Hibernate. Please do not use.
*
* @param id the id to set
* @since Envoy Common v0.2-alpha
*/
public void setId(long id) { this.id = id; }
/**
* Only used for Hibernate. Please do not use.
*
* @param senderId the senderId to set
* @since Envoy Common v0.2-alpha
*/
public void setSenderId(long senderId) { this.senderId = senderId; }
/**
* Only used for Hibernate. Please do not use.
*
* @param recipientId the recipientId to set
* @since Envoy Common v0.2-alpha
*/
public void setRecipientId(long recipientId) { this.recipientId = recipientId; }
/**
* Only used for Hibernate. Please do not use.
*
* @param sender the sender to set
* @since Envoy Common v0.2-alpha
*/
public void setSender(User sender) { this.sender = sender; }
/**
* Only used for Hibernate. Please do not use.
*
* @param recipient the recipient to set
* @since Envoy Common v0.2-alpha
*/
public void setRecipient(User recipient) { this.recipient = recipient; }
/**
* Only used for Hibernate. Please do not use.
*
* @param date the date to set
* @since Envoy Common v0.2-alpha
*/
public void setDate(Date date) { this.date = date; }
/**
* Only used for Hibernate. Please do not use.
*
* @param text the text to set
* @since Envoy Common v0.2-alpha
*/
public void setText(String text) { this.text = text; }
/**
* Only used for Hibernate. Please do not use.
*
* @param attachment the attachment to set
* @since Envoy Common v0.2-alpha
*/
public void setAttachment(MessageAttachment<?> attachment) { this.attachment = attachment; }
/**
* @param status the status to set
* @since Envoy Common v0.2-alpha
*/
public void setStatus(MessageStatus status) { this.status = status; }
}