Moved event classes to envoy-common
This commit is contained in:
parent
cf05f8e1d3
commit
2ce0a09af8
@ -41,7 +41,7 @@ public class TextMessage extends Message {
|
|||||||
getId(),
|
getId(),
|
||||||
getSender(),
|
getSender(),
|
||||||
getRecipient(),
|
getRecipient(),
|
||||||
new SimpleDateFormat("dd.MM.yyyy hh:mm:ss").format(getDate()),
|
new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(getDate()),
|
||||||
getStatus(),
|
getStatus(),
|
||||||
content);
|
content);
|
||||||
}
|
}
|
||||||
|
19
src/main/java/envoy/event/Event.java
Normal file
19
src/main/java/envoy/event/Event.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package envoy.event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project: <strong>envoy-common</strong><br>
|
||||||
|
* File: <strong>Event.java</strong><br>
|
||||||
|
* Created: <strong>04.12.2019</strong><br>
|
||||||
|
*
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
|
* @param <T> the type of the Event
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
|
public interface Event<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the data associated with this event
|
||||||
|
*/
|
||||||
|
T get();
|
||||||
|
}
|
||||||
|
|
81
src/main/java/envoy/event/EventBus.java
Normal file
81
src/main/java/envoy/event/EventBus.java
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package envoy.event;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class handles events by allowing {@link EventHandler} object to register
|
||||||
|
* themselves and then be notified about certain events dispatched by the event
|
||||||
|
* bus.<br>
|
||||||
|
* <br>
|
||||||
|
* The event bus is a singleton and can be used across the entire application to
|
||||||
|
* guarantee the propagation of events.<br>
|
||||||
|
*
|
||||||
|
* Project: <strong>envoy-common</strong><br>
|
||||||
|
* File: <strong>EventBus.java</strong><br>
|
||||||
|
* Created: <strong>04.12.2019</strong><br>
|
||||||
|
*
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
|
public class EventBus {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains all {@link EventHandler} instances registered at this
|
||||||
|
* {@link EventBus} as values mapped to by their supported {@link Event}
|
||||||
|
* classes.
|
||||||
|
*/
|
||||||
|
private Map<Class<? extends Event<?>>, List<EventHandler>> handlers = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The singleton instance of this {@link EventBus} that is used across the
|
||||||
|
* entire application.
|
||||||
|
*/
|
||||||
|
private static EventBus eventBus = new EventBus();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This constructor is not accessible from outside this class because a
|
||||||
|
* singleton instance of it is provided by the {@link EventBus#getInstance()}
|
||||||
|
* method.
|
||||||
|
*/
|
||||||
|
private EventBus() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the singleton instance of the {@link EventBus}
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* @param eventClass the class which the {@link EventHandler} is subscribed to
|
||||||
|
* @param handler the {@link EventHandler} to register
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
|
public void register(Class<? extends Event<?>> eventClass, EventHandler handler) {
|
||||||
|
if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>());
|
||||||
|
handlers.get(eventClass).add(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispatches a {@link Event} to every {@link EventHandler} 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
|
public Map<Class<? extends Event<?>>, List<EventHandler>> getHandlers() { return handlers; }
|
||||||
|
}
|
18
src/main/java/envoy/event/EventHandler.java
Normal file
18
src/main/java/envoy/event/EventHandler.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package envoy.event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project: <strong>envoy-common</strong><br>
|
||||||
|
* File: <strong>EventHandler.java</strong><br>
|
||||||
|
* Created: <strong>04.12.2019</strong><br>
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
}
|
27
src/main/java/envoy/event/MessageEvent.java
Normal file
27
src/main/java/envoy/event/MessageEvent.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package envoy.event;
|
||||||
|
|
||||||
|
import envoy.data.Message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project: <strong>envoy-common</strong><br>
|
||||||
|
* File: <strong>MessageCreationEvent.java</strong><br>
|
||||||
|
* Created: <strong>4 Dec 2019</strong><br>
|
||||||
|
*
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
|
*/
|
||||||
|
public class MessageEvent implements Event<Message> {
|
||||||
|
|
||||||
|
protected final Message message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes a {@link MessageEvent} conveying information about a
|
||||||
|
* {@link Message} object.
|
||||||
|
*
|
||||||
|
* @param message the {@link Message} object to attach to this event
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
|
public MessageEvent(Message message) { this.message = message; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Message get() { return message; }
|
||||||
|
}
|
Reference in New Issue
Block a user