Added MessageBuilder, enhanced Javadoc
This commit is contained in:
parent
4a8d5b5ef5
commit
b66900c864
@ -6,6 +6,8 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Formatter;
|
||||
|
||||
/**
|
||||
* Contains a {@link User}'s login information.<br>
|
||||
* <br>
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
* File: <strong>LoginCredentials.java</strong><br>
|
||||
* Created: <strong>29.12.2019</strong><br>
|
||||
@ -15,10 +17,11 @@ import java.util.Formatter;
|
||||
*/
|
||||
public class LoginCredentials implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -7395245059059523314L;
|
||||
private final String name;
|
||||
private final byte[] passwordHash;
|
||||
|
||||
private static final long serialVersionUID = -7395245059059523314L;
|
||||
|
||||
/**
|
||||
* Creates an in stance of {@link LoginCredentials}.
|
||||
*
|
||||
@ -44,10 +47,7 @@ public class LoginCredentials implements Serializable {
|
||||
*/
|
||||
public byte[] getPasswordHash() { return passwordHash; }
|
||||
|
||||
private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
return md.digest(input);
|
||||
}
|
||||
private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException { return MessageDigest.getInstance("SHA-256").digest(input); }
|
||||
|
||||
private byte[] toByteArray(char[] chars) {
|
||||
byte[] bytes = new byte[chars.length * 2];
|
||||
@ -62,12 +62,9 @@ public class LoginCredentials implements Serializable {
|
||||
public String toString() {
|
||||
try (Formatter form = new Formatter()) {
|
||||
form.format("LoginCredentials[name=%s,passwordHash=", name);
|
||||
for (byte element : passwordHash) {
|
||||
for (byte element : passwordHash)
|
||||
form.format("%02x", element);
|
||||
}
|
||||
form.format("]");
|
||||
String str = form.toString();
|
||||
return str;
|
||||
return form.format("]").toString();
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,6 @@ package envoy.data;
|
||||
import java.io.Serializable;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Represents a unique message with a unique, numeric ID. Further metadata
|
||||
@ -18,10 +17,10 @@ import java.util.Optional;
|
||||
* @author Leon Hofmeister
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public abstract class Message implements Serializable {
|
||||
public class Message implements Serializable {
|
||||
|
||||
/**
|
||||
* This enum defines all possible statuses a {link Message} can have.
|
||||
* This enumeration defines all possible statuses a {link Message} can have.
|
||||
*
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
@ -49,48 +48,41 @@ public abstract class Message implements Serializable {
|
||||
READ
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -4393477412979594435L;
|
||||
private final long id;
|
||||
|
||||
private final User sender, recipient;
|
||||
|
||||
private final Date date;
|
||||
|
||||
private final String text;
|
||||
|
||||
private final MessageAttachment<?> messageAttachment;
|
||||
private final long id;
|
||||
private final User sender, recipient;
|
||||
private final Date date;
|
||||
private final String text;
|
||||
private final MessageAttachment<?> attachment;
|
||||
|
||||
private MessageStatus status;
|
||||
|
||||
private static final long serialVersionUID = -4393477412979594435L;
|
||||
|
||||
/**
|
||||
* Initializes a {@link Message} from the client's perspective. The current date
|
||||
* is used as the message date and the status is set to
|
||||
* {@link MessageStatus#WAITING}.
|
||||
* Initializes a {@link Message} with values for all of its properties. The use
|
||||
* of this constructor is only intended for the {@link MessageBuilder} class, as
|
||||
* this class provides {@code null} checks and default values for all
|
||||
* properties.
|
||||
*
|
||||
* @param <T> the type of the attachment
|
||||
* @param id unique ID
|
||||
* @param sender the user who sends the message
|
||||
* @param recipient 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
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public <T> Message(long id, User sender, User recipient, String text, @SuppressWarnings("rawtypes") Optional<MessageAttachment> attachment) {
|
||||
this.id = id;
|
||||
this.sender = sender;
|
||||
this.recipient = recipient;
|
||||
this.text = text;
|
||||
this.messageAttachment = attachment.isEmpty() ? null : attachment.get();
|
||||
this.date = new Date();
|
||||
this.status = MessageStatus.WAITING;
|
||||
Message(long id, User sender, User recipient, Date date, String text, MessageAttachment<?> attachment, MessageStatus status) {
|
||||
this.id = id;
|
||||
this.sender = sender;
|
||||
this.recipient = recipient;
|
||||
this.date = date;
|
||||
this.text = text;
|
||||
this.attachment = attachment;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the date at which this message was created
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public Date getDate() { return date; }
|
||||
|
||||
/**
|
||||
* @return the ID of this message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
@ -98,11 +90,10 @@ public abstract class Message implements Serializable {
|
||||
public long getId() { return id; }
|
||||
|
||||
/**
|
||||
* @param <T> the type of the message attachment
|
||||
* @return the messageAttachment
|
||||
* @return the sender of this message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public <T> MessageAttachment<?> getMessageAttachment() { return messageAttachment; }
|
||||
public User getSender() { return sender; }
|
||||
|
||||
/**
|
||||
* @return the recipient of this message
|
||||
@ -111,16 +102,10 @@ public abstract class Message implements Serializable {
|
||||
public User getRecipient() { return recipient; }
|
||||
|
||||
/**
|
||||
* @return the sender of this message
|
||||
* @return the date at which this message was created
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public User getSender() { return sender; }
|
||||
|
||||
/**
|
||||
* @return the current status of this message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public MessageStatus getStatus() { return status; }
|
||||
public Date getDate() { return date; }
|
||||
|
||||
/**
|
||||
* @return the text content of this message
|
||||
@ -128,6 +113,18 @@ public abstract class Message implements Serializable {
|
||||
*/
|
||||
public String getText() { return text; }
|
||||
|
||||
/**
|
||||
* @return the messageAttachment
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public MessageAttachment<?> getAttachment() { return attachment; }
|
||||
|
||||
/**
|
||||
* @return the current status of this message
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public MessageStatus getStatus() { return status; }
|
||||
|
||||
/**
|
||||
* Changes the current {@link MessageStatus} to the next logical status.<br>
|
||||
* <br>
|
||||
@ -148,7 +145,7 @@ public abstract class Message implements Serializable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,content=%s]",
|
||||
return String.format("TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s]",
|
||||
id,
|
||||
sender,
|
||||
recipient,
|
||||
|
@ -2,10 +2,8 @@ package envoy.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.swing.Scrollable;
|
||||
|
||||
/**
|
||||
* This Interface should be used for any type supposed to be a {@link Message}
|
||||
* This interface should be used for any type supposed to be a {@link Message}
|
||||
* attachment (i.e. images or sound).<br>
|
||||
* <br>
|
||||
* Project: <strong>envoy-common</strong><br>
|
||||
@ -16,7 +14,7 @@ import javax.swing.Scrollable;
|
||||
* @param <T> the type of this message attachment
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
public interface MessageAttachment<T> extends Serializable, Scrollable {
|
||||
public interface MessageAttachment<T> extends Serializable {
|
||||
|
||||
/**
|
||||
* @return the type implementing this interface
|
||||
|
125
src/main/java/envoy/data/MessageBuilder.java
Normal file
125
src/main/java/envoy/data/MessageBuilder.java
Normal file
@ -0,0 +1,125 @@
|
||||
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;
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ import java.io.Serializable;
|
||||
public class User implements Serializable {
|
||||
|
||||
/**
|
||||
* This enum defines all possible statuses a user can have.
|
||||
* This enumeration defines all possible statuses a user can have.
|
||||
*
|
||||
* @since Envoy Common v0.2-alpha
|
||||
*/
|
||||
@ -44,14 +44,13 @@ public class User implements Serializable {
|
||||
OFFLINE;
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 3530947374856708236L;
|
||||
|
||||
private final long id;
|
||||
|
||||
private final String name;
|
||||
|
||||
private UserStatus status;
|
||||
|
||||
private static final long serialVersionUID = 3530947374856708236L;
|
||||
|
||||
/**
|
||||
* Initializes a {@link User}. The {@link UserStatus} is set to
|
||||
* {@link UserStatus#OFFLINE}.
|
||||
|
@ -11,6 +11,9 @@ import envoy.data.Message;
|
||||
*/
|
||||
public class MessageEvent implements Event<Message> {
|
||||
|
||||
/**
|
||||
* the {@link Message} attached to this {@link MessageEvent}.
|
||||
*/
|
||||
protected final Message message;
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user