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/MessageBuilder.java

125 lines
3.3 KiB
Java

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>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class MessageBuilder {
// Mandatory properties without default values
private final User sender, recipient;
// Properties with default values
private long id;
private Date date;
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 sender the user who sends the {@link Message}
* @param recipient the user who received the {@link Message}
* @since Envoy Common v0.2-alpha
*/
public MessageBuilder(User sender, User recipient) {
if (sender == null) throw new NullPointerException("Message sender is null");
if (recipient == null) throw new NullPointerException("Message recipient is null");
this.sender = sender;
this.recipient = recipient;
}
/**
* 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>
*
* @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 (text == null) text = "";
if (status == null) status = MessageStatus.WAITING;
return new Message(id, sender, recipient, date, text, attachment, status);
}
/**
* @param id the unique ID 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;
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;
}
}