ExperimentFactory.java
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.mina.client.base;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.Map;
@Slf4j
public abstract class ExperimentFactory < T extends Experiment> {
private Map<String, T> experiments;
public ExperimentFactory() {
experiments = CollectionUtils.newHashMap(10);
}
public T getExperiment(String matrixId) {
return experiments.getOrDefault(matrixId, null);
}
public void stopExperiment(String matrixId) {
T t = getExperiment(matrixId);
if( t != null && t.isContinue()){
t.finish();
experiments.remove(matrixId);
}
}
abstract public boolean isSupport();
abstract Class<T> getClazz();
public ExperimentProgressListner getListner() {
return null; //TODO 生成任务进度更新监听器
}
public T createExperiment(MatrixClient client, ExperimentOptions options){
if(isSupport()) {
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());
}
Class<T> tClass = getClazz();
try{
T t = tClass.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());
}
}
}