cucumber.runner.EventBus Maven / Gradle / Ivy
package cucumber.runner;
import cucumber.api.event.Event;
import cucumber.api.event.EventHandler;
import cucumber.api.event.EventPublisher;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class EventBus implements EventPublisher {
private final TimeService stopWatch;
private Map, List> handlers = new HashMap, List>();
public EventBus(TimeService stopWatch) {
this.stopWatch = stopWatch;
}
public Long getTime() {
return stopWatch.time();
}
public void send(Event event) {
if (handlers.containsKey(event.getClass())) {
for (EventHandler handler : handlers.get(event.getClass())) {
//noinspection unchecked: protected by registerHandlerFor
handler.receive(event);
}
}
}
@Override
public void registerHandlerFor(Class eventType, EventHandler handler) {
if (handlers.containsKey(eventType)) {
handlers.get(eventType).add(handler);
} else {
List list = new ArrayList();
list.add(handler);
handlers.put(eventType, list);
}
}
}