220 lines
6.6 KiB
Java
220 lines
6.6 KiB
Java
package envoy.data;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
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>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public class MessageBuilder {
|
|
|
|
// Mandatory properties without default values
|
|
private final long senderID, recipientID;
|
|
|
|
// Properties with default values
|
|
private long id;
|
|
private LocalDateTime creationDate;
|
|
private String text;
|
|
private Attachment attachment;
|
|
private Message.MessageStatus status;
|
|
private boolean forwarded;
|
|
|
|
/**
|
|
* 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}
|
|
* @param recipientID the ID of the user who receives the {@link Message}
|
|
* @param idGenerator the ID generator used to generate a unique {@link Message}
|
|
* id
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public MessageBuilder(long senderID, long recipientID, IDGenerator idGenerator) { this(senderID, recipientID, idGenerator.next()); }
|
|
|
|
/**
|
|
* 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}
|
|
* @param recipientID the ID of the user who receives the {@link Message}
|
|
* @param messageId the ID of the {@link Message}
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public MessageBuilder(long senderID, long recipientID, long messageId) {
|
|
this.senderID = senderID;
|
|
this.recipientID = recipientID;
|
|
id = messageId;
|
|
}
|
|
|
|
/**
|
|
* 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}
|
|
* @param iDGenerator the ID generator used to generate a unique {@link Message}
|
|
* id
|
|
* @since Envoy v0.1-beta
|
|
*/
|
|
public MessageBuilder(Message msg, long recipientID, IDGenerator iDGenerator) {
|
|
this(msg.getRecipientID(), recipientID, iDGenerator.next());
|
|
this.attachment = msg.getAttachment();
|
|
this.creationDate = LocalDateTime.now();
|
|
this.forwarded = true;
|
|
this.text = msg.getText();
|
|
this.status = MessageStatus.WAITING;
|
|
}
|
|
|
|
/**
|
|
* 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 LocalDateTime.now()}</td>
|
|
* <tr>
|
|
* <tr>
|
|
* <td>{@code text}</td>
|
|
* <td>{@code ""}</td>
|
|
* <tr>
|
|
* <tr>
|
|
* <td>{@code status}</td>
|
|
* <td>{@code MessageStatus.WAITING}</td>
|
|
* <tr>
|
|
* </table>
|
|
*
|
|
* @return a new instance of {@link Message}
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public Message build() {
|
|
supplyDefaults();
|
|
return new Message(id, senderID, recipientID, creationDate, text, attachment, status, forwarded);
|
|
}
|
|
|
|
/**
|
|
* Creates an instance of {@link GroupMessage} with the previously supplied
|
|
* values. <br>
|
|
* <b> Sets all member statuses to {@link MessageStatus#WAITING}.</b><br>
|
|
* 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>
|
|
* </table>
|
|
*
|
|
* @param group the {@link Group} that is used to fill the map of member
|
|
* statuses
|
|
* @return a new instance of {@link GroupMessage}
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public GroupMessage buildGroupMessage(Group group) {
|
|
var memberStatuses = new HashMap<Long, Message.MessageStatus>();
|
|
group.getContacts().forEach(user -> memberStatuses.put(user.getID(), MessageStatus.WAITING));
|
|
return buildGroupMessage(group, memberStatuses);
|
|
}
|
|
|
|
/**
|
|
* Creates an instance of {@link GroupMessage} 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>
|
|
* </table>
|
|
*
|
|
* @param group the {@link Group} that is used to fill the map of
|
|
* member statuses
|
|
* @param memberStatuses the map of all current statuses
|
|
* @return a new instance of {@link GroupMessage}
|
|
* @since Envoy Common v0.1-beta
|
|
*/
|
|
public GroupMessage buildGroupMessage(Group group, Map<Long, MessageStatus> memberStatuses) {
|
|
if (group == null || memberStatuses == null) throw new NullPointerException();
|
|
supplyDefaults();
|
|
return new GroupMessage(id, senderID, recipientID, creationDate, text, attachment, status, forwarded, memberStatuses);
|
|
}
|
|
|
|
private void supplyDefaults() {
|
|
if (creationDate == null) creationDate = LocalDateTime.now();
|
|
if (text == null) text = "";
|
|
if (status == null) status = MessageStatus.WAITING;
|
|
}
|
|
|
|
/**
|
|
* @param creationDate the creation date of the {@link Message} to create
|
|
* @return this {@link MessageBuilder}
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public MessageBuilder setDate(LocalDateTime creationDate) {
|
|
this.creationDate = creationDate;
|
|
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 Attachment} of the {@link Message} to
|
|
* create
|
|
* @return this {@link MessageBuilder}
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public MessageBuilder setAttachment(Attachment 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;
|
|
}
|
|
|
|
/**
|
|
* @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;
|
|
}
|
|
}
|