AbstractExperimentFactory.java 1.3 KB
package com.example.mina.client.base;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class AbstractExperimentFactory<T extends Experiment<MatrixClient, ExperimentOptions>> {

    abstract public boolean isSupport(ExperimentOptions options);

    protected abstract Class<T> getClazz();

    protected abstract ExperimentType getExperimentType();

    public ExperimentProgressListner getListner() {
        //TODO 生成任务进度更新监听器
        return null;
    }

    public T createExperiment(MatrixClient client, ExperimentOptions options) {
        if (isSupport(options)) {
            log.error("the experiment is not supported by the matrix! matrix id: " + client.getDeviceId());
            throw new RuntimeException("the experiment is not supported by the matrix! matrix id: " + client.getDeviceId());
        }
        try {
            T t = getClazz().getDeclaredConstructor().newInstance();
            t.setClient(client);
            t.setOptions(options);
            t.setProgressListner(getListner());
            return t;
        } catch (Exception e) {
            log.error("create a experiment task is failed, matrix id: " + client.getDeviceId());
            throw new RuntimeException("create a experiment task is failed, matrix id: " + client.getDeviceId());
        }
    }

}