Replace Date by LocalDateTime

Closes #22
This commit is contained in:
Kai S. K. Engelbart 2020-06-20 09:02:01 +02:00
parent bb6360d2a7
commit 9193468213
5 changed files with 31 additions and 30 deletions

View File

@ -1,7 +1,7 @@
package envoy.data; package envoy.data;
import java.time.LocalDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.Map; import java.util.Map;
/** /**
@ -38,7 +38,8 @@ public final class GroupMessage extends Message {
* {@link GroupMessage} * {@link GroupMessage}
* @since Envoy Common v0.1-beta * @since Envoy Common v0.1-beta
*/ */
GroupMessage(long id, long senderID, long groupID, Date creationDate, String text, MessageAttachment<?> attachment, MessageStatus status, GroupMessage(long id, long senderID, long groupID, LocalDateTime creationDate, String text, MessageAttachment<?> attachment,
MessageStatus status,
boolean forwarded, Map<Long, MessageStatus> memberStatuses) { boolean forwarded, Map<Long, MessageStatus> memberStatuses) {
super(id, senderID, groupID, creationDate, text, attachment, status, forwarded); super(id, senderID, groupID, creationDate, text, attachment, status, forwarded);
this.memberStatuses = memberStatuses; this.memberStatuses = memberStatuses;
@ -53,10 +54,10 @@ public final class GroupMessage extends Message {
setStatus(Collections.min(memberStatuses.values())); setStatus(Collections.min(memberStatuses.values()));
switch (getStatus()) { switch (getStatus()) {
case RECEIVED: case RECEIVED:
setReceivedDate(new Date()); setReceivedDate(LocalDateTime.now());
break; break;
case READ: case READ:
setReadDate(new Date()); setReadDate(LocalDateTime.now());
break; break;
} }
} }

View File

@ -1,8 +1,8 @@
package envoy.data; package envoy.data;
import java.io.Serializable; import java.io.Serializable;
import java.text.SimpleDateFormat; import java.time.LocalDateTime;
import java.util.Date; import java.time.format.DateTimeFormatter;
/** /**
* Represents a unique message with a unique, numeric ID. Further metadata * Represents a unique message with a unique, numeric ID. Further metadata
@ -50,14 +50,14 @@ public class Message implements Serializable {
private final long id, senderID, recipientID; private final long id, senderID, recipientID;
private final boolean forwarded; private final boolean forwarded;
private final Date creationDate; private final LocalDateTime creationDate;
private final String text; private final String text;
private final MessageAttachment<?> attachment; private final MessageAttachment<?> attachment;
private Date receivedDate, readDate; private LocalDateTime receivedDate, readDate;
private MessageStatus status; private MessageStatus status;
private static final long serialVersionUID = 0L; private static final long serialVersionUID = 1L;
/** /**
* Initializes a {@link Message} with values for all of its properties. The use * Initializes a {@link Message} with values for all of its properties. The use
@ -75,7 +75,8 @@ public class Message implements Serializable {
* @param forwarded whether this message was forwarded * @param forwarded whether this message was forwarded
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
Message(long id, long senderID, long recipientID, Date creationDate, String text, MessageAttachment<?> attachment, MessageStatus status, Message(long id, long senderID, long recipientID, LocalDateTime creationDate, String text, MessageAttachment<?> attachment,
MessageStatus status,
boolean forwarded) { boolean forwarded) {
this.id = id; this.id = id;
this.senderID = senderID; this.senderID = senderID;
@ -111,7 +112,7 @@ public class Message implements Serializable {
id, id,
senderID, senderID,
recipientID, recipientID,
new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(creationDate), DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss").format(creationDate),
status, status,
text, text,
forwarded, forwarded,
@ -140,32 +141,32 @@ public class Message implements Serializable {
* @return the date at which this message was created * @return the date at which this message was created
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public Date getCreationDate() { return creationDate; } public LocalDateTime getCreationDate() { return creationDate; }
/** /**
* @return the date at which the message has been received by the sender * @return the date at which the message has been received by the sender
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public Date getReceivedDate() { return receivedDate; } public LocalDateTime getReceivedDate() { return receivedDate; }
/** /**
* @param receivedDate the date at which the message has been received by the * @param receivedDate the date at which the message has been received by the
* sender * sender
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public void setReceivedDate(Date receivedDate) { this.receivedDate = receivedDate; } public void setReceivedDate(LocalDateTime receivedDate) { this.receivedDate = receivedDate; }
/** /**
* @return the date at which the message has been read by the sender * @return the date at which the message has been read by the sender
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public Date getReadDate() { return readDate; } public LocalDateTime getReadDate() { return readDate; }
/** /**
* @param readDate at which the message has been read by the sender * @param readDate at which the message has been read by the sender
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public void setReadDate(Date readDate) { this.readDate = readDate; } public void setReadDate(LocalDateTime readDate) { this.readDate = readDate; }
/** /**
* @return the text content of this message * @return the text content of this message

View File

@ -1,6 +1,6 @@
package envoy.data; package envoy.data;
import java.util.Date; import java.time.LocalDateTime;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -23,7 +23,7 @@ public class MessageBuilder {
// Properties with default values // Properties with default values
private long id; private long id;
private Date creationDate; private LocalDateTime creationDate;
private String text; private String text;
private MessageAttachment<?> attachment; private MessageAttachment<?> attachment;
private Message.MessageStatus status; private Message.MessageStatus status;
@ -70,7 +70,7 @@ public class MessageBuilder {
public MessageBuilder(Message msg, long recipientID, IDGenerator iDGenerator) { public MessageBuilder(Message msg, long recipientID, IDGenerator iDGenerator) {
this(msg.getRecipientID(), recipientID, iDGenerator.next()); this(msg.getRecipientID(), recipientID, iDGenerator.next());
this.attachment = msg.getAttachment(); this.attachment = msg.getAttachment();
this.creationDate = new Date(); this.creationDate = LocalDateTime.now();
this.forwarded = true; this.forwarded = true;
this.text = msg.getText(); this.text = msg.getText();
this.status = MessageStatus.WAITING; this.status = MessageStatus.WAITING;
@ -83,7 +83,7 @@ public class MessageBuilder {
* <table border="1"> * <table border="1">
* <tr> * <tr>
* <td>{@code date}</td> * <td>{@code date}</td>
* <td>{@code new Date()}</td> * <td>{@code LocalDateTime.now()}</td>
* <tr> * <tr>
* <tr> * <tr>
* <td>{@code text}</td> * <td>{@code text}</td>
@ -161,7 +161,7 @@ public class MessageBuilder {
} }
private void supplyDefaults() { private void supplyDefaults() {
if (creationDate == null) creationDate = new Date(); if (creationDate == null) creationDate = LocalDateTime.now();
if (text == null) text = ""; if (text == null) text = "";
if (status == null) status = MessageStatus.WAITING; if (status == null) status = MessageStatus.WAITING;
} }
@ -171,7 +171,7 @@ public class MessageBuilder {
* @return this {@link MessageBuilder} * @return this {@link MessageBuilder}
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public MessageBuilder setDate(Date creationDate) { public MessageBuilder setDate(LocalDateTime creationDate) {
this.creationDate = creationDate; this.creationDate = creationDate;
return this; return this;
} }

View File

@ -1,6 +1,6 @@
package envoy.event; package envoy.event;
import java.util.Date; import java.time.LocalDateTime;
import envoy.data.GroupMessage; import envoy.data.GroupMessage;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
@ -29,7 +29,7 @@ public class GroupMessageStatusChangeEvent extends MessageStatusChangeEvent {
* @param memberID the ID of the group member that caused the status change * @param memberID the ID of the group member that caused the status change
* @since Envoy Common v0.1-beta * @since Envoy Common v0.1-beta
*/ */
public GroupMessageStatusChangeEvent(long id, MessageStatus status, Date date, long memberID) { public GroupMessageStatusChangeEvent(long id, MessageStatus status, LocalDateTime date, long memberID) {
super(id, status, date); super(id, status, date);
this.memberID = memberID; this.memberID = memberID;
} }
@ -42,5 +42,4 @@ public class GroupMessageStatusChangeEvent extends MessageStatusChangeEvent {
@Override @Override
public String toString() { return String.format("GroupMessageStatusChangeEvent[meta=%s,memberID=%d]", super.toString(), memberID); } public String toString() { return String.format("GroupMessageStatusChangeEvent[meta=%s,memberID=%d]", super.toString(), memberID); }
} }

View File

@ -1,6 +1,6 @@
package envoy.event; package envoy.event;
import java.util.Date; import java.time.LocalDateTime;
import envoy.data.Message; import envoy.data.Message;
@ -15,7 +15,7 @@ import envoy.data.Message;
public class MessageStatusChangeEvent extends Event<Message.MessageStatus> { public class MessageStatusChangeEvent extends Event<Message.MessageStatus> {
private final long id; private final long id;
private final Date date; private final LocalDateTime date;
private static final long serialVersionUID = 0L; private static final long serialVersionUID = 0L;
@ -28,7 +28,7 @@ public class MessageStatusChangeEvent extends Event<Message.MessageStatus> {
* @param date the date at which the MessageStatus change occurred * @param date the date at which the MessageStatus change occurred
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public MessageStatusChangeEvent(long id, Message.MessageStatus status, Date date) { public MessageStatusChangeEvent(long id, Message.MessageStatus status, LocalDateTime date) {
super(status); super(status);
this.id = id; this.id = id;
this.date = date; this.date = date;
@ -40,7 +40,7 @@ public class MessageStatusChangeEvent extends Event<Message.MessageStatus> {
* @param message the message from which to build the event * @param message the message from which to build the event
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public MessageStatusChangeEvent(Message message) { this(message.getID(), message.getStatus(), new Date()); } public MessageStatusChangeEvent(Message message) { this(message.getID(), message.getStatus(), LocalDateTime.now()); }
/** /**
* @return the ID of the {@link Message} this event is related to * @return the ID of the {@link Message} this event is related to
@ -52,7 +52,7 @@ public class MessageStatusChangeEvent extends Event<Message.MessageStatus> {
* @return the date at which the status change occurred * @return the date at which the status change occurred
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public Date getDate() { return date; } public LocalDateTime getDate() { return date; }
@Override @Override
public String toString() { return String.format("MessageStatusChangeEvent[id=%d,status=%s,date=%s]", id, value, date); } public String toString() { return String.format("MessageStatusChangeEvent[id=%d,status=%s,date=%s]", id, value, date); }