package com.imdroid.sideslope.executor;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
public class BizExecutors {

    private static HashMap<Class<?>, Object> executors = new HashMap<>();

    private static List<Executor<?, ?>> executorList;

    public BizExecutors(List<Executor<?, ?>> executorList) {
        this.executorList = executorList;
        for (Executor<?, ?> executor : this.executorList) {
            System.out.println("executor type:" + executor.getMessageType().getName());
            executors.put(executor.getMessageType(), executor);
        }
    }


    public static <Q, R> R execute(Q query) {
        Executor<Q, R> executor = (Executor<Q, R>)(executors.get(query.getClass()));
        return executor.execute(query);
    }
}
