diff --git a/src/main/java/envoy/data/Contacts.java b/src/main/java/envoy/data/Contacts.java
index bd19102..c86c1fc 100644
--- a/src/main/java/envoy/data/Contacts.java
+++ b/src/main/java/envoy/data/Contacts.java
@@ -7,38 +7,27 @@ 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 long userId;
- 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(long userId, List contacts) {
- this.userId = userId;
- this.contacts = contacts;
- }
+ 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
diff --git a/src/main/java/envoy/event/ContactOperationEvent.java b/src/main/java/envoy/event/ContactOperationEvent.java
new file mode 100644
index 0000000..7410e65
--- /dev/null
+++ b/src/main/java/envoy/event/ContactOperationEvent.java
@@ -0,0 +1,61 @@
+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 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 {
+
+ /**
+ * 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 = -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) {
+ super(contact);
+ this.operationType = operationType;
+ }
+
+ /**
+ * @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..4076265
--- /dev/null
+++ b/src/main/java/envoy/event/ContactSearchRequest.java
@@ -0,0 +1,24 @@
+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 extends Event {
+
+ 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) { 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..929ee30
--- /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
+ * @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 3be9d9c..9d42bf5 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]", 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/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 extends Event>> 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 66bbbc8..8304a11 100644
--- a/src/main/java/envoy/event/HandshakeRejectionEvent.java
+++ b/src/main/java/envoy/event/HandshakeRejectionEvent.java
@@ -13,7 +13,7 @@ import envoy.data.LoginCredentials;
* @author Kai S. K. Engelbart
* @since Envoy Common v0.3-alpha
*/
-public class HandshakeRejectionEvent implements Event {
+public class HandshakeRejectionEvent extends Event {
/**
* Select this value if a given password hash was incorrect
@@ -53,7 +53,7 @@ public class HandshakeRejectionEvent implements Event {
*
* @since Envoy Common v0.3-alpha
*/
- public HandshakeRejectionEvent() { this(""); }
+ public HandshakeRejectionEvent() { super(""); }
/**
* Creates an instance of {@link HandshakeRejectionEvent}.
@@ -61,12 +61,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(reason); }
}
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); }
}