From fe490c0001c7661dddd1b3d6b1b3d2429f419719 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 11 Feb 2020 16:53:41 +0100 Subject: [PATCH] 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); } }