Commit 883069a7 authored by Riegel's avatar Riegel
Browse files

Added basis for Eventbus implementation

parent 216ab18a
Pipeline #9851 passed with stage
in 1 minute and 15 seconds
package de.hft.stuttgart.citydoctor2.eventListeners;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import de.hft.stuttgart.citydoctor2.eventbus.CDEvent;
import de.hft.stuttgart.citydoctor2.eventbus.CDEventId;
import de.hft.stuttgart.citydoctor2.eventbus.EventBusListener;
public final class EventBusListenerTemplate extends EventBusListener{
private static final Set<CDEventId> registeredEvents;
static {
Set<CDEventId> events = new HashSet<>();
// signals.add(CDEventId);
// ...
registeredEvents = events;
}
public EventBusListenerTemplate() {
super(registeredEvents);
throw new IllegalStateException("Called Constructor of EventListenerTemplate");
}
@Override
protected void handleEvent(CDEvent e) {
// Do Event handling logic here
// Oerload to specify different behaviour for each respective Event
}
}
package de.hft.stuttgart.citydoctor2.eventbus;
/**
* Abstract base for implementation of new Events handled by the eventbus
*/
public interface CDEvent {
public Object getData();
public String getMessage();
public String toString();
public CDEventId getEventId();
}
package de.hft.stuttgart.citydoctor2.eventbus;
import java.io.Serializable;
public class CDEventId implements Serializable{
/**
*
*/
private static final long serialVersionUID = -328991495763035712L;
public static final CDEventId CITYMODEL_LOADED = new CDEventId("CITYMODEL_LOADED");
public static final CDEventId CITYMODEL_VALIDATION_STARTED = new CDEventId("CITYMODEL_VALIDATION_STARTED");
public static final CDEventId CITYMODEL_VALIDATION_FINISHED = new CDEventId("CITYMODEL_VALIDATION_FINISHED");
public static final CDEventId GEOMETRY_VALIDATION_STARTED = new CDEventId("GEOMETRY_VALIDATION_STARTED");
public static final CDEventId GEOMETRY_VALIDATION_FINISHED = new CDEventId("GEOMETRY_VALIDATION_END");
public static final CDEventId CHECK_STARTED = new CDEventId("CHECK_STARTED");
public static final CDEventId CHECK_FINISHED = new CDEventId("CHECK_FINISHED");
private String name;
public CDEventId(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CDEventId other = (CDEventId) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return name;
}
}
package de.hft.stuttgart.citydoctor2.eventbus;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public final class EventBus{
private static EventBus INSTANCE;
private Set<EventBusListener> registeredListeners = new HashSet<>();
private EventBus() {
}
public static EventBus getInstance() {
if (INSTANCE == null) {
INSTANCE = new EventBus();
}
return INSTANCE;
}
public void emit(CDEvent e) {
registeredListeners.forEach(x -> x.receive(e));
}
public boolean tryRegisterEventListener(EventBusListener listener) {
return registeredListeners.add(listener);
}
public boolean tryUnregisterEventListener(EventBusListener listener) {
return registeredListeners.remove(listener);
}
}
package de.hft.stuttgart.citydoctor2.eventbus;
import java.util.Collections;
import java.util.Set;
public abstract class EventBusListener {
protected final Set<CDEventId> listensTo;
protected EventBusListener(Set<CDEventId> events) {
listensTo = Collections.unmodifiableSet(events);
}
public void receive(CDEvent e) {
if (listensTo.contains(e.getEventId())) {
handleEvent(e);
}
}
protected abstract void handleEvent(CDEvent e);
public Set<CDEventId> getListensTo(){
return listensTo;
}
}
package de.hft.stuttgart.citydoctor2.events;
import org.citygml4j.core.model.core.CityModel;
import de.hft.stuttgart.citydoctor2.datastructure.GmlId;
import de.hft.stuttgart.citydoctor2.eventbus.CDEvent;
import de.hft.stuttgart.citydoctor2.eventbus.CDEventId;
public final class CItyModelLoadedEvent implements CDEvent{
private CityModel model;
private CDEventId id = CDEventId.CITYMODEL_LOADED;
public CItyModelLoadedEvent(CityModel model) {
this.model = model;
};
@Override
public CityModel getData() {
return model;
}
@Override
public String getMessage() {
String msg = "Loaded %s";
msg = String.format(msg, model);
return msg;
}
@Override
public String toString() {
String representation = "[Event: %s, Data: %s]";
representation = String.format(representation, this.toString(), model.toString());
return representation;
}
@Override
public CDEventId getEventId() {
return id;
}
}
package de.hft.stuttgart.citydoctor2.events;
import de.hft.stuttgart.citydoctor2.datastructure.GmlId;
import de.hft.stuttgart.citydoctor2.eventbus.CDEvent;
import de.hft.stuttgart.citydoctor2.eventbus.CDEventId;
public final class EventTemplate implements CDEvent{
private Object example;
public EventTemplate() {
throw new IllegalStateException("Called constructor of Event-template");
};
@Override
public Object getData() {
return example;
}
@Override
public String getMessage() {
return "Message for a GUI element";
}
@Override
public String toString() {
return "String representation of this Event";
}
@Override
public CDEventId getEventId() {
return null;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment