package fabrik.simulator.pic16f84;

import fabrik.simulator.pic16f84.interfaces.*;

import java.util.HashMap;

public class PICComponentLocator {
    // https://en.wikipedia.org/wiki/Service_locator_pattern
    private final HashMap<Class<? extends PICComponentInterface>, PICComponentInterface> componentCatalogue;

    public PICComponentLocator() {
        super();
        this.componentCatalogue = new HashMap<>();
    }

    public void registerComponent (Class<? extends PICComponentInterface> componentClass, PICComponentInterface component) {
        this.componentCatalogue.put(componentClass, component);
    }

    public <T extends PICComponentInterface> T getComponent(Class<T> componentClass) {
        T component = (T) this.componentCatalogue.get(componentClass);
        return component;
    }

    public void initAll() {
        for (PICComponentInterface component : componentCatalogue.values()) component.initialize(this);
    }

}
