com.baidu.unbiz.fluentvalidator
类 FluentValidator

java.lang.Object
  继承者 com.baidu.unbiz.fluentvalidator.FluentValidator

@NotThreadSafe
@Stateful
public class FluentValidator
extends Object

链式调用验证器

按照Fluent Interface风格实现的验证工具,以一种近似于可以语义解释的方式做对象的验证。

典型的调用方式如下:

 Result ret = FluentValidator.checkAll().failFast()
     .on(car.getLicensePlate(), new CarLicensePlateValidator())
     .on(car.getManufacturer(), new CarManufacturerValidator())
     .on(car.getSeatCount(), new CarSeatCountValidator())
     .doValidate().result(toSimple());
 


字段摘要
protected  ValidateCallback defaultCb
          默认验证回调
 
构造方法摘要
protected FluentValidator()
          私有构造方法,只能通过checkAll()去创建对象
 
方法摘要
static FluentValidator checkAll()
          创建FluentValidator
static FluentValidator checkAll(Class... groups)
          创建FluentValidator
 FluentValidator configure(Registry registry)
          如果启用通过FluentValidate注解方式的验证,需要寻找验证器实例,这里配置注册中心的步骤
protected  void doAdd(ListAble<ValidatorElement> listAble)
          将验证对象及其验证器放入validatorElementList
protected
<T> MultiValidatorElement
doOn(T t)
          在某个对象上通过FluentValidate注解方式的验证, 需要保证configure(Registry)已经先执行配置完毕Registry
 FluentValidator doValidate()
          按照默认验证回调条件,开始使用验证
 FluentValidator doValidate(ValidateCallback cb)
          按照指定验证回调条件,开始使用验证
 FluentValidator failFast()
          出错即退出
 FluentValidator failOver()
          出错不退出而继续
<T> FluentValidator
on(T t)
          在某个对象上通过FluentValidate注解方式的验证, 需要保证configure(Registry)已经先执行配置完毕Registry
<T> FluentValidator
on(T t, Validator<T> v)
          在待验证对象t上,使用v验证器进行验证
<T> FluentValidator
on(T t, ValidatorChain chain)
          在待验证对象t上,使用chain验证器链进行验证
<T> FluentValidator
onEach(Collection<T> t)
          在某个集合对象上通过FluentValidate注解方式的验证, 需要保证configure(Registry)已经先执行配置完毕Registry

注:当集合为空时,则会跳过

<T> FluentValidator
onEach(Collection<T> t, Validator<T> v)
          在待验证对象集合t上,使用v验证器进行验证

注:当集合为空时,则会跳过

<T> FluentValidator
onEach(T[] t)
          在某个数组对象上通过FluentValidate注解方式的验证, 需要保证configure(Registry)已经先执行配置完毕Registry

注:当数组为空时,则会跳过

<T> FluentValidator
onEach(T[] t, Validator<T> v)
          在待验证对象数组t上,使用v验证器进行验证

注:当数组为空时,则会跳过

 FluentValidator putAttribute2Context(String key, Object value)
          将键值对放入上下文
 FluentValidator putClosure2Context(String key, Closure value)
          将闭包注入上下文
<T> T
result(ResultCollector<T> resultCollector)
          转换为对外的验证结果,在FluentValidator.on(..).on(..).doValidate()这一连串“惰性求值”计算后的“及时求值”收殓出口。
 FluentValidator setExcludeGroups(Class<?>[] excludeGroups)
          设置排除的分组
 FluentValidator setGroups(Class<?>[] groups)
          设置分组
 FluentValidator setIsFailFast(boolean isFailFast)
          设置是否快速失败
 FluentValidator when(boolean expression)
          当满足expression条件时,才去使用前一个Validator或者ValidatorChain来验证
 FluentValidator withContext(ValidatorContext context)
          使用已经存在的一个验证上下文,共享context本身以及验证结果
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

defaultCb

protected ValidateCallback defaultCb
默认验证回调

构造方法详细信息

FluentValidator

protected FluentValidator()
私有构造方法,只能通过checkAll()去创建对象

方法详细信息

putAttribute2Context

public FluentValidator putAttribute2Context(String key,
                                            Object value)
将键值对放入上下文

参数:
key - 键
value - 值
返回:
FluentValidator

putClosure2Context

public FluentValidator putClosure2Context(String key,
                                          Closure value)
将闭包注入上下文

参数:
key - 键
value - 闭包
返回:
FluentValidator

checkAll

public static FluentValidator checkAll()
创建FluentValidator

返回:
FluentValidator

checkAll

public static FluentValidator checkAll(Class... groups)
创建FluentValidator

参数:
groups - 分组
返回:
FluentValidator

withContext

public FluentValidator withContext(ValidatorContext context)
使用已经存在的一个验证上下文,共享context本身以及验证结果

参数:
context - 验证上下文
返回:
FluentValidator

failFast

public FluentValidator failFast()
出错即退出

返回:
FluentValidator

failOver

public FluentValidator failOver()
出错不退出而继续

返回:
FluentValidator

configure

public FluentValidator configure(Registry registry)
如果启用通过FluentValidate注解方式的验证,需要寻找验证器实例,这里配置注册中心的步骤

参数:
registry - 验证器注册查找器
返回:
FluentValidator

on

public <T> FluentValidator on(T t)
在某个对象上通过FluentValidate注解方式的验证, 需要保证configure(Registry)已经先执行配置完毕Registry

参数:
t - 待验证对象
返回:
FluentValidator

onEach

public <T> FluentValidator onEach(T[] t)
在某个数组对象上通过FluentValidate注解方式的验证, 需要保证configure(Registry)已经先执行配置完毕Registry

注:当数组为空时,则会跳过

参数:
t - 待验证对象
返回:
FluentValidator

onEach

public <T> FluentValidator onEach(Collection<T> t)
在某个集合对象上通过FluentValidate注解方式的验证, 需要保证configure(Registry)已经先执行配置完毕Registry

注:当集合为空时,则会跳过

参数:
t - 待验证对象
返回:
FluentValidator

doOn

protected <T> MultiValidatorElement doOn(T t)
在某个对象上通过FluentValidate注解方式的验证, 需要保证configure(Registry)已经先执行配置完毕Registry

参数:
t - 待验证对象
返回:
FluentValidator

on

public <T> FluentValidator on(T t,
                              Validator<T> v)
在待验证对象t上,使用v验证器进行验证

参数:
t - 待验证对象
v - 验证器
返回:
FluentValidator

on

public <T> FluentValidator on(T t,
                              ValidatorChain chain)
在待验证对象t上,使用chain验证器链进行验证

参数:
t - 待验证对象
chain - 验证器链
返回:
FluentValidator

onEach

public <T> FluentValidator onEach(T[] t,
                                  Validator<T> v)
在待验证对象数组t上,使用v验证器进行验证

注:当数组为空时,则会跳过

参数:
t - 待验证对象数组
v - 验证器
返回:
FluentValidator

onEach

public <T> FluentValidator onEach(Collection<T> t,
                                  Validator<T> v)
在待验证对象集合t上,使用v验证器进行验证

注:当集合为空时,则会跳过

参数:
t - 待验证对象集合
v - 验证器
返回:
FluentValidator

doAdd

protected void doAdd(ListAble<ValidatorElement> listAble)
将验证对象及其验证器放入validatorElementList

参数:
listAble - 验证对象及其验证器封装类

when

public FluentValidator when(boolean expression)
当满足expression条件时,才去使用前一个Validator或者ValidatorChain来验证

参数:
expression - 满足条件表达式
返回:
FluentValidator

doValidate

public FluentValidator doValidate()
按照默认验证回调条件,开始使用验证

返回:
FluentValidator

doValidate

public FluentValidator doValidate(ValidateCallback cb)
按照指定验证回调条件,开始使用验证

参数:
cb - 验证回调
返回:
FluentValidator
另请参见:
ValidateCallback

result

public <T> T result(ResultCollector<T> resultCollector)
转换为对外的验证结果,在FluentValidator.on(..).on(..).doValidate()这一连串“惰性求值”计算后的“及时求值”收殓出口。

<T>是验证结果的泛型

参数:
resultCollector - 验证结果收集器
返回:
对外验证结果

setGroups

public FluentValidator setGroups(Class<?>[] groups)
设置分组

参数:
groups - 分组
返回:
FluentValidator

setIsFailFast

public FluentValidator setIsFailFast(boolean isFailFast)
设置是否快速失败

参数:
isFailFast - 是否快速失败
返回:
FluentValidator

setExcludeGroups

public FluentValidator setExcludeGroups(Class<?>[] excludeGroups)
设置排除的分组

参数:
excludeGroups - 排除分组
返回:
FluentValidator


Copyright © 2015–2016 neoremind. All rights reserved.