2019-12-28 21:03:41 +01:00
|
|
|
package envoy.data;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
2020-07-16 17:04:35 +02:00
|
|
|
import java.time.Instant;
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2020-09-16 15:52:58 +02:00
|
|
|
import dev.kske.eventbus.IEvent;
|
|
|
|
|
2019-12-28 21:03:41 +01:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* 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>
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-28 21:03:41 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
2019-12-30 18:49:48 +01:00
|
|
|
* @author Leon Hofmeister
|
2019-12-28 21:03:41 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-09-16 15:52:58 +02:00
|
|
|
public class Message implements Serializable, IEvent {
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
2019-12-31 10:16:52 +01:00
|
|
|
* This enumeration defines all possible statuses a {link Message} can have.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-28 17:01:02 +01:00
|
|
|
public enum MessageStatus {
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
2020-09-25 14:29:23 +02:00
|
|
|
* The message has not yet been sent to the server
|
2019-12-30 18:49:48 +01:00
|
|
|
*/
|
|
|
|
WAITING,
|
|
|
|
|
|
|
|
/**
|
2020-09-25 14:29:23 +02:00
|
|
|
* The message has been sent to the server.
|
2019-12-30 18:49:48 +01:00
|
|
|
*/
|
|
|
|
SENT,
|
|
|
|
|
|
|
|
/**
|
2020-09-25 14:29:23 +02:00
|
|
|
* The message has been received by its recipient.
|
2019-12-30 18:49:48 +01:00
|
|
|
*/
|
|
|
|
RECEIVED,
|
|
|
|
|
|
|
|
/**
|
2020-09-25 14:29:23 +02:00
|
|
|
* The message has been read by its recipient.
|
2019-12-30 18:49:48 +01:00
|
|
|
*/
|
|
|
|
READ
|
|
|
|
}
|
|
|
|
|
2020-07-02 10:58:02 +02:00
|
|
|
private final long id, senderID, recipientID;
|
|
|
|
private final boolean forwarded;
|
2020-07-16 17:04:35 +02:00
|
|
|
private final Instant creationDate;
|
2020-07-02 10:58:02 +02:00
|
|
|
private final String text;
|
|
|
|
private final Attachment attachment;
|
2019-12-30 18:49:48 +01:00
|
|
|
|
2020-07-16 17:04:35 +02:00
|
|
|
private Instant receivedDate, readDate;
|
2020-01-02 17:50:04 +01:00
|
|
|
private MessageStatus status;
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2020-07-16 17:04:35 +02:00
|
|
|
private static final long serialVersionUID = 2L;
|
2019-12-31 10:16:52 +01:00
|
|
|
|
2019-12-28 21:03:41 +01:00
|
|
|
/**
|
2020-10-19 18:17:51 +02: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.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2020-01-28 17:32:33 +01:00
|
|
|
* @param id unique ID
|
2020-03-25 17:43:55 +01:00
|
|
|
* @param senderID the ID of the user who sends the message
|
|
|
|
* @param recipientID the ID of the user who receives the message
|
2020-01-28 17:32:33 +01:00
|
|
|
* @param creationDate the creation date of the message
|
2020-07-02 14:47:04 +02:00
|
|
|
* @param receivedDate the received date of the message
|
|
|
|
* @param readDate the read date of the message
|
2020-01-28 17:32:33 +01:00
|
|
|
* @param text the text content of the message
|
|
|
|
* @param attachment the attachment of the message, if present
|
|
|
|
* @param status the current {@link MessageStatus} of the message
|
2020-03-22 14:52:38 +01:00
|
|
|
* @param forwarded whether this message was forwarded
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Common v0.2-beta
|
2020-01-02 17:50:04 +01:00
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
Message(long id, long senderID, long recipientID, Instant creationDate, Instant receivedDate,
|
|
|
|
Instant readDate, String text,
|
|
|
|
Attachment attachment, MessageStatus status, boolean forwarded) {
|
2020-01-02 17:50:04 +01:00
|
|
|
this.id = id;
|
2020-03-25 17:43:55 +01:00
|
|
|
this.senderID = senderID;
|
|
|
|
this.recipientID = recipientID;
|
2020-01-28 17:32:33 +01:00
|
|
|
this.creationDate = creationDate;
|
2020-07-02 14:47:04 +02:00
|
|
|
this.receivedDate = receivedDate;
|
|
|
|
this.readDate = readDate;
|
2020-01-02 17:50:04 +01:00
|
|
|
this.text = text;
|
|
|
|
this.attachment = attachment;
|
|
|
|
this.status = status;
|
2020-03-22 14:52:38 +01:00
|
|
|
this.forwarded = forwarded;
|
2019-12-28 21:03:41 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2020-10-19 18:17:51 +02:00
|
|
|
if (status == MessageStatus.READ)
|
|
|
|
throw new IllegalStateException("Message status READ is already reached");
|
2019-12-31 10:20:35 +01:00
|
|
|
status = MessageStatus.values()[status.ordinal() + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2020-10-19 18:17:51 +02:00
|
|
|
return String.format(
|
|
|
|
"Message[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,forwarded=%b,hasAttachment=%b]",
|
|
|
|
id,
|
|
|
|
senderID,
|
|
|
|
recipientID,
|
|
|
|
creationDate,
|
|
|
|
status,
|
|
|
|
text,
|
|
|
|
forwarded,
|
|
|
|
attachment != null);
|
2019-12-31 10:20:35 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 21:03:41 +01:00
|
|
|
/**
|
|
|
|
* @return the ID of this message
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-03-25 17:43:55 +01:00
|
|
|
public long getID() { return id; }
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-31 15:38:07 +01:00
|
|
|
/**
|
|
|
|
* @return the sender ID of this message
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-03-25 17:43:55 +01:00
|
|
|
public long getSenderID() { return senderID; }
|
2019-12-31 15:38:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the recipient ID of this message
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-03-25 17:43:55 +01:00
|
|
|
public long getRecipientID() { return recipientID; }
|
2019-12-31 15:38:07 +01:00
|
|
|
|
2019-12-28 21:03:41 +01:00
|
|
|
/**
|
2019-12-31 10:16:52 +01:00
|
|
|
* @return the date at which this message was created
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Common v0.2-beta
|
2019-12-28 21:03:41 +01:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public Instant getCreationDate() { return creationDate; }
|
2019-12-28 21:03:41 +01:00
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the date at which the message has been received by the sender
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Common v0.2-beta
|
2019-12-30 18:49:48 +01:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public Instant getReceivedDate() { return receivedDate; }
|
2019-12-31 10:16:52 +01:00
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* @param receivedDate the date at which the message has been received by the sender
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Common v0.2-beta
|
2019-12-31 10:16:52 +01:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public void setReceivedDate(Instant receivedDate) { this.receivedDate = receivedDate; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the date at which the message has been read by the sender
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Common v0.2-beta
|
2020-01-01 19:16:40 +01:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public Instant getReadDate() { return readDate; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @param readDate at which the message has been read by the sender
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Common v0.2-beta
|
2020-01-01 19:16:40 +01:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public void setReadDate(Instant readDate) { this.readDate = readDate; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the text content of this message
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public String getText() { return text; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the messageAttachment
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-07-02 10:58:02 +02:00
|
|
|
public Attachment getAttachment() { return attachment; }
|
2020-01-01 19:16:40 +01:00
|
|
|
|
2020-07-02 14:34:32 +02:00
|
|
|
/**
|
|
|
|
* @return {@code true} if an attachment is present
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public boolean hasAttachment() {
|
|
|
|
return attachment != null;
|
|
|
|
}
|
2020-07-02 14:34:32 +02:00
|
|
|
|
2020-01-01 19:16:40 +01:00
|
|
|
/**
|
2020-01-02 17:50:04 +01:00
|
|
|
* @return the current status of this message
|
2020-01-01 19:16:40 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-02 17:50:04 +01:00
|
|
|
public MessageStatus getStatus() { return status; }
|
2020-01-11 14:17:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param status the new {@link MessageStatus}, if permitted
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public void setStatus(MessageStatus status) {
|
2020-10-19 18:17:51 +02:00
|
|
|
if (status.ordinal() < this.status.ordinal())
|
|
|
|
throw new IllegalStateException("This message is moving backwards in time");
|
2020-01-28 17:01:02 +01:00
|
|
|
this.status = status;
|
2020-01-11 14:17:51 +01:00
|
|
|
}
|
2020-03-22 14:52:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return whether this message was forwarded
|
|
|
|
* @since Envoy common v0.1-beta
|
|
|
|
*/
|
|
|
|
public boolean isForwarded() { return forwarded; }
|
|
|
|
}
|