Made id a mandatory field in MessageBuilder

Added support for IdGenerator in the MessageBuilder constructor.
This commit is contained in:
Kai S. K. Engelbart 2020-01-28 17:32:33 +01:00
parent dd3f920e94
commit aed8016485
2 changed files with 34 additions and 28 deletions

View File

@ -64,20 +64,20 @@ public class Message implements Serializable {
* 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 date 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 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
* @since Envoy Common v0.2-alpha
*/
Message(long id, long senderId, long recipientId, Date date, String text, MessageAttachment<?> attachment, MessageStatus status) {
Message(long id, long senderId, long recipientId, Date creationDate, String text, MessageAttachment<?> attachment, MessageStatus status) {
this.id = id;
this.senderId = senderId;
this.recipientId = recipientId;
creationDate = date;
this.creationDate = creationDate;
this.text = text;
this.attachment = attachment;
this.status = status;

View File

@ -10,7 +10,7 @@ import envoy.data.Message.MessageStatus;
* 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
*/
@ -21,7 +21,7 @@ public class MessageBuilder {
// Properties with default values
private long id;
private Date date;
private Date creationDate;
private String text;
private MessageAttachment<?> attachment;
private Message.MessageStatus status;
@ -32,11 +32,27 @@ public class MessageBuilder {
*
* @param senderId the ID of the user who sends the {@link Message}
* @param recipientId the ID of the user who received 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) {
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 received 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;
}
/**
@ -57,36 +73,26 @@ public class MessageBuilder {
* <td>{@code MessageStatus.WAITING}</td>
* <tr>
* </table>
*
*
* @return a new instance of {@link Message}
* @since Envoy Common v0.2-alpha
*/
public Message build() {
// Supplement default values
if (date == null) date = new Date();
if (creationDate == null) creationDate = new Date();
if (text == null) text = "";
if (status == null) status = MessageStatus.WAITING;
return new Message(id, senderId, recipientId, date, text, attachment, status);
return new Message(id, senderId, recipientId, creationDate, text, attachment, status);
}
/**
* @param id the unique ID of the {@link Message} to create
* @param creationDate the creation date of the {@link Message} to create
* @return this {@link MessageBuilder}
* @since Envoy Common v0.2-alpha
*/
public MessageBuilder setId(long id) {
this.id = id;
return this;
}
/**
* @param date the creation date of the {@link Message} to create
* @return this {@link MessageBuilder}
* @since Envoy Common v0.2-alpha
*/
public MessageBuilder setDate(Date date) {
this.date = date;
public MessageBuilder setDate(Date creationDate) {
this.creationDate = creationDate;
return this;
}