package com.example.mina.client.base; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import lombok.extern.slf4j.Slf4j; import org.springframework.util.Assert; /** * @author BenJin Yin * @date 2021/4/6 */ @Slf4j public class ExperimentManager { private final ConcurrentMap experimentFactories; private final ConcurrentMap> experiments = new ConcurrentHashMap<>(8); public ExperimentManager(ConcurrentMap experimentFactories) { this.experimentFactories = experimentFactories; } public Experiment getExperiment(String experimentId) { return experiments.getOrDefault(experimentId, null); } public void stopExperiment(String experimentId) { Experiment t = getExperiment(experimentId); if (t != null && t.isContinue()) { t.finish(); experiments.remove(experimentId); } } public T getClientFactory(String name) { return this.experimentFactories.get(name); } public Experiment getOrCreateExperiment(MatrixClient client, ExperimentOptions options) { Assert.notNull(client, "matrix connect config is null"); Assert.hasLength(client.getDeviceId(), "deviceId can not be empty"); String deviceId = client.getDeviceId(); Experiment experiment = experiments.get(options.getExperimentId()); if (experiment != null) { return experiment; } if (!experimentFactories.containsKey(deviceId)) { log.error("the matrix type is not supported by the system! matrix type is: " + deviceId); throw new RuntimeException("the matrix type is not supported by the system! matrix type is: " + deviceId); } T t = experimentFactories.get(deviceId); experiment = t.createExperiment(client, options); experiments.put(options.experimentId, experiment); return experiment; } }