131 lines
2.8 KiB
Java
131 lines
2.8 KiB
Java
package org.gcube.application.cms.plugins.events;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* The Class EventManager.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
*
|
|
* Dec 11, 2023
|
|
*/
|
|
public class EventManager implements EventManagerInterface<ItemObservable> {
|
|
|
|
private HashMap<Event, List<EventListener<ItemObservable>>> listeners = new HashMap<Event, List<EventListener<ItemObservable>>>();
|
|
|
|
/**
|
|
* The Class Event.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
*
|
|
* Nov 29, 2023
|
|
*/
|
|
public static enum Event {
|
|
|
|
PROJECT_CREATED("PROJECT_CREATED"),
|
|
PROJECT_UPDATED("PROJECT_UPDATED"),
|
|
PROJECT_DELETED("PROJECT_DELETED"),
|
|
LIFECYCLE_STEP_PERFORMED("LIFECYCLE_STEP_PERFORMED");
|
|
|
|
String name;
|
|
|
|
/**
|
|
* Instantiates a new event.
|
|
*
|
|
* @param name the name
|
|
*/
|
|
Event(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
/**
|
|
* Gets the name.
|
|
*
|
|
* @return the name
|
|
*/
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
}
|
|
|
|
// private instance, so that it can be
|
|
// accessed by only by getInstance() method
|
|
private static EventManager instance;
|
|
|
|
/**
|
|
* Instantiates a new event manager.
|
|
*/
|
|
private EventManager() {
|
|
// private constructor
|
|
}
|
|
|
|
/**
|
|
* Gets the single instance of EventManager.
|
|
*
|
|
* @return single instance of EventManager
|
|
*/
|
|
public static EventManager getInstance() {
|
|
if (instance == null) {
|
|
// synchronized block to remove overhead
|
|
synchronized (EventManager.class) {
|
|
if (instance == null) {
|
|
// if instance is null, initialize
|
|
instance = new EventManager();
|
|
}
|
|
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
/**
|
|
* Subscribe.
|
|
*
|
|
* @param eventType the event type
|
|
* @param listener the listener
|
|
*/
|
|
public void subscribe(Event eventType, EventListener<ItemObservable> listener) {
|
|
List<EventListener<ItemObservable>> list = listeners.get(eventType);
|
|
if (list == null)
|
|
list = new ArrayList<EventListener<ItemObservable>>();
|
|
|
|
list.add(listener);
|
|
listeners.put(eventType, list);
|
|
}
|
|
|
|
/**
|
|
* Unsubscribe.
|
|
*
|
|
* @param eventType the event type
|
|
* @param listener the listener
|
|
*/
|
|
public void unsubscribe(Event eventType, EventListener<ItemObservable> listener) {
|
|
List<EventListener<ItemObservable>> list = listeners.get(eventType);
|
|
if (list == null)
|
|
return;
|
|
|
|
list.remove(listener);
|
|
listeners.put(eventType, list);
|
|
}
|
|
|
|
/**
|
|
* Notify.
|
|
*
|
|
* @param eventType the event type
|
|
* @param item the item
|
|
*/
|
|
public void notify(Event eventType, ItemObservable item) {
|
|
for (EventManager.Event event : listeners.keySet()) {
|
|
if (event.equals(eventType)) {
|
|
for (EventListener<ItemObservable> eventListner : listeners.get(eventType)) {
|
|
eventListner.updated(item);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|