From f93b01f65ae6a830e2790aaf121cb201f1eee17e Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 28 Mar 2020 15:21:02 +0100 Subject: [PATCH] Created ContactDeletionEvent and equals- + hashcode- method for Contact additionally extracted enum ElementOperation and fixed wrong toString-method in Message --- src/main/java/envoy/data/Contact.java | 20 ++- src/main/java/envoy/data/GroupMessage.java | 129 ++++++++++-------- src/main/java/envoy/data/Message.java | 3 +- src/main/java/envoy/data/MessageBuilder.java | 25 ++++ .../java/envoy/event/ElementOperation.java | 26 ++++ .../java/envoy/event/GroupCreationEvent.java | 4 +- .../java/envoy/event/GroupResizeEvent.java | 43 ++---- .../event/contact/ContactDeletionEvent.java | 26 ++++ .../{ => contact}/ContactOperationEvent.java | 35 +---- .../{ => contact}/ContactSearchRequest.java | 4 +- .../{ => contact}/ContactSearchResult.java | 3 +- .../envoy/event/contact/package-info.java | 13 ++ src/main/java/module-info.java | 1 + 13 files changed, 202 insertions(+), 130 deletions(-) create mode 100644 src/main/java/envoy/event/ElementOperation.java create mode 100644 src/main/java/envoy/event/contact/ContactDeletionEvent.java rename src/main/java/envoy/event/{ => contact}/ContactOperationEvent.java (51%) rename src/main/java/envoy/event/{ => contact}/ContactSearchRequest.java (91%) rename src/main/java/envoy/event/{ => contact}/ContactSearchResult.java (92%) create mode 100644 src/main/java/envoy/event/contact/package-info.java diff --git a/src/main/java/envoy/data/Contact.java b/src/main/java/envoy/data/Contact.java index 870aec8..53992c6 100644 --- a/src/main/java/envoy/data/Contact.java +++ b/src/main/java/envoy/data/Contact.java @@ -1,6 +1,7 @@ package envoy.data; import java.io.Serializable; +import java.util.Objects; /** * This class is the superclass for both {@link User} and {@link Group}.
@@ -16,7 +17,7 @@ import java.io.Serializable; public abstract class Contact implements Serializable { private final long id; - private String name; + private String name; private static final long serialVersionUID = 0L; @@ -51,9 +52,24 @@ public abstract class Contact implements Serializable { public void setName(String name) { this.name = name; } /** - * * {@inheritDoc} */ @Override public String toString() { return String.format("Contact[id=%d,name=%s]", id, name); } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { return Objects.hash(id); } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Contact)) return false; + return id == ((Contact) obj).id; + } } diff --git a/src/main/java/envoy/data/GroupMessage.java b/src/main/java/envoy/data/GroupMessage.java index 5845c2a..b1c05ce 100644 --- a/src/main/java/envoy/data/GroupMessage.java +++ b/src/main/java/envoy/data/GroupMessage.java @@ -1,60 +1,69 @@ -package envoy.data; - -import java.util.Collections; -import java.util.Date; -import java.util.Map; - -/** - * Project: envoy-common
- * File: GroupMessage.java
- * Created: 26.03.2020
- * - * @author Maximilian Käfer - * @since Envoy Common v0.1-beta - */ -public final class GroupMessage extends Message { - - private final Map memberStatuses; - - private static final long serialVersionUID = 0L; - - /** - * Initializes a {@link GroupMessage} 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 id unique ID - * @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 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 - * @param memberStatuses a map of all members and their status according to this - * {@link GroupMessage} - * @since Envoy Common v0.1-beta - */ - GroupMessage(long id, long senderID, long groupID, Date creationDate, String text, MessageAttachment attachment, MessageStatus status, - boolean forwarded, Map memberStatuses) { - super(id, senderID, groupID, creationDate, text, attachment, status, forwarded); - this.memberStatuses = memberStatuses; - } - - public void updateStatus() { - setStatus(Collections.min(memberStatuses.values())); - switch (getStatus()) { - case RECEIVED: - setReceivedDate(new Date()); - break; - - case READ: - setReadDate(new Date()); - break; - } - } - - public Map getMemberStatuses() { return memberStatuses; } -} +package envoy.data; + +import java.util.Collections; +import java.util.Date; +import java.util.Map; + +/** + * Project: envoy-common
+ * File: GroupMessage.java
+ * Created: 26.03.2020
+ * + * @author Maximilian Käfer + * @since Envoy Common v0.1-beta + */ +public final class GroupMessage extends Message { + + private final Map memberStatuses; + + private static final long serialVersionUID = 0L; + + /** + * Initializes a {@link GroupMessage} 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 id unique ID + * @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 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 + * @param memberStatuses a map of all members and their status according to this + * {@link GroupMessage} + * @since Envoy Common v0.1-beta + */ + GroupMessage(long id, long senderID, long groupID, Date creationDate, String text, MessageAttachment attachment, MessageStatus status, + boolean forwarded, Map memberStatuses) { + super(id, senderID, groupID, creationDate, text, attachment, status, forwarded); + this.memberStatuses = memberStatuses; + } + + /** + * Sets the status to be the minimum of all members. + * + * @since Envoy Common v0.1-beta + */ + public void updateStatus() { + setStatus(Collections.min(memberStatuses.values())); + switch (getStatus()) { + case RECEIVED: + setReceivedDate(new Date()); + break; + + case READ: + setReadDate(new Date()); + break; + } + } + + /** + * @return the map of all statuses in this {@link GroupMessage} + * @since Envoy Common v0.1-beta + */ + public Map getMemberStatuses() { return memberStatuses; } +} diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index ff9a453..ec7faf4 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -107,8 +107,7 @@ public class Message implements Serializable { @Override public String toString() { - return String.format( - "TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,forwarded,hasAttachment=%b]", + return String.format("Message[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,forwarded=%b,hasAttachment=%b]", id, senderID, recipientID, diff --git a/src/main/java/envoy/data/MessageBuilder.java b/src/main/java/envoy/data/MessageBuilder.java index 34dc3f1..c1a5c6f 100644 --- a/src/main/java/envoy/data/MessageBuilder.java +++ b/src/main/java/envoy/data/MessageBuilder.java @@ -102,6 +102,31 @@ public class MessageBuilder { return new Message(id, senderID, recipientID, creationDate, text, attachment, status, forwarded); } + /** + * Creates an instance of {@link GroupMessage} with the previously supplied + * values. If a mandatory value is not set, a default value will be used + * instead:
+ *
+ * + * + * + * + * + * + * + * + * + * + * + * + * + *
{@code date}{@code new Date()}
{@code text}{@code ""}
{@code status}{@code MessageStatus.WAITING}
+ * + * @param group the {@link Group} that is used to fill the map of member + * statuses + * @return a new instance of {@link GroupMessage} + * @since Envoy Common v0.2-alpha + */ public GroupMessage buildGroupMessage(Group group) { if (group == null) throw new NullPointerException(); supplyDefaults(); diff --git a/src/main/java/envoy/event/ElementOperation.java b/src/main/java/envoy/event/ElementOperation.java new file mode 100644 index 0000000..e0ed0df --- /dev/null +++ b/src/main/java/envoy/event/ElementOperation.java @@ -0,0 +1,26 @@ +package envoy.event; + +/** + * This enum declares all modification possibilities for a given container.
+ * These can be: {@link ElementOperation#ADD} or + * {@link ElementOperation#REMOVE}.
+ *
+ * Project: envoy-common
+ * File: ElementOperation.java
+ * Created: 25 Mar 2020
+ * + * @author Leon Hofmeister + * @since Envoy Common v0.1-beta + */ +public enum ElementOperation { + /** + * Select this element, if the given element should be added to the given + * container + */ + ADD, + /** + * Select this element, if the given element should be removed from the given + * container + */ + REMOVE +} diff --git a/src/main/java/envoy/event/GroupCreationEvent.java b/src/main/java/envoy/event/GroupCreationEvent.java index 17810e9..5b6d365 100644 --- a/src/main/java/envoy/event/GroupCreationEvent.java +++ b/src/main/java/envoy/event/GroupCreationEvent.java @@ -1,8 +1,8 @@ package envoy.event; /** - * This event creates a group with the given name. - * + * This event creates a group with the given name.
+ *
* Project: envoy-common
* File: GroupCreationEvent.java
* Created: 25 Mar 2020
diff --git a/src/main/java/envoy/event/GroupResizeEvent.java b/src/main/java/envoy/event/GroupResizeEvent.java index 110ea9b..24f4ac8 100644 --- a/src/main/java/envoy/event/GroupResizeEvent.java +++ b/src/main/java/envoy/event/GroupResizeEvent.java @@ -19,54 +19,29 @@ import envoy.data.User; */ public class GroupResizeEvent extends Event { - private final long groupID; - private final Operation operation; + private final long groupID; + private final ElementOperation operation; private static final long serialVersionUID = 0L; - /** - * This enum defines all possibilities for handling - * {@link GroupResizeEvent}s.
- * These can be: {@link Operation#ADD} or {@link Operation#REMOVE}.
- *
- * Project: envoy-common
- * File: GroupResizeEvent.java
- * Created: 25 Mar 2020
- * - * @author Leon Hofmeister - * @since Envoy Common v0.1-beta - */ - public enum Operation { - /** - * Select this element, if the given {@link User} should be added to the given - * {@link Group} - */ - ADD, - /** - * Select this element, if the given {@link User} should be removed from the - * given {@link Group} - */ - REMOVE - } - /** * Initializes a {@link GroupResizeEvent} through a Contact where the name has * already been set. * * @param user the {@link User} who wants to join or leave a group * @param group the {@link Group} he wants to join or leave - * @param operation whether the user should be removed from this group. If - * false, this user will be added. + * @param operation describes what to do with the given user:
+ * add him to this group or remove him from it * @since Envoy Common v0.2-alpha */ - public GroupResizeEvent(User user, Group group, Operation operation) { + public GroupResizeEvent(User user, Group group, ElementOperation operation) { super(user.getID()); if (group.getMemberIDs().contains(user.getID())) { - if (operation.equals(Operation.ADD)) throw new IllegalStateException( + if (operation.equals(ElementOperation.ADD)) throw new IllegalStateException( "Cannot add " + user + " to group " + group.getID() + " because he is already a member of this group"); - } else if (operation.equals(Operation.REMOVE)) + } else if (operation.equals(ElementOperation.REMOVE)) throw new IllegalStateException("Cannot remove " + user + " from group " + group.getID() + " because he is no part of this group"); - groupID = group.getID(); + groupID = group.getID(); this.operation = operation; } @@ -80,7 +55,7 @@ public class GroupResizeEvent extends Event { * @return the operationType * @since Envoy Common v0.1-beta */ - public Operation getOperation() { return operation; } + public ElementOperation getOperation() { return operation; } /** * {@inheritDoc} diff --git a/src/main/java/envoy/event/contact/ContactDeletionEvent.java b/src/main/java/envoy/event/contact/ContactDeletionEvent.java new file mode 100644 index 0000000..79c13d1 --- /dev/null +++ b/src/main/java/envoy/event/contact/ContactDeletionEvent.java @@ -0,0 +1,26 @@ +package envoy.event.contact; + +import envoy.data.Contact; +import envoy.event.Event; + +/** + * This event is sent if a {@link Contact} has been deleted/ has decided to + * delete himself.
+ *
+ * Project: envoy-common
+ * File: ContactDeletionEvent.java
+ * Created: 28 Mar 2020
+ * + * @author Leon Hofmeister + * @since Envoy Common v0.1-beta + */ +public class ContactDeletionEvent extends Event { + + private static final long serialVersionUID = 0L; + + /** + * @param value the id of the {@link Contact} being deleted + * @since Envoy Common v0.1-beta + */ + protected ContactDeletionEvent(Long value) { super(value); } +} diff --git a/src/main/java/envoy/event/ContactOperationEvent.java b/src/main/java/envoy/event/contact/ContactOperationEvent.java similarity index 51% rename from src/main/java/envoy/event/ContactOperationEvent.java rename to src/main/java/envoy/event/contact/ContactOperationEvent.java index 9fe505d..59a08cf 100644 --- a/src/main/java/envoy/event/ContactOperationEvent.java +++ b/src/main/java/envoy/event/contact/ContactOperationEvent.java @@ -1,6 +1,8 @@ -package envoy.event; +package envoy.event.contact; import envoy.data.User; +import envoy.event.ElementOperation; +import envoy.event.Event; /** * Signifies the modification of a contact list.
@@ -14,32 +16,9 @@ import envoy.data.User; */ public class ContactOperationEvent extends Event { - /** - * Specifies the operation performed on a contact list.
- *
- * Project: envoy-common
- * File: ContactOperationEvent.java
- * Created: 05.02.2020
- * - * @author Maximilian Käfer - * @since Envoy Common v0.2-alpha - */ - public enum Operation { + private final ElementOperation operationType; - /** - * Adds a user to the contact list. - */ - ADD, - - /** - * Removes a user from the contact list. - */ - REMOVE; - } - - private final Operation operationType; - - private static final long serialVersionUID = 0L; + private static final long serialVersionUID = 1L; /** * Initializes a {@link ContactOperationEvent}. @@ -48,7 +27,7 @@ public class ContactOperationEvent extends Event { * @param operationType the type of operation to perform * @since Envoy Common v0.2-alpha */ - public ContactOperationEvent(User contact, Operation operationType) { + public ContactOperationEvent(User contact, ElementOperation operationType) { super(contact); this.operationType = operationType; } @@ -57,5 +36,5 @@ public class ContactOperationEvent extends Event { * @return the type of operation to perform * @since Envoy Common v0.2-alpha */ - public Operation getOperationType() { return operationType; } + public ElementOperation getOperationType() { return operationType; } } diff --git a/src/main/java/envoy/event/ContactSearchRequest.java b/src/main/java/envoy/event/contact/ContactSearchRequest.java similarity index 91% rename from src/main/java/envoy/event/ContactSearchRequest.java rename to src/main/java/envoy/event/contact/ContactSearchRequest.java index bd13812..e7c4f22 100644 --- a/src/main/java/envoy/event/ContactSearchRequest.java +++ b/src/main/java/envoy/event/contact/ContactSearchRequest.java @@ -1,4 +1,6 @@ -package envoy.event; +package envoy.event.contact; + +import envoy.event.Event; /** * Requests a contact search from the server.
diff --git a/src/main/java/envoy/event/ContactSearchResult.java b/src/main/java/envoy/event/contact/ContactSearchResult.java similarity index 92% rename from src/main/java/envoy/event/ContactSearchResult.java rename to src/main/java/envoy/event/contact/ContactSearchResult.java index 5f1691e..635d5fd 100644 --- a/src/main/java/envoy/event/ContactSearchResult.java +++ b/src/main/java/envoy/event/contact/ContactSearchResult.java @@ -1,8 +1,9 @@ -package envoy.event; +package envoy.event.contact; import java.util.List; import envoy.data.User; +import envoy.event.Event; /** * Contains a list of {@link User}s for which a search was performed.
diff --git a/src/main/java/envoy/event/contact/package-info.java b/src/main/java/envoy/event/contact/package-info.java new file mode 100644 index 0000000..375ea89 --- /dev/null +++ b/src/main/java/envoy/event/contact/package-info.java @@ -0,0 +1,13 @@ +/** + * This package contains all contact-related events.
+ *
+ * Project: envoy-common
+ * File: package-info.java
+ * Created: 28 Mar 2020
+ * + * @author Leon Hofmeister + * @author Maximilian Käfer + * @author Kai S.K. Engelbart + * @since Envoy Common v0.1-beta + */ +package envoy.event.contact; diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 7177d83..e4aa197 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -13,6 +13,7 @@ module envoy.common { exports envoy.util; exports envoy.exception; exports envoy.event; + exports envoy.event.contact; requires transitive java.logging; }