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

97 lines
2.5 KiB
Java
Raw Normal View History

package envoy.data;
import java.io.Serializable;
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
* @since Envoy Common v0.2-alpha
*/
public abstract class Message implements Serializable {
private final long id;
private final User sender, recipient;
private final Date date;
private MessageStatus status;
private static final long serialVersionUID = -4393477412979594435L;
/**
* 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 id unique ID
* @param sender the user who sends the message
* @param recipient the user who receives the message
* @since Envoy Common v0.2-alpha
*/
public Message(long id, User sender, User recipient) {
this.id = id;
this.sender = sender;
this.recipient = recipient;
date = new Date();
status = MessageStatus.WAITING;
}
/**
* @return the ID of this message
* @since Envoy Common v0.2-alpha
*/
public long getId() { return id; }
/**
* @return the sender of this message
* @since Envoy Common v0.2-alpha
*/
public User getSender() { return sender; }
/**
* @return the recipient of this message
* @since Envoy Common v0.2-alpha
*/
public User getRecipient() { return recipient; }
/**
* @return the date at which this message was created
* @since Envoy Common v0.2-alpha
*/
public Date getDate() { return date; }
/**
* @return the current status of this message
* @since Envoy Common v0.2-alpha
*/
public MessageStatus getStatus() { return status; }
/**
* 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];
}
public static enum MessageStatus {
WAITING, SENT, RECEIVED, READ
}
}