shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.di;
 
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.List;
import java.util.Map;
 
public abstract class AbstractTemplateGenerator<T> implements TemplateGenerator<T>{
 
    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
    @Override
    public T generate(Object option) throws TemplateException {
        if(!this.checkOption(option)){
            throw new TemplateException("option参数校验未通过", null);
        }
        List<Map<String, String>> content = this.acquireWriteContent(option);
        if(StringUtils.isEmptyList(content)){
            throw new TemplateException("未获取到写入模板内容", null);
        }
 
        return this.writeContent(content, option);
    }
 
    /**
     * 检测输入参数是否可用,子类可覆盖次方法。<p></p>
     * 该输入源可能是文件流、路径、配置参数等。
     * @param option
     * @return
     */
    protected boolean checkOption(Object option){
        return true;
    }
 
    /**
     * 获取要写入模板的内容。
     * @param option
     * @return
     */
    protected abstract List<Map<String, String>> acquireWriteContent(Object option);
 
    /**
     * 向模板中写入模板内容。
     * @param data key = 字段ID, value = 字段描述
     */
    protected abstract T writeContent(List<Map<String, String>> data, Object option) throws TemplateException;
}