Reworked Attachment

MessageAttachment is now called Attachment and contains its data inside
a byte array, as well as an AttachmentType.

Closes #20
This commit is contained in:
Kai S. K. Engelbart 2020-07-02 10:58:02 +02:00
parent 8540549ef2
commit 5b568e8c43
6 changed files with 89 additions and 61 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
@ -7,7 +8,8 @@
<wb-module deploy-name="envoy-common">
@ -15,7 +17,8 @@
<wb-resource deploy-path="/" source-path="/src/main/java"/>
@ -23,7 +26,8 @@
</wb-module>

View File

@ -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).
* <p>
* Project: <strong>envoy-common</strong><br>
* File: <strong>Attachment.java</strong><br>
* Created: <strong>30 Dec 2019</strong><br>
*
* @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; }
}

View File

@ -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<Long, MessageStatus> memberStatuses) {
GroupMessage(long id, long senderID, long groupID, LocalDateTime creationDate, String text,
Attachment attachment, MessageStatus status, boolean forwarded, Map<Long, MessageStatus> memberStatuses) {
super(id, senderID, groupID, creationDate, text, attachment, status, forwarded);
this.memberStatuses = memberStatuses;
}

View File

@ -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

View File

@ -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).<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>MessageAttachment.java</strong><br>
* Created: <strong>30 Dec 2019</strong><br>
*
* @author Leon Hofmeister
* @param <T> the type of this message attachment
* @since Envoy Common v0.2-alpha
*/
public class MessageAttachment<T> 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; }
}

View File

@ -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;
}