From d6a4378db9562e21f482bc68b993dfdb39cf757b Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Wed, 5 Feb 2020 22:20:04 +0100 Subject: [PATCH 1/6] Contacts events Created 2 events used for the contact list (add/remove) of the client and requesting a list of users form the server. --- .../java/envoy/event/ContactOperation.java | 50 +++++++++++++++++++ .../java/envoy/event/ContactsRequest.java | 32 ++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/main/java/envoy/event/ContactOperation.java create mode 100644 src/main/java/envoy/event/ContactsRequest.java diff --git a/src/main/java/envoy/event/ContactOperation.java b/src/main/java/envoy/event/ContactOperation.java new file mode 100644 index 0000000..1a5a108 --- /dev/null +++ b/src/main/java/envoy/event/ContactOperation.java @@ -0,0 +1,50 @@ +package envoy.event; + +import envoy.data.User; + +/** + * Project: envoy-common
+ * File: ContactOperation.java
+ * Created: 05.02.2020
+ * + * @author Maximilian Käfer + * @since Envoy Common v0.2-alpha + */ +public class ContactOperation implements Event { + + public enum Operation { + ADD, REMOVE; + } + + private final User contact; + private final Operation operationType; + + private static final long serialVersionUID = -1166652868189511553L; + + /** + * Initializes a {@link ContacsOperation}. + * + * @param operationType the {@link Operation} which should be executed on the + * {@link contact}. + * @since Envoy Common v0.2-alpha + */ + public ContactOperation(User contact, Operation operationType) { + this.contact = contact; + this.operationType = operationType; + } + + /** + * @return the {@link User}, which should be added/removed from the contacts + * list. + * @since Envoy Common v0.2-alpha + */ + @Override + public User get() { return contact; } + + /** + * @return the {@link Operation}, which should be executed with the contact, + * this event corresponds with. + * @since Envoy Common v0.2-alpha + */ + public Operation getOperationType() { return operationType; } +} diff --git a/src/main/java/envoy/event/ContactsRequest.java b/src/main/java/envoy/event/ContactsRequest.java new file mode 100644 index 0000000..04abcfd --- /dev/null +++ b/src/main/java/envoy/event/ContactsRequest.java @@ -0,0 +1,32 @@ +package envoy.event; + +/** + * Project: envoy-common
+ * File: ContactsRequest.java
+ * Created: 05.02.2020
+ * + * @author Maximilian Käfer + * @since Envoy Common v0.2-alpha + */ +public class ContactsRequest implements Event { + + private final String request; + + private static final long serialVersionUID = -7969312055630533627L; + + /** + * Initializes a {@link ContactsRequest}. + * + * @param request the string, which is entered by the client to request + * corresponding clients as contacts from the server. + * @since Envoy Common v0.2-alpha + */ + public ContactsRequest(String request) { this.request = request; } + + /** + * @return the request string + * @since Envoy Common v0.2-alpha + */ + @Override + public String get() { return request; } +} From e443c9149aafcc55c7267e14b294578413276b3f Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 8 Feb 2020 13:44:05 +0100 Subject: [PATCH 2/6] Removed user id from Contacts --- src/main/java/envoy/data/Contacts.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/envoy/data/Contacts.java b/src/main/java/envoy/data/Contacts.java index bd19102..12846ac 100644 --- a/src/main/java/envoy/data/Contacts.java +++ b/src/main/java/envoy/data/Contacts.java @@ -13,7 +13,6 @@ import java.util.List; */ public class Contacts implements Serializable { - private final long userId; private final List contacts; private static final long serialVersionUID = 136970804968152871L; @@ -25,20 +24,13 @@ public class Contacts implements Serializable { * @param contacts the contact list * @since Envoy Common v0.2-alpha */ - public Contacts(long userId, List contacts) { - this.userId = userId; + public Contacts(List contacts) { this.contacts = contacts; } @Override public String toString() { return String.format("Contacts[%s]", contacts); } - /** - * @return the ID of the user this contacts belong to - * @since Envoy Common v0.2-alpha - */ - public long getUserId() { return userId; } - /** * @return a list of users messages can be sent to * @since Envoy Common v0.2-alpha From a767f10ac96040c2f90facf5ad19145abc95add8 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 9 Feb 2020 23:58:09 +0100 Subject: [PATCH 3/6] Adjusted ContactsRequest --- src/main/java/envoy/event/ContactsRequest.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/event/ContactsRequest.java b/src/main/java/envoy/event/ContactsRequest.java index 04abcfd..00d593d 100644 --- a/src/main/java/envoy/event/ContactsRequest.java +++ b/src/main/java/envoy/event/ContactsRequest.java @@ -1,5 +1,7 @@ package envoy.event; +import envoy.data.User; + /** * Project: envoy-common
* File: ContactsRequest.java
@@ -11,6 +13,7 @@ package envoy.event; public class ContactsRequest implements Event { private final String request; + private final User client; private static final long serialVersionUID = -7969312055630533627L; @@ -21,7 +24,10 @@ public class ContactsRequest implements Event { * corresponding clients as contacts from the server. * @since Envoy Common v0.2-alpha */ - public ContactsRequest(String request) { this.request = request; } + public ContactsRequest(String request, User client) { + this.request = request; + this.client = client; + } /** * @return the request string @@ -29,4 +35,9 @@ public class ContactsRequest implements Event { */ @Override public String get() { return request; } + + /** + * @return returns the client, which sent this request + */ + public User getClient() { return client; } } From d497553fcaa461f1ba1db692295055a14cb84925 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 10 Feb 2020 21:44:20 +0100 Subject: [PATCH 4/6] Small refactoring --- src/main/java/envoy/data/Contacts.java | 9 +-- .../java/envoy/event/ContactOperation.java | 50 -------------- .../envoy/event/ContactOperationEvent.java | 69 +++++++++++++++++++ .../envoy/event/ContactSearchRequest.java | 33 +++++++++ .../java/envoy/event/ContactsRequest.java | 43 ------------ 5 files changed, 105 insertions(+), 99 deletions(-) delete mode 100644 src/main/java/envoy/event/ContactOperation.java create mode 100644 src/main/java/envoy/event/ContactOperationEvent.java create mode 100644 src/main/java/envoy/event/ContactSearchRequest.java delete mode 100644 src/main/java/envoy/event/ContactsRequest.java diff --git a/src/main/java/envoy/data/Contacts.java b/src/main/java/envoy/data/Contacts.java index 12846ac..c86c1fc 100644 --- a/src/main/java/envoy/data/Contacts.java +++ b/src/main/java/envoy/data/Contacts.java @@ -7,26 +7,23 @@ import java.util.List; * Project: envoy-common
* File: Contacts.java
* Created: 02.01.2020
- * + * * @author Kai S. K. Engelbart * @since Envoy Common v0.2-alpha */ public class Contacts implements Serializable { - private final List contacts; + private final List contacts; private static final long serialVersionUID = 136970804968152871L; /** * Creates an instance of {@link Contacts}. * - * @param userId the ID of the user this contacts belong to * @param contacts the contact list * @since Envoy Common v0.2-alpha */ - public Contacts(List contacts) { - this.contacts = contacts; - } + public Contacts(List contacts) { this.contacts = contacts; } @Override public String toString() { return String.format("Contacts[%s]", contacts); } diff --git a/src/main/java/envoy/event/ContactOperation.java b/src/main/java/envoy/event/ContactOperation.java deleted file mode 100644 index 1a5a108..0000000 --- a/src/main/java/envoy/event/ContactOperation.java +++ /dev/null @@ -1,50 +0,0 @@ -package envoy.event; - -import envoy.data.User; - -/** - * Project: envoy-common
- * File: ContactOperation.java
- * Created: 05.02.2020
- * - * @author Maximilian Käfer - * @since Envoy Common v0.2-alpha - */ -public class ContactOperation implements Event { - - public enum Operation { - ADD, REMOVE; - } - - private final User contact; - private final Operation operationType; - - private static final long serialVersionUID = -1166652868189511553L; - - /** - * Initializes a {@link ContacsOperation}. - * - * @param operationType the {@link Operation} which should be executed on the - * {@link contact}. - * @since Envoy Common v0.2-alpha - */ - public ContactOperation(User contact, Operation operationType) { - this.contact = contact; - this.operationType = operationType; - } - - /** - * @return the {@link User}, which should be added/removed from the contacts - * list. - * @since Envoy Common v0.2-alpha - */ - @Override - public User get() { return contact; } - - /** - * @return the {@link Operation}, which should be executed with the contact, - * this event corresponds with. - * @since Envoy Common v0.2-alpha - */ - public Operation getOperationType() { return operationType; } -} diff --git a/src/main/java/envoy/event/ContactOperationEvent.java b/src/main/java/envoy/event/ContactOperationEvent.java new file mode 100644 index 0000000..643e04b --- /dev/null +++ b/src/main/java/envoy/event/ContactOperationEvent.java @@ -0,0 +1,69 @@ +package envoy.event; + +import envoy.data.User; + +/** + * Signifies the modification of a contact list.
+ *
+ * Project: envoy-common
+ * File: ContactOperationEvent.java
+ * Created: 05.02.2020
+ * + * @author Maximilian Käfer + * @since Envoy Common v0.2-alpha + */ +public class ContactOperationEvent implements 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 { + + /** + * Adds a user to the contact list. + */ + ADD, + + /** + * Removes a user from the contact list. + */ + REMOVE; + } + + private final User contact; + private final Operation operationType; + + private static final long serialVersionUID = -1166652868189511553L; + + /** + * Initializes a {@link ContactOperationEvent}. + * + * @param contact the user on which the operation is performed + * @param operationType the type of operation to perform + * @since Envoy Common v0.2-alpha + */ + public ContactOperationEvent(User contact, Operation operationType) { + this.contact = contact; + this.operationType = operationType; + } + + /** + * @return the user to perform an operation on + * @since Envoy Common v0.2-alpha + */ + @Override + public User get() { return contact; } + + /** + * @return the type of operation to perform + * @since Envoy Common v0.2-alpha + */ + public Operation getOperationType() { return operationType; } +} diff --git a/src/main/java/envoy/event/ContactSearchRequest.java b/src/main/java/envoy/event/ContactSearchRequest.java new file mode 100644 index 0000000..701c1dc --- /dev/null +++ b/src/main/java/envoy/event/ContactSearchRequest.java @@ -0,0 +1,33 @@ +package envoy.event; + +/** + * Requests a contact search from the server.
+ *
+ * Project: envoy-common
+ * File: ContactSearchRequest.java
+ * Created: 05.02.2020
+ * + * @author Maximilian Käfer + * @since Envoy Common v0.2-alpha + */ +public class ContactSearchRequest implements Event { + + private final String searchPhrase; + + private static final long serialVersionUID = -7969312055630533627L; + + /** + * Initializes a {@link ContactSearchRequest}. + * + * @param searchPhrase the search phrase to use in the contact search + * @since Envoy Common v0.2-alpha + */ + public ContactSearchRequest(String searchPhrase) { this.searchPhrase = searchPhrase; } + + /** + * @return the search phrase + * @since Envoy Common v0.2-alpha + */ + @Override + public String get() { return searchPhrase; } +} diff --git a/src/main/java/envoy/event/ContactsRequest.java b/src/main/java/envoy/event/ContactsRequest.java deleted file mode 100644 index 00d593d..0000000 --- a/src/main/java/envoy/event/ContactsRequest.java +++ /dev/null @@ -1,43 +0,0 @@ -package envoy.event; - -import envoy.data.User; - -/** - * Project: envoy-common
- * File: ContactsRequest.java
- * Created: 05.02.2020
- * - * @author Maximilian Käfer - * @since Envoy Common v0.2-alpha - */ -public class ContactsRequest implements Event { - - private final String request; - private final User client; - - private static final long serialVersionUID = -7969312055630533627L; - - /** - * Initializes a {@link ContactsRequest}. - * - * @param request the string, which is entered by the client to request - * corresponding clients as contacts from the server. - * @since Envoy Common v0.2-alpha - */ - public ContactsRequest(String request, User client) { - this.request = request; - this.client = client; - } - - /** - * @return the request string - * @since Envoy Common v0.2-alpha - */ - @Override - public String get() { return request; } - - /** - * @return returns the client, which sent this request - */ - public User getClient() { return client; } -} From fe490c0001c7661dddd1b3d6b1b3d2429f419719 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 11 Feb 2020 16:53:41 +0100 Subject: [PATCH 5/6] Made Event an abstract class, added ContactSearchResult --- .../envoy/event/ContactOperationEvent.java | 16 +++------- .../envoy/event/ContactSearchRequest.java | 13 ++------ .../java/envoy/event/ContactSearchResult.java | 28 +++++++++++++++++ src/main/java/envoy/event/Event.java | 30 +++++++++++++++++-- .../envoy/event/HandshakeRejectionEvent.java | 15 ++-------- .../java/envoy/event/IdGeneratorRequest.java | 2 +- .../envoy/event/MessageStatusChangeEvent.java | 14 ++------- .../envoy/event/UserStatusChangeEvent.java | 18 ++++------- 8 files changed, 74 insertions(+), 62 deletions(-) create mode 100644 src/main/java/envoy/event/ContactSearchResult.java diff --git a/src/main/java/envoy/event/ContactOperationEvent.java b/src/main/java/envoy/event/ContactOperationEvent.java index 643e04b..7410e65 100644 --- a/src/main/java/envoy/event/ContactOperationEvent.java +++ b/src/main/java/envoy/event/ContactOperationEvent.java @@ -12,7 +12,7 @@ import envoy.data.User; * @author Maximilian Käfer * @since Envoy Common v0.2-alpha */ -public class ContactOperationEvent implements Event { +public class ContactOperationEvent extends Event { /** * Specifies the operation performed on a contact list.
@@ -37,8 +37,7 @@ public class ContactOperationEvent implements Event { REMOVE; } - private final User contact; - private final Operation operationType; + private final Operation operationType; private static final long serialVersionUID = -1166652868189511553L; @@ -50,17 +49,10 @@ public class ContactOperationEvent implements Event { * @since Envoy Common v0.2-alpha */ public ContactOperationEvent(User contact, Operation operationType) { - this.contact = contact; - this.operationType = operationType; + super(contact); + this.operationType = operationType; } - /** - * @return the user to perform an operation on - * @since Envoy Common v0.2-alpha - */ - @Override - public User get() { return contact; } - /** * @return the type of operation to perform * @since Envoy Common v0.2-alpha diff --git a/src/main/java/envoy/event/ContactSearchRequest.java b/src/main/java/envoy/event/ContactSearchRequest.java index 701c1dc..4076265 100644 --- a/src/main/java/envoy/event/ContactSearchRequest.java +++ b/src/main/java/envoy/event/ContactSearchRequest.java @@ -10,9 +10,7 @@ package envoy.event; * @author Maximilian Käfer * @since Envoy Common v0.2-alpha */ -public class ContactSearchRequest implements Event { - - private final String searchPhrase; +public class ContactSearchRequest extends Event { private static final long serialVersionUID = -7969312055630533627L; @@ -22,12 +20,5 @@ public class ContactSearchRequest implements Event { * @param searchPhrase the search phrase to use in the contact search * @since Envoy Common v0.2-alpha */ - public ContactSearchRequest(String searchPhrase) { this.searchPhrase = searchPhrase; } - - /** - * @return the search phrase - * @since Envoy Common v0.2-alpha - */ - @Override - public String get() { return searchPhrase; } + public ContactSearchRequest(String searchPhrase) { super(searchPhrase); } } diff --git a/src/main/java/envoy/event/ContactSearchResult.java b/src/main/java/envoy/event/ContactSearchResult.java new file mode 100644 index 0000000..03fd681 --- /dev/null +++ b/src/main/java/envoy/event/ContactSearchResult.java @@ -0,0 +1,28 @@ +package envoy.event; + +import java.util.List; + +import envoy.data.User; + +/** + * Contains a list of {@link User}s for which a search was performed.
+ *
+ * Project: envoy-common
+ * File: ContactSearchResult.java
+ * Created: 11 Feb 2020
+ * + * @author Kai S. K. Engelbart + * @since Envoy Common v0.2-alpha + */ +public class ContactSearchResult extends Event> { + + private static final long serialVersionUID = -8934154116102031561L; + + /** + * Creates an instance of {@link ContactSearchResult}. + * + * @param users the users found during the search + */ + public ContactSearchResult(List users) { super(users); } + +} diff --git a/src/main/java/envoy/event/Event.java b/src/main/java/envoy/event/Event.java index 3be9d9c..064ceaa 100644 --- a/src/main/java/envoy/event/Event.java +++ b/src/main/java/envoy/event/Event.java @@ -11,10 +11,36 @@ import java.io.Serializable; * @param the type of the Event * @since Envoy v0.2-alpha */ -public interface Event extends Serializable { +public abstract class Event implements Serializable { + + protected final T value; + + private static final long serialVersionUID = 4673659457380399167L; + + protected Event(T value) { this.value = value; } /** * @return the data associated with this event */ - default T get() { return null; } + public T get() { return value; } + + @Override + public String toString() { return String.format("%s[value=%s]%n", this.getClass().getSimpleName(), value); } + + /** + * Serves as a super class for events that do not carry a value.
+ *
+ * Project: envoy-common
+ * File: Event.java
+ * Created: 11 Feb 2020
+ * + * @author Kai S. K. Engelbart + * @since Envoy Common v0.2-alpha + */ + public static abstract class Valueless extends Event { + + private static final long serialVersionUID = -9019362144094097997L; + + protected Valueless() { super(null); } + } } diff --git a/src/main/java/envoy/event/HandshakeRejectionEvent.java b/src/main/java/envoy/event/HandshakeRejectionEvent.java index aca655e..8c71ffa 100644 --- a/src/main/java/envoy/event/HandshakeRejectionEvent.java +++ b/src/main/java/envoy/event/HandshakeRejectionEvent.java @@ -11,9 +11,7 @@ package envoy.event; * @author Kai S. K. Engelbart * @since Envoy Common v0.3-alpha */ -public class HandshakeRejectionEvent implements Event { - - private final String reason; +public class HandshakeRejectionEvent extends Event { private static final long serialVersionUID = -8723270093452609197L; @@ -22,7 +20,7 @@ public class HandshakeRejectionEvent implements Event { * * @since Envoy Common v0.3-alpha */ - public HandshakeRejectionEvent() { this(""); } + public HandshakeRejectionEvent() { super(""); } /** * Creates an instance of {@link HandshakeRejectionEvent}. @@ -30,12 +28,5 @@ public class HandshakeRejectionEvent implements Event { * @param reason the reason why the handshake was rejected * @since Envoy Common v0.3-alpha */ - public HandshakeRejectionEvent(String reason) { this.reason = reason; } - - /** - * @return the reason why the handshake was rejected - * @since Envoy Common v0.3-alpha - */ - @Override - public String get() { return reason; } + public HandshakeRejectionEvent(String reason) { super(""); } } diff --git a/src/main/java/envoy/event/IdGeneratorRequest.java b/src/main/java/envoy/event/IdGeneratorRequest.java index 885f40d..fe7871d 100644 --- a/src/main/java/envoy/event/IdGeneratorRequest.java +++ b/src/main/java/envoy/event/IdGeneratorRequest.java @@ -11,7 +11,7 @@ package envoy.event; * @author Kai S. K. Engelbart * @since Envoy Common v0.3-alpha */ -public class IdGeneratorRequest implements Event { +public class IdGeneratorRequest extends Event.Valueless { private static final long serialVersionUID = 1431107413883364583L; } diff --git a/src/main/java/envoy/event/MessageStatusChangeEvent.java b/src/main/java/envoy/event/MessageStatusChangeEvent.java index a696c36..d7e7e4a 100644 --- a/src/main/java/envoy/event/MessageStatusChangeEvent.java +++ b/src/main/java/envoy/event/MessageStatusChangeEvent.java @@ -12,10 +12,9 @@ import envoy.data.Message; * @author Kai S. K. Engelbart * @since Envoy Common v0.2-alpha */ -public class MessageStatusChangeEvent implements Event { +public class MessageStatusChangeEvent extends Event { private final long id; - private final Message.MessageStatus status; private final Date date; private static final long serialVersionUID = 4566145392192761313L; @@ -30,8 +29,8 @@ public class MessageStatusChangeEvent implements Event { * @since Envoy Common v0.2-alpha */ public MessageStatusChangeEvent(long id, Message.MessageStatus status, Date date) { + super(status); this.id = id; - this.status = status; this.date = date; } @@ -43,13 +42,6 @@ public class MessageStatusChangeEvent implements Event { */ public MessageStatusChangeEvent(Message message) { this(message.getId(), message.getStatus(), new Date()); } - /** - * @return the status of the {@link Message} this event is related to - * @since Envoy Common v0.2-alpha - */ - @Override - public Message.MessageStatus get() { return status; } - /** * @return the ID of the {@link Message} this event is related to * @since Envoy Common v0.2-alpha @@ -63,5 +55,5 @@ public class MessageStatusChangeEvent implements Event { public Date getDate() { return date; } @Override - public String toString() { return String.format("MessageStatusChangeEvent[id=%d,status=%s,date=%s]", id, status, date); } + public String toString() { return String.format("MessageStatusChangeEvent[id=%d,status=%s,date=%s]", id, value, date); } } diff --git a/src/main/java/envoy/event/UserStatusChangeEvent.java b/src/main/java/envoy/event/UserStatusChangeEvent.java index 1e47952..ed6e79f 100644 --- a/src/main/java/envoy/event/UserStatusChangeEvent.java +++ b/src/main/java/envoy/event/UserStatusChangeEvent.java @@ -11,10 +11,9 @@ import envoy.data.User.UserStatus; * @author Leon Hofmeister * @since Envoy Common v0.2-alpha */ -public class UserStatusChangeEvent implements Event { +public class UserStatusChangeEvent extends Event { - private final long id; - private final User.UserStatus status; + private final long id; private static final long serialVersionUID = 4566145392192761313L; @@ -27,8 +26,8 @@ public class UserStatusChangeEvent implements Event { * @since Envoy Common v0.2-alpha */ public UserStatusChangeEvent(long id, User.UserStatus status) { - this.id = id; - this.status = status; + super(status); + this.id = id; } /** @@ -39,13 +38,6 @@ public class UserStatusChangeEvent implements Event { */ public UserStatusChangeEvent(User user) { this(user.getId(), user.getStatus()); } - /** - * @return the status of the {@link User} this event is related to - * @since Envoy Common v0.2-alpha - */ - @Override - public User.UserStatus get() { return status; } - /** * @return the ID of the {@link User} this event is related to * @since Envoy Common v0.2-alpha @@ -53,5 +45,5 @@ public class UserStatusChangeEvent implements Event { public long getId() { return id; } @Override - public String toString() { return String.format("UserStatusChangeEvent[id=%d,status=%s]", id, status); } + public String toString() { return String.format("UserStatusChangeEvent[id=%d,status=%s]", id, value); } } From 45d77ef9b96a9a0292aa931fa5fe780decfd1838 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 12 Feb 2020 05:58:40 +0100 Subject: [PATCH 6/6] Clean-up, improved generic type usage in EventBus Fixes #13 --- .../java/envoy/event/ContactSearchResult.java | 2 +- src/main/java/envoy/event/Event.java | 2 +- src/main/java/envoy/event/EventBus.java | 49 +++++++++---------- src/main/java/envoy/event/EventHandler.java | 18 ------- .../envoy/event/HandshakeRejectionEvent.java | 2 +- 5 files changed, 26 insertions(+), 47 deletions(-) delete mode 100644 src/main/java/envoy/event/EventHandler.java diff --git a/src/main/java/envoy/event/ContactSearchResult.java b/src/main/java/envoy/event/ContactSearchResult.java index 03fd681..929ee30 100644 --- a/src/main/java/envoy/event/ContactSearchResult.java +++ b/src/main/java/envoy/event/ContactSearchResult.java @@ -22,7 +22,7 @@ public class ContactSearchResult extends Event> { * Creates an instance of {@link ContactSearchResult}. * * @param users the users found during the search + * @since Envoy Common v0.2-alpha */ public ContactSearchResult(List users) { super(users); } - } diff --git a/src/main/java/envoy/event/Event.java b/src/main/java/envoy/event/Event.java index 064ceaa..9d42bf5 100644 --- a/src/main/java/envoy/event/Event.java +++ b/src/main/java/envoy/event/Event.java @@ -25,7 +25,7 @@ public abstract class Event implements Serializable { public T get() { return value; } @Override - public String toString() { return String.format("%s[value=%s]%n", this.getClass().getSimpleName(), value); } + public String toString() { return String.format("%s[value=%s]", this.getClass().getSimpleName(), value); } /** * Serves as a super class for events that do not carry a value.
diff --git a/src/main/java/envoy/event/EventBus.java b/src/main/java/envoy/event/EventBus.java index f2e5c9f..146fb58 100644 --- a/src/main/java/envoy/event/EventBus.java +++ b/src/main/java/envoy/event/EventBus.java @@ -1,18 +1,15 @@ package envoy.event; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Consumer; /** - * This class handles events by allowing {@link EventHandler} object to register - * themselves and then be notified about certain events dispatched by the event - * bus.
+ * This class handles events by allowing event handlers to register themselves + * and then be notified about certain events dispatched by the event bus.
*
* The event bus is a singleton and can be used across the entire application to * guarantee the propagation of events.
- * + *
* Project: envoy-common
* File: EventBus.java
* Created: 04.12.2019
@@ -23,14 +20,13 @@ import java.util.Map; public class EventBus { /** - * Contains all {@link EventHandler} instances registered at this - * {@link EventBus} as values mapped to by their supported {@link Event} - * classes. + * Contains all event handler instances registered at this event bus as values + * mapped to by their supported event classes. */ - private Map>, List> handlers = new HashMap<>(); + private Map>, List>>> handlers = new HashMap<>(); /** - * The singleton instance of this {@link EventBus} that is used across the + * The singleton instance of this event bus that is used across the * entire application. */ private static EventBus eventBus = new EventBus(); @@ -43,39 +39,40 @@ public class EventBus { private EventBus() {} /** - * @return the singleton instance of the {@link EventBus} + * @return the singleton instance of the event bus * @since Envoy v0.2-alpha */ public static EventBus getInstance() { return eventBus; } /** - * Registers an {@link EventHandler} to be notified when a - * {@link Event} of a certain type is dispatched. + * Registers an event handler to be notified when an + * event of a certain type is dispatched. * - * @param eventClass the class which the {@link EventHandler} is subscribed to - * @param handler the {@link EventHandler} to register + * @param the type of event values to notify the handler about + * @param eventClass the class which the event handler is subscribing to + * @param handler the event handler to register * @since Envoy v0.2-alpha */ - public void register(Class> eventClass, EventHandler handler) { + @SuppressWarnings("unchecked") + public > void register(Class eventClass, Consumer handler) { if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>()); - handlers.get(eventClass).add(handler); + handlers.get(eventClass).add((Consumer>) handler); } /** - * Dispatches a {@link Event} to every {@link EventHandler} subscribed to it. + * Dispatches an event to every event handler subscribed to it. * * @param event the {@link Event} to dispatch * @since Envoy v0.2-alpha */ public void dispatch(Event event) { - handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.handle(event)); + handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.accept(event)); } /** - * @return a map of all {@link EventHandler} instances currently registered at - * this {@link EventBus} with the {@link Event} classes they are - * subscribed to as keys + * @return a map of all event handler instances currently registered at this + * event bus with the event classes they are subscribed to as keys * @since Envoy v0.2-alpha */ - public Map>, List> getHandlers() { return handlers; } + public Map>, List>>> getHandlers() { return handlers; } } diff --git a/src/main/java/envoy/event/EventHandler.java b/src/main/java/envoy/event/EventHandler.java deleted file mode 100644 index f842476..0000000 --- a/src/main/java/envoy/event/EventHandler.java +++ /dev/null @@ -1,18 +0,0 @@ -package envoy.event; - -/** - * Project: envoy-common
- * File: EventHandler.java
- * Created: 04.12.2019
- * - * @author Kai S. K. Engelbart - */ -public interface EventHandler { - - /** - * Consumes an event dispatched by the event bus. - * - * @param event The event dispatched by the event bus, only of supported type - */ - void handle(Event event); -} diff --git a/src/main/java/envoy/event/HandshakeRejectionEvent.java b/src/main/java/envoy/event/HandshakeRejectionEvent.java index 8c71ffa..5d58aa0 100644 --- a/src/main/java/envoy/event/HandshakeRejectionEvent.java +++ b/src/main/java/envoy/event/HandshakeRejectionEvent.java @@ -28,5 +28,5 @@ public class HandshakeRejectionEvent extends Event { * @param reason the reason why the handshake was rejected * @since Envoy Common v0.3-alpha */ - public HandshakeRejectionEvent(String reason) { super(""); } + public HandshakeRejectionEvent(String reason) { super(reason); } }