com.biz.verification.handler.CheckParameterSizeHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of biz-all Show documentation
Show all versions of biz-all Show documentation
BizX 是一个灵活而高效的业务开发框架, 其中也有很多为业务开发所需要的工具类的提供。
The newest version!
package com.biz.verification.handler;
import com.biz.common.utils.Common;
import com.biz.verification.annotation.check.BizXCheckSize;
import com.biz.verification.error.BizXVerificationException;
import com.biz.verification.strategy.CheckParameterStrategy;
import lombok.extern.slf4j.Slf4j;
import java.lang.annotation.Annotation;
import java.util.Collection;
/**
* 具体实现检查长度的处理器
*
* 该类实现了 {@link CheckParameterStrategy} 接口,用于处理带有 {@link BizXCheckSize} 注解的参数。
*
*
* @see CheckParameterStrategy
* @see BizXCheckSize
* @see BizXVerificationException
* @see Common
* @see Annotation
* @see Collection
*
* @author francis
* @since 1.0.1
*/
@Slf4j
public class CheckParameterSizeHandler implements CheckParameterStrategy {
/**
* 获取当前处理器支持的注解类型
*
* @return 支持的注解类型 {@link BizXCheckSize}
*/
@Override
public Class extends Annotation> getCheckAnnotation() {
return BizXCheckSize.class;
}
/**
* 检查注解和参数是否满足注解的要求
*
* @param annotation 注解实例
* @param value 被检查的参数
* @param className 类名称
* @param methodName 方法名称
* @param fieldName 参数名称
* @throws BizXVerificationException 如果参数不满足注解的要求,则抛出此异常
*/
@Override
public void check(Annotation annotation, Object value, String className, String methodName, String fieldName) throws BizXVerificationException {
if (value == null) {
return;
}
if (annotation instanceof BizXCheckSize) {
BizXCheckSize check = Common.to(annotation);
if (value instanceof Collection) {
Collection> num = Common.to(value);
if (num.size() < check.min() || num.size() > check.max()) {
throwError(check, className, methodName, fieldName);
}
return;
}
if (value instanceof String) {
String num = Common.to(value);
if (num.length() < check.min() || num.length() > check.max()) {
throwError(check, className, methodName, fieldName);
}
}
}
}
private static void throwError(BizXCheckSize check, String className, String methodName, String fieldName) throws BizXVerificationException {
throw new BizXVerificationException(check.error().code(), check.error().message(), className, methodName, fieldName);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy