209 lines
5.8 KiB
Java
209 lines
5.8 KiB
Java
package envoy.data;
|
|
|
|
import java.io.Serializable;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public class Message implements Serializable {
|
|
|
|
/**
|
|
* This enumeration defines all possible statuses a {link Message} can have.
|
|
*
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public 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 final long id, senderID, recipientID;
|
|
private final boolean forwarded;
|
|
private final LocalDateTime creationDate;
|
|
private final String text;
|
|
private final Attachment attachment;
|
|
|
|
private LocalDateTime receivedDate, readDate;
|
|
private MessageStatus status;
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
/**
|
|
* 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 senderID the ID of the user who sends the message
|
|
* @param recipientID the ID of the user who receives the message
|
|
* @param creationDate the creation date of the message
|
|
* @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
|
|
* @param forwarded whether this message was forwarded
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
Message(long id, long senderID, long recipientID, LocalDateTime creationDate, String text, Attachment attachment, MessageStatus status,
|
|
boolean forwarded) {
|
|
this.id = id;
|
|
this.senderID = senderID;
|
|
this.recipientID = recipientID;
|
|
this.creationDate = creationDate;
|
|
this.text = text;
|
|
this.attachment = attachment;
|
|
this.status = status;
|
|
this.forwarded = forwarded;
|
|
}
|
|
|
|
/**
|
|
* 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("Message[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,forwarded=%b,hasAttachment=%b]",
|
|
id,
|
|
senderID,
|
|
recipientID,
|
|
DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss").format(creationDate),
|
|
status,
|
|
text,
|
|
forwarded,
|
|
attachment != null);
|
|
}
|
|
|
|
/**
|
|
* @return the ID of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public long getID() { return id; }
|
|
|
|
/**
|
|
* @return the sender ID of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public long getSenderID() { return senderID; }
|
|
|
|
/**
|
|
* @return the recipient ID of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public long getRecipientID() { return recipientID; }
|
|
|
|
/**
|
|
* @return the date at which this message was created
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public LocalDateTime getCreationDate() { return creationDate; }
|
|
|
|
/**
|
|
* @return the date at which the message has been received by the sender
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public LocalDateTime getReceivedDate() { return receivedDate; }
|
|
|
|
/**
|
|
* @param receivedDate the date at which the message has been received by the
|
|
* sender
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public void setReceivedDate(LocalDateTime receivedDate) { this.receivedDate = receivedDate; }
|
|
|
|
/**
|
|
* @return the date at which the message has been read by the sender
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public LocalDateTime getReadDate() { return readDate; }
|
|
|
|
/**
|
|
* @param readDate at which the message has been read by the sender
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public void setReadDate(LocalDateTime readDate) { this.readDate = readDate; }
|
|
|
|
/**
|
|
* @return the text content of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public String getText() { return text; }
|
|
|
|
/**
|
|
* @return the messageAttachment
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public Attachment getAttachment() { return attachment; }
|
|
|
|
/**
|
|
* @return {@code true} if an attachment is present
|
|
* @since Envoy Common v0.1-beta
|
|
*/
|
|
public boolean hasAttachment() { return attachment != null; }
|
|
|
|
/**
|
|
* @return the current status of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public MessageStatus getStatus() { return status; }
|
|
|
|
/**
|
|
* @param status the new {@link MessageStatus}, if permitted
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public void setStatus(MessageStatus status) {
|
|
if (status.ordinal() < this.status.ordinal()) throw new IllegalStateException("This message is moving backwards in time");
|
|
this.status = status;
|
|
}
|
|
|
|
/**
|
|
* @return whether this message was forwarded
|
|
* @since Envoy common v0.1-beta
|
|
*/
|
|
public boolean isForwarded() { return forwarded; }
|
|
}
|