package envoy.data; import java.util.Date; import envoy.data.Message.MessageStatus; /** * Provides a method of constructing the {@link Message} class.
*
* Project: envoy-common
* File: MessageBuilder.java
* Created: 31.12.2019
* * @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 Date creationDate; private String text; private MessageAttachment attachment; private Message.MessageStatus status; /** * 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 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 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; } /** * 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:
*
* * * * * * * * * * * * * *
{@code date}{@code new Date()}
{@code text}{@code ""}
{@code status}{@code MessageStatus.WAITING}
* * @return a new instance of {@link Message} * @since Envoy Common v0.2-alpha */ public Message build() { // Supplement default values if (creationDate == null) creationDate = new Date(); if (text == null) text = ""; if (status == null) status = MessageStatus.WAITING; return new Message(id, senderId, recipientId, creationDate, text, attachment, status); } /** * @param creationDate the creation date of the {@link Message} to create * @return this {@link MessageBuilder} * @since Envoy Common v0.2-alpha */ public MessageBuilder setDate(Date 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 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; } }