package de.accso.flexinale.common.infrastructure.eventbus;

import de.accso.flexinale.common.api.eventbus.EventBus;
import de.accso.flexinale.common.api.eventbus.EventBusFactory;
import de.accso.flexinale.common.api.event.Event;

import java.util.HashMap;
import java.util.Map;

public class InMemorySyncEventBusFactory implements EventBusFactory {
    protected final Map<Class<? extends Event>, EventBus<? extends Event>> eventTypeToEventBusMap = new HashMap<>();

    @SuppressWarnings("unchecked")
    @Override
    public <E extends Event> EventBus<E> createOrGetEventBusFor(final Class<E> eventType) {
        EventBus<? extends Event> eventBus = eventTypeToEventBusMap.get(eventType);
        if (eventBus == null) {
            eventBus = new InMemorySyncEventBus<>(eventType);
            eventTypeToEventBusMap.put(eventType, eventBus);
        }
        return (EventBus<E>) eventBus;
    }
}
