2019-12-31 10:16:52 +01:00
|
|
|
package envoy.data;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import envoy.data.Message.MessageStatus;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a method of constructing the {@link Message} class.<br>
|
|
|
|
* <br>
|
|
|
|
* Project: <strong>envoy-common</strong><br>
|
|
|
|
* File: <strong>MessageBuilder.java</strong><br>
|
|
|
|
* Created: <strong>31.12.2019</strong><br>
|
2020-01-28 17:32:33 +01:00
|
|
|
*
|
2019-12-31 10:16:52 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public class MessageBuilder {
|
|
|
|
|
|
|
|
// Mandatory properties without default values
|
2020-01-02 17:50:04 +01:00
|
|
|
private final long senderId, recipientId;
|
2019-12-31 10:16:52 +01:00
|
|
|
|
|
|
|
// Properties with default values
|
|
|
|
private long id;
|
2020-01-28 17:32:33 +01:00
|
|
|
private Date creationDate;
|
2019-12-31 10:16:52 +01:00
|
|
|
private String text;
|
|
|
|
private MessageAttachment<?> attachment;
|
|
|
|
private Message.MessageStatus status;
|
2020-03-22 14:52:38 +01:00
|
|
|
private boolean forwarded;
|
2019-12-31 10:16:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of {@link MessageBuilder} with all mandatory values
|
|
|
|
* without defaults for the {@link Message} class.
|
|
|
|
*
|
2020-01-02 17:50:04 +01:00
|
|
|
* @param senderId the ID of the user who sends the {@link Message}
|
2020-03-22 14:52:38 +01:00
|
|
|
* @param recipientId the ID of the user who receives the {@link Message}
|
2020-03-24 19:06:56 +01:00
|
|
|
* @param iDGenerator the ID generator used to generate a unique {@link Message}
|
2020-01-28 17:32:33 +01:00
|
|
|
* id
|
2019-12-31 10:16:52 +01:00
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-03-24 19:06:56 +01:00
|
|
|
public MessageBuilder(long senderId, long recipientId, IDGenerator iDGenerator) { this(senderId, recipientId, iDGenerator.next()); }
|
2020-01-28 17:32:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of {@link MessageBuilder} with all mandatory values
|
|
|
|
* without defaults for the {@link Message} class.
|
|
|
|
*
|
|
|
|
* @param senderId the ID of the user who sends the {@link Message}
|
2020-03-22 14:52:38 +01:00
|
|
|
* @param recipientId the ID of the user who receives the {@link Message}
|
2020-01-28 17:32:33 +01:00
|
|
|
* @param messageId the ID of the {@link Message}
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public MessageBuilder(long senderId, long recipientId, long messageId) {
|
2020-01-02 17:50:04 +01:00
|
|
|
this.senderId = senderId;
|
|
|
|
this.recipientId = recipientId;
|
2020-01-28 17:32:33 +01:00
|
|
|
id = messageId;
|
2019-12-31 10:16:52 +01:00
|
|
|
}
|
|
|
|
|
2020-03-22 14:52:38 +01:00
|
|
|
/**
|
|
|
|
* This constructor transforms a given {@link Message} into a new message for a
|
|
|
|
* new receiver.
|
|
|
|
* This makes it especially useful in the case of forwarding messages.
|
|
|
|
*
|
|
|
|
* @param msg the message to copy
|
|
|
|
* @param recipientId the ID of the user who receives the {@link Message}
|
2020-03-24 19:06:56 +01:00
|
|
|
* @param iDGenerator the ID generator used to generate a unique {@link Message}
|
2020-03-22 14:52:38 +01:00
|
|
|
* id
|
|
|
|
* @since Envoy v0.1-beta
|
|
|
|
*/
|
2020-03-24 19:06:56 +01:00
|
|
|
public MessageBuilder(Message msg, long recipientId, IDGenerator iDGenerator) {
|
|
|
|
this(msg.getRecipientId(), recipientId, iDGenerator.next());
|
2020-03-22 14:52:38 +01:00
|
|
|
this.attachment = msg.getAttachment();
|
|
|
|
this.creationDate = new Date();
|
|
|
|
this.forwarded = true;
|
|
|
|
this.text = msg.getText();
|
|
|
|
this.status = MessageStatus.WAITING;
|
|
|
|
}
|
|
|
|
|
2019-12-31 10:16:52 +01:00
|
|
|
/**
|
|
|
|
* Creates an instance of {@link Message} with the previously supplied values.
|
|
|
|
* If a mandatory value is not set, a default value will be used instead:<br>
|
|
|
|
* <br>
|
|
|
|
* <table border="1">
|
|
|
|
* <tr>
|
|
|
|
* <td>{@code date}</td>
|
|
|
|
* <td>{@code new Date()}</td>
|
|
|
|
* <tr>
|
|
|
|
* <tr>
|
|
|
|
* <td>{@code text}</td>
|
|
|
|
* <td>{@code ""}</td>
|
|
|
|
* <tr>
|
|
|
|
* <tr>
|
|
|
|
* <td>{@code status}</td>
|
|
|
|
* <td>{@code MessageStatus.WAITING}</td>
|
|
|
|
* <tr>
|
|
|
|
* </table>
|
2020-01-28 17:32:33 +01:00
|
|
|
*
|
2019-12-31 10:16:52 +01:00
|
|
|
* @return a new instance of {@link Message}
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public Message build() {
|
|
|
|
// Supplement default values
|
2020-01-28 17:32:33 +01:00
|
|
|
if (creationDate == null) creationDate = new Date();
|
2019-12-31 10:16:52 +01:00
|
|
|
if (text == null) text = "";
|
|
|
|
if (status == null) status = MessageStatus.WAITING;
|
|
|
|
|
2020-03-22 14:52:38 +01:00
|
|
|
return new Message(id, senderId, recipientId, creationDate, text, attachment, status, forwarded);
|
2019-12-31 10:16:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-28 17:32:33 +01:00
|
|
|
* @param creationDate the creation date of the {@link Message} to create
|
2019-12-31 10:16:52 +01:00
|
|
|
* @return this {@link MessageBuilder}
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
2020-01-28 17:32:33 +01:00
|
|
|
public MessageBuilder setDate(Date creationDate) {
|
|
|
|
this.creationDate = creationDate;
|
2019-12-31 10:16:52 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param text the text of the {@link Message} to create
|
|
|
|
* @return this {@link MessageBuilder}
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public MessageBuilder setText(String text) {
|
|
|
|
this.text = text;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param attachment the {@link MessageAttachment} of the {@link Message} to
|
|
|
|
* create
|
|
|
|
* @return this {@link MessageBuilder}
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public MessageBuilder setAttachment(MessageAttachment<?> attachment) {
|
|
|
|
this.attachment = attachment;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param status the {@link MessageStatus} of the {@link Message} to create
|
|
|
|
* @return this {@link MessageBuilder}
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public MessageBuilder setStatus(Message.MessageStatus status) {
|
|
|
|
this.status = status;
|
|
|
|
return this;
|
|
|
|
}
|
2020-03-22 14:52:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param forwarded sets whether this message is a forwarded message or not
|
|
|
|
* @return this {@link MessageBuilder}
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
|
|
|
public MessageBuilder setForwarded(boolean forwarded) {
|
|
|
|
this.forwarded = forwarded;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|