From 5b568e8c435698b86d3c91b5f5312cb3feccc600 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 2 Jul 2020 10:58:02 +0200 Subject: [PATCH 1/5] Reworked Attachment MessageAttachment is now called Attachment and contains its data inside a byte array, as well as an AttachmentType. Closes #20 --- .settings/org.eclipse.wst.common.component | 12 ++-- src/main/java/envoy/data/Attachment.java | 69 +++++++++++++++++++ src/main/java/envoy/data/GroupMessage.java | 5 +- src/main/java/envoy/data/Message.java | 15 ++-- .../java/envoy/data/MessageAttachment.java | 43 ------------ src/main/java/envoy/data/MessageBuilder.java | 6 +- 6 files changed, 89 insertions(+), 61 deletions(-) create mode 100644 src/main/java/envoy/data/Attachment.java delete mode 100644 src/main/java/envoy/data/MessageAttachment.java diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 44aa2b3..c8aeed9 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,5 +1,6 @@ - + + @@ -7,7 +8,8 @@ - + + @@ -15,7 +17,8 @@ - + + @@ -23,7 +26,8 @@ - + + diff --git a/src/main/java/envoy/data/Attachment.java b/src/main/java/envoy/data/Attachment.java new file mode 100644 index 0000000..7b8d397 --- /dev/null +++ b/src/main/java/envoy/data/Attachment.java @@ -0,0 +1,69 @@ +package envoy.data; + +import java.io.Serializable; + +/** + * This interface should be used for any type supposed to be a {@link Message} + * attachment (i.e. images or sound). + *

+ * Project: envoy-common
+ * File: Attachment.java
+ * Created: 30 Dec 2019
+ * + * @author Leon Hofmeister + * @author Kai S. K. Engelbart + * @since Envoy Common v0.2-alpha + */ +public class Attachment implements Serializable { + + /** + * Defines the type of the attachment. + * + * @since Envoy Common v0.1-beta + */ + public enum AttachmentType { + + /** + * This attachment type denotes a picture. + * + * @since Envoy Common v0.1-beta + */ + PICTURE, + + /** + * This attachment type denotes a voice message. + * + * @since Envoy Common v0.1-beta + */ + VOICE + } + + private final byte[] data; + private final AttachmentType type; + + private static final long serialVersionUID = 1L; + + /** + * Constructs an attachment. + * + * @param data the data of the attachment + * @param type the type of the attachment + * @since Envoy Common v0.1-beta + */ + public Attachment(byte[] data, AttachmentType type) { + this.data = data; + this.type = type; + } + + /** + * @return the data of the attachment + * @since Envoy Common v0.1-beta + */ + public byte[] getData() { return data; } + + /** + * @return the type of the attachment + * @since Envoy Common v0.1-beta + */ + public AttachmentType getType() { return type; } +} diff --git a/src/main/java/envoy/data/GroupMessage.java b/src/main/java/envoy/data/GroupMessage.java index 85b61e8..cb79a12 100644 --- a/src/main/java/envoy/data/GroupMessage.java +++ b/src/main/java/envoy/data/GroupMessage.java @@ -38,9 +38,8 @@ public final class GroupMessage extends Message { * {@link GroupMessage} * @since Envoy Common v0.1-beta */ - GroupMessage(long id, long senderID, long groupID, LocalDateTime creationDate, String text, MessageAttachment attachment, - MessageStatus status, - boolean forwarded, Map memberStatuses) { + GroupMessage(long id, long senderID, long groupID, LocalDateTime creationDate, String text, + Attachment attachment, MessageStatus status, boolean forwarded, Map memberStatuses) { super(id, senderID, groupID, creationDate, text, attachment, status, forwarded); this.memberStatuses = memberStatuses; } diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index 4b579bc..54b405e 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -48,11 +48,11 @@ public class Message implements Serializable { READ } - private final long id, senderID, recipientID; - private final boolean forwarded; - private final LocalDateTime creationDate; - private final String text; - private final MessageAttachment attachment; + private final long id, senderID, recipientID; + private final boolean forwarded; + private final LocalDateTime creationDate; + private final String text; + private final Attachment attachment; private LocalDateTime receivedDate, readDate; private MessageStatus status; @@ -75,8 +75,7 @@ public class Message implements Serializable { * @param forwarded whether this message was forwarded * @since Envoy Common v0.2-alpha */ - Message(long id, long senderID, long recipientID, LocalDateTime creationDate, String text, MessageAttachment attachment, - MessageStatus status, + Message(long id, long senderID, long recipientID, LocalDateTime creationDate, String text, Attachment attachment, MessageStatus status, boolean forwarded) { this.id = id; this.senderID = senderID; @@ -178,7 +177,7 @@ public class Message implements Serializable { * @return the messageAttachment * @since Envoy Common v0.2-alpha */ - public MessageAttachment getAttachment() { return attachment; } + public Attachment getAttachment() { return attachment; } /** * @return the current status of this message diff --git a/src/main/java/envoy/data/MessageAttachment.java b/src/main/java/envoy/data/MessageAttachment.java deleted file mode 100644 index 3d04eab..0000000 --- a/src/main/java/envoy/data/MessageAttachment.java +++ /dev/null @@ -1,43 +0,0 @@ -package envoy.data; - -import java.io.IOException; -import java.io.Serializable; - -import envoy.util.SerializationUtils; - -/** - * This interface should be used for any type supposed to be a {@link Message} - * attachment (i.e. images or sound).
- *
- * Project: envoy-common
- * File: MessageAttachment.java
- * Created: 30 Dec 2019
- * - * @author Leon Hofmeister - * @param the type of this message attachment - * @since Envoy Common v0.2-alpha - */ -public class MessageAttachment implements Serializable { - - private static final long serialVersionUID = 0L; - private T value; - - /** - * @return the type implementing this interface - * @since Envoy Common v0.2-alpha - */ - T getValue() { return value; } - - /** - * @return the {@link MessageAttachment} as a byte array - * @throws IOException if the serialization failed - * @since Envoy Common v0.2-alpha - */ - byte[] toByteArray() throws IOException { return SerializationUtils.writeToByteArray(this); } - - /** - * @param value the value to set - * @since Envoy Common v0.2-alpha - */ - public void setValue(T value) { this.value = value; } -} diff --git a/src/main/java/envoy/data/MessageBuilder.java b/src/main/java/envoy/data/MessageBuilder.java index 934f65b..8a6164d 100644 --- a/src/main/java/envoy/data/MessageBuilder.java +++ b/src/main/java/envoy/data/MessageBuilder.java @@ -25,7 +25,7 @@ public class MessageBuilder { private long id; private LocalDateTime creationDate; private String text; - private MessageAttachment attachment; + private Attachment attachment; private Message.MessageStatus status; private boolean forwarded; @@ -187,12 +187,12 @@ public class MessageBuilder { } /** - * @param attachment the {@link MessageAttachment} of the {@link Message} to + * @param attachment the {@link Attachment} of the {@link Message} to * create * @return this {@link MessageBuilder} * @since Envoy Common v0.2-alpha */ - public MessageBuilder setAttachment(MessageAttachment attachment) { + public MessageBuilder setAttachment(Attachment attachment) { this.attachment = attachment; return this; } From 0f1bce87e0afe57e1ee600c9347eeaf1b242cea0 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 2 Jul 2020 14:34:32 +0200 Subject: [PATCH 2/5] Add Message#hasAttachment convenience method --- src/main/java/envoy/data/Message.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index 54b405e..ba0a756 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -179,6 +179,12 @@ public class Message implements Serializable { */ public Attachment getAttachment() { return attachment; } + /** + * @return {@code true} if an attachment is present + * @since Envoy Common v0.1-beta + */ + public boolean hasAttachment() { return attachment != null; } + /** * @return the current status of this message * @since Envoy Common v0.2-alpha From ef1d9785e08bab54d654eee29faef1def9ca6c0a Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 2 Jul 2020 14:47:04 +0200 Subject: [PATCH 3/5] Add received and read date to MessageBuilder This simplifies some calls on the server --- src/main/java/envoy/data/GroupMessage.java | 6 ++-- src/main/java/envoy/data/Message.java | 8 +++-- src/main/java/envoy/data/MessageBuilder.java | 31 ++++++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/data/GroupMessage.java b/src/main/java/envoy/data/GroupMessage.java index cb79a12..7fb0d4a 100644 --- a/src/main/java/envoy/data/GroupMessage.java +++ b/src/main/java/envoy/data/GroupMessage.java @@ -29,6 +29,8 @@ public final class GroupMessage extends Message { * @param senderID the ID of the user who sends the message * @param groupID the ID of the group which receives the message * @param creationDate the creation date of the message + * @param receivedDate the received date of the message + * @param readDate the read 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 Message.MessageStatus} of the @@ -38,9 +40,9 @@ public final class GroupMessage extends Message { * {@link GroupMessage} * @since Envoy Common v0.1-beta */ - GroupMessage(long id, long senderID, long groupID, LocalDateTime creationDate, String text, + GroupMessage(long id, long senderID, long groupID, LocalDateTime creationDate, LocalDateTime receivedDate, LocalDateTime readDate, String text, Attachment attachment, MessageStatus status, boolean forwarded, Map memberStatuses) { - super(id, senderID, groupID, creationDate, text, attachment, status, forwarded); + super(id, senderID, groupID, creationDate, receivedDate, readDate, text, attachment, status, forwarded); this.memberStatuses = memberStatuses; } diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index ba0a756..040ad59 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -69,18 +69,22 @@ public class Message implements Serializable { * @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 receivedDate the received date of the message + * @param readDate the read 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 forwarded whether this message was forwarded * @since Envoy Common v0.2-alpha */ - Message(long id, long senderID, long recipientID, LocalDateTime creationDate, String text, Attachment attachment, MessageStatus status, - boolean forwarded) { + Message(long id, long senderID, long recipientID, LocalDateTime creationDate, LocalDateTime receivedDate, LocalDateTime readDate, String text, + Attachment attachment, MessageStatus status, boolean forwarded) { this.id = id; this.senderID = senderID; this.recipientID = recipientID; this.creationDate = creationDate; + this.receivedDate = receivedDate; + this.readDate = readDate; this.text = text; this.attachment = attachment; this.status = status; diff --git a/src/main/java/envoy/data/MessageBuilder.java b/src/main/java/envoy/data/MessageBuilder.java index 8a6164d..917adc4 100644 --- a/src/main/java/envoy/data/MessageBuilder.java +++ b/src/main/java/envoy/data/MessageBuilder.java @@ -23,7 +23,7 @@ public class MessageBuilder { // Properties with default values private long id; - private LocalDateTime creationDate; + private LocalDateTime creationDate, receivedDate, readDate; private String text; private Attachment attachment; private Message.MessageStatus status; @@ -83,7 +83,8 @@ public class MessageBuilder { * * * - * + * * * * @@ -100,7 +101,7 @@ public class MessageBuilder { */ public Message build() { supplyDefaults(); - return new Message(id, senderID, recipientID, creationDate, text, attachment, status, forwarded); + return new Message(id, senderID, recipientID, creationDate, receivedDate, readDate, text, attachment, status, forwarded); } /** @@ -157,7 +158,7 @@ public class MessageBuilder { public GroupMessage buildGroupMessage(Group group, Map memberStatuses) { if (group == null || memberStatuses == null) throw new NullPointerException(); supplyDefaults(); - return new GroupMessage(id, senderID, recipientID, creationDate, text, attachment, status, forwarded, memberStatuses); + return new GroupMessage(id, senderID, recipientID, creationDate, receivedDate, readDate, text, attachment, status, forwarded, memberStatuses); } private void supplyDefaults() { @@ -171,11 +172,31 @@ public class MessageBuilder { * @return this {@link MessageBuilder} * @since Envoy Common v0.2-alpha */ - public MessageBuilder setDate(LocalDateTime creationDate) { + public MessageBuilder setCreationDate(LocalDateTime creationDate) { this.creationDate = creationDate; return this; } + /** + * @param receivedDate the received date of the {@link Message} to create + * @return this {@link MessageBuilder} + * @since Envoy Common v0.1-beta + */ + public MessageBuilder setReceivedDate(LocalDateTime receivedDate) { + this.receivedDate = receivedDate; + return this; + } + + /** + * @param readDate the read date of the {@link Message} to create + * @return this {@link MessageBuilder} + * @since Envoy Common v0.1-beta + */ + public MessageBuilder setReadDate(LocalDateTime readDate) { + this.readDate = readDate; + return this; + } + /** * @param text the text of the {@link Message} to create * @return this {@link MessageBuilder} From 87ef0c47423847610aefd052d6ce25fef800450f Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 2 Jul 2020 15:50:04 +0000 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: delvh --- src/main/java/envoy/data/Attachment.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/data/Attachment.java b/src/main/java/envoy/data/Attachment.java index 7b8d397..bfc496a 100644 --- a/src/main/java/envoy/data/Attachment.java +++ b/src/main/java/envoy/data/Attachment.java @@ -35,7 +35,14 @@ public class Attachment implements Serializable { * * @since Envoy Common v0.1-beta */ - VOICE + VOICE, + + /** + * This attachment type denotes a document. + * + * @since Envoy Common v0.1-beta + */ + DOCUMENT } private final byte[] data; From ac99b4b09b79e5788a1bc0f8697b2e473c6d7b12 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 2 Jul 2020 15:59:38 +0000 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: delvh --- src/main/java/envoy/data/Attachment.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/envoy/data/Attachment.java b/src/main/java/envoy/data/Attachment.java index bfc496a..177441b 100644 --- a/src/main/java/envoy/data/Attachment.java +++ b/src/main/java/envoy/data/Attachment.java @@ -30,6 +30,14 @@ public class Attachment implements Serializable { */ PICTURE, + + /** + * This attachment type denotes a video. + * + * @since Envoy Common v0.1-beta + */ + VIDEO, + /** * This attachment type denotes a voice message. *
{@code date}{@code LocalDateTime.now()}{@code LocalDateTime.now()} and {@code null} for {@code receivedDate} and + * {@code readDate}
{@code text}