package com.base.biz.model.entity;

import com.base.biz.model.KeyValueDTO;
import com.base.biz.model.enums.TypeList;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class TypeListBiz {

    public static <E,V> List<KeyValueDTO<E,V>> getTypeList(TypeList[] typeList) throws Exception {
        List<KeyValueDTO<E,V>> list = new ArrayList<>();
        for (TypeList type:typeList){
            KeyValueDTO<E,V> keyValueDTO = new KeyValueDTO<>();
            Class<?> clazz = type.getClass();
            Method getKey = clazz.getMethod("getKey");
            Method getValue = clazz.getMethod("getValue");
            E key = (E) getKey.invoke(type);
            V value = (V) getValue.invoke(type);
            keyValueDTO.setKey(key);
            keyValueDTO.setValue(value);
            list.add(keyValueDTO);
        }
        return list;
    }
}
