1.FluentValidator
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* 链式调用验证器
* @date 2019/12/27
*/
public class FluentValidator {
private static final Logger logger = LoggerFactory.getLogger(FluentValidator.class);
private List<ValidateElement> validatorElementList = new ArrayList<>();
/**
* 验证器上下文
*/
private ValidateContext context = new ValidateContext();
/**
* 私有构造方法,只能通过checkAll创建对象
*/
private FluentValidator() {
}
/**
* 创建FluentValidator对象
* @return
*/
public static FluentValidator checkAll() {
return new FluentValidator();
}
/**
* 使用验证器进行验证
* @param validate 验证器
* @return
*/
public FluentValidator on(Validate validate) {
validatorElementList.add(new ValidateElement(null, validate));
return this;
}
/**
* 使用验证器验证指定对象
* @param t 待验证对象
* @param validate 验证器
* @return
*/
public FluentValidator on(Validate validate,Object t) {
validatorElementList.add(new ValidateElement(t, validate));
return this;
}
/**
* 使用验证器验证指定对象
* @param t 待验证对象
* @param validate 验证器
* @param condition 条件,为true时才会将验证器加入验证器列表中
* @return
*/
public FluentValidator on(Object t, Validate validate, boolean condition) {
if (condition) {
validatorElementList.add(new ValidateElement(t, validate));
}
return this;
}
/**
* 执行各个验证器中的验证逻辑
*/
public FluentValidator doValidate() {
if (validatorElementList.isEmpty()) {
logger.info("Nothing to validate");
return null;
}
long start = System.currentTimeMillis();
String validatorName;
try {
for (ValidateElement element : validatorElementList) {
Object target = element.getTarget();
Validate validate = element.getValidator();
validatorName = validate.getClass().getSimpleName();
logger.info("{} is running", validatorName);
validate.validate(context, target);
}
} catch (ValidateException e) {
throw e;
} catch (Exception e) {
throw e;
} finally {
logger.info("End to validate,time consuming {} ms", (System.currentTimeMillis() - start));
}
return this;
}
/**
* 将键值对放入上下文
* @param key 键
* @param value 值
* @return FluentValidator
*/
public FluentValidator putAttribute2Context(String key, Object value) {
if (context == null) {
context = new ValidateContext();
}
context.setAttribute(key, value);
return this;
}
/**
* 获取验证器上下文
* @return
*/
public ValidateContext getContext() {
return context;
}
}
2.Validate
/**
* 验证器接口,泛型T表示待验证对象的类型
*/
public interface Validate {
/**
* 执行验证,如果验证失败,则抛出ValidateException异常
* @param context 验证上下文
* @param t 待验证对象
*/
void validate(ValidateContext context, Object t);
}
3.Validate的校验实现类CustomerSignValidator.校验不通过,抛出异常;校验通过,传递上下文
import lombok.extern.slf4j.Slf4j;
/**
* @Description 类描述
* @Author tangzhiqiang
* @Date Created in 15:57 2021/4/9
*/
@Slf4j
public class CustomerSignValidator implements Validate{
@Override
public void validate(ValidateContext context, Object t) {
if(t.equals("22")){
throw new RuntimeException("报错了");
}
context.setAttribute("zhasnag",t);
}
}
4.ValidateContext,上下文
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
/**
* 验证器在执行调用过程中的上下文
* 1.验证器中的数据传递共享
* 2.验证结果数据缓存以作后续使用
*/
public class ValidateContext {
private Map attributes;
/**
* 设置属性值
* @param key 键
* @param value 值
*/
public void setAttribute(String key, Object value) {
if (attributes == null) {
attributes = new HashMap<>();
}
attributes.put(key, value);
}
/**
* 获取String值
* @param key
* @return
*/
public String getString(String key) {
return (String) getAttribute(key);
}
/**
* 获取Integer值
* @param key
* @return
*/
public Integer getInteger(String key) {
return (Integer) getAttribute(key);
}
/**
* 获取Boolean值
* @param key
* @return
*/
public Boolean getBoolean(String key) {
return (Boolean) getAttribute(key);
}
/**
* 获取Long值
* @param key
* @return
*/
public Long getLong(String key) {
return (Long) getAttribute(key);
}
/**
* 获取BigDecimal值
* @param key
* @return
*/
public BigDecimal getBigDecimal(String key) {
return (BigDecimal) getAttribute(key);
}
/**
* 获取对象
* @param key
* @param
* @return
*/
public T getClazz(String key) {
return (T) getAttribute(key);
}
/**
* 获取属性
* @param key 键
* @return 值
*/
public Object getAttribute(String key) {
if (attributes != null && !attributes.isEmpty()) {
return attributes.get(key);
}
return null;
}
}
5.ValidateElement,验证器包装类
import lombok.Getter;
import lombok.Setter;
/**
* @Description 验证器包装类
*/
@Setter
@Getter
public class ValidateElement {
/**
* 待验证对象
*/
private Object target;
/**
* 验证器
*/
private Validate validator;
public ValidateElement(Object target,Validate validator){
this.target =target;
this.validator =validator;
}
}
6.Test,测试类
/**
* @Description 类描述
* @Author tangzhiqiang
* @Date Created in 16:05 2021/4/9
*/
public class Test {
public static void main(String[] args) {
FluentValidator fluentValidator = FluentValidator.checkAll()
.on(new CustomerSignValidator(),"111")
.doValidate();
ValidateContext context = fluentValidator.getContext();
System.out.println(context.getAttribute("zhasnag"));
}
}