159 lines
4.1 KiB
Java
159 lines
4.1 KiB
Java
package envoy.data;
|
|
|
|
import java.io.Serializable;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* 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 abstract class Message implements Serializable {
|
|
|
|
/**
|
|
* This enum 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 static final long serialVersionUID = -4393477412979594435L;
|
|
private final long id;
|
|
|
|
private final User sender, recipient;
|
|
|
|
private final Date date;
|
|
|
|
private final String text;
|
|
|
|
private final MessageAttachment<?> messageAttachment;
|
|
|
|
private MessageStatus status;
|
|
|
|
/**
|
|
* Initializes a {@link Message} from the client's perspective. The current date
|
|
* is used as the message date and the status is set to
|
|
* {@link MessageStatus#WAITING}.
|
|
*
|
|
* @param <T> the type of the attachment
|
|
* @param id unique ID
|
|
* @param sender the user who sends the message
|
|
* @param recipient the user who receives the message
|
|
* @param text the text content of the message
|
|
* @param attachment the attachment of the message, if present
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public <T> Message(long id, User sender, User recipient, String text, @SuppressWarnings("rawtypes") Optional<MessageAttachment> attachment) {
|
|
this.id = id;
|
|
this.sender = sender;
|
|
this.recipient = recipient;
|
|
this.text = text;
|
|
this.messageAttachment = attachment.isEmpty() ? null : attachment.get();
|
|
this.date = new Date();
|
|
this.status = MessageStatus.WAITING;
|
|
}
|
|
|
|
/**
|
|
* @return the date at which this message was created
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public Date getDate() { return date; }
|
|
|
|
/**
|
|
* @return the ID of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public long getId() { return id; }
|
|
|
|
/**
|
|
* @param <T> the type of the message attachment
|
|
* @return the messageAttachment
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public <T> MessageAttachment<?> getMessageAttachment() { return messageAttachment; }
|
|
|
|
/**
|
|
* @return the recipient of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public User getRecipient() { return recipient; }
|
|
|
|
/**
|
|
* @return the sender of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public User getSender() { return sender; }
|
|
|
|
/**
|
|
* @return the current status of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public MessageStatus getStatus() { return status; }
|
|
|
|
/**
|
|
* @return the text content of this message
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public String getText() { return text; }
|
|
|
|
/**
|
|
* 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,content=%s]",
|
|
id,
|
|
sender,
|
|
recipient,
|
|
new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date),
|
|
status,
|
|
text);
|
|
}
|
|
} |