public class CheckParamUtil {
private static final List<Class<?>> DIGIT_CLASS = Lists.newArrayList(new Class<?>[] {Integer.class, Long.class, Short.class});
private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
/**
* 获取校验接口
*
* @param name
* @param value
* @return
*/
public static Checker getChecker(String name, Object value) {
return new Checker(name, value);
}
/**
* 默认校验实现
*/
public static class Checker {
/**
* 参数名称
*/
private final String name;
/**
* 参数值
*/
private final Object value;
private Checker(String name, Object value) {
this.name = name;
this.value = value;
}
/**
* 数字参数校验
* <pre>
* CheckParamUtil.getChecker("fieldName", null).digit() => success
* CheckParamUtil.getChecker("fieldName", 1234).digit() => success
* CheckParamUtil.getChecker("fieldName", "123").digit() => success
* CheckParamUtil.getChecker("fieldName", 1.23).digit() => failure
* CheckParamUtil.getChecker("fieldName", "1.23").digit() => failure
* </pre>
*
* @return Checker
*/
public Checker digit() {
// NULL不做处理
if (value == null) {
return this;
}
// 数字
if (DIGIT_CLASS.contains(value.getClass())) {
return this;
}
// 字符串
if (value instanceof String) {
if (NumberUtils.isDigits((String) value)) {
return this;
}
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 必须是数字的字符串");
}
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 必须是数字");
}
/**
* 参数非空
* <pre>
* CheckParamUtil.getChecker("fieldName", 1234).notNull() => success
* CheckParamUtil.getChecker("fieldName", "1234").notNull() => success
* CheckParamUtil.getChecker("fieldName", null).notNull() => failure
* </pre>
*
* @return Checker
*/
public Checker notNull() {
if (value == null) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 不能为空");
}
return this;
}
/**
* 非空(用于集合)
* <pre>
* CheckParamUtil.getChecker("fieldName", {1,2,3,4}).notEmpty() => success
* CheckParamUtil.getChecker("fieldName", {1:1, 2:2}).notEmpty() => success
* CheckParamUtil.getChecker("fieldName", null).notEmpty() => failure
* CheckParamUtil.getChecker("fieldName", {}).notEmpty() => failure
* </pre>
*
* @return Checker
*/
public Checker notEmpty() {
notNull();
if ((value instanceof Map) && CollectionUtils.isEmpty((Map<?, ?>) value)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 不能为空");
}
if ((value instanceof Collection) && CollectionUtils.isEmpty((Collection<?>) value)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 不能为空");
}
return this;
}
/**
* 非空(用于字符串)
* <pre>
* CheckParamUtil.getChecker("fieldName", "string").notBlank() => success
* CheckParamUtil.getChecker("fieldName", 12345678).notBlank() => failure
* CheckParamUtil.getChecker("fieldName", " 1234 ").notBlank() => success
* CheckParamUtil.getChecker("fieldName", null).notBlank() => failure
* CheckParamUtil.getChecker("fieldName", "").notBlank() => failure
* CheckParamUtil.getChecker("fieldName", " ").notBlank() => failure
* CheckParamUtil.getChecker("fieldName", "\r\t\n").notBlank() => failure
* </pre>
*
* @return Checker
*/
public Checker notBlank() {
if (value == null || value instanceof String) {
notNull();
if (StringUtils.isBlank((String) value)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 不能为空");
}
} else {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 必须是字符串");
}
return this;
}
/**
* 长度
* <pre>
* CheckUtil.getChecker("fieldName", "12345").length(5) => success
* CheckUtil.getChecker("fieldName", 12345).length(5) => success
* CheckUtil.getChecker("fieldName", 0).length(0) => success
* CheckUtil.getChecker("fieldName", "").length(0) => success
* CheckUtil.getChecker("collectionName", emptyList).length(0) => true
* CheckUtil.getChecker("collectionName", emptyList).length(3) => true
* CheckUtil.getChecker("collectionName", oneElementList).length(3) => failure
* CheckUtil.getChecker("collectionName", oneElementList).length(0) => failure
* CheckUtil.getChecker("collectionName", 2).length(0) => failure
* CheckUtil.getChecker("collectionName", "123456").length(0) => failure
* CheckUtil.getChecker("fieldName", null).length(5) => failure
* CheckUtil.getChecker("fieldName", "123456").length(5) => failure
* CheckUtil.getChecker("collectionName", fiveElementList).length(3) => failure
* </pre>
*
* @param length 长度
* @return Checker
*/
public Checker length(int length) {
if (value == null || value instanceof String) {
if (String.valueOf(value).length() != length) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 长度必须是" + length);
}
return this;
} else if (value instanceof Collection) {
if (((Collection<?>) value).size() != length) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 长度必须是" + length);
}
return this;
} else if (value instanceof Integer) {
if (String.valueOf(value).length() != length) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 长度必须是" + length);
}
return this;
} else {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 必须是字符串、集合或数字");
}
}
/**
* 数字范围
* <pre>
* min == null && max != null => value <= max
* min != null && max == null => value >= min
* min != null && max != null => min <= value <= max
* </pre>
*
* @param min
* @param max
* @return
*/
public Checker range(Long min, Long max) {
digit();
Long val = Long.valueOf(value.toString());
if (min != null && !(val >= min)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 必须大于" + min);
}
if (max != null && !(val <= max)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 必须小于等于" + max);
}
return this;
}
/**
* 检查值是否等于或小于参数max
*
* @param max
* @return
*/
public Checker equalOrLessThan(Long max) {
notNull();
digit();
Long val = null;
if (value instanceof Long) {
val = (Long) value;
} else {
val = Long.valueOf(value.toString());
}
if (max != null && (val > max)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 不能超过" + max);
}
return this;
}
/**
* 检查字符串长度是否等于或小于参数max
*
* @param max
* @return
*/
public Checker lengthEqualOrLessThan(Integer max) {
notNull();
if (value instanceof String) {
if (max != null && (((String) value).length() > max)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 长度不能大于" + max);
}
} else {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 必须是字符串");
}
return this;
}
/**
* 检查集合容量是否等于或小于参数max
*
* @param max
* @return
*/
public Checker sizeEqualOrLessThan(Integer max) {
notNull();
if (value instanceof Collection) {
if (max != null && (((Collection<?>) value).size() > max)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 不能大于" + max);
}
} else if ((value instanceof Map)) {
if (max != null && (((Map<?, ?>) value).size() > max)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 不能大于" + max);
}
} else {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 必须是集合");
}
return this;
}
/**
* 检查参数是否是日期格式(例:yyyyMMdd)
*
* @return
*/
public Checker isDateFormat() {
notBlank();
try {
DateFormat dateFormat = new SimpleDateFormat((String) value);
Date date = new Date();
// 判断入参是否能当作模板来格式化时间
String dateString = dateFormat.format(date);
// 将格式化后的时间转换为Date
Date parse = dateFormat.parse(dateString);
// 判断时间格式化前后,年份是否一致
if (date.getYear() != parse.getYear() || date.getMonth() != parse.getMonth() || date.getDay() != parse.getDay()) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR, name + " 时间类型的默认值不正确,只允许日期格式模板,例如“yyyyMMdd或yyyy-MM-dd”");
}
return this;
} catch (Throwable throwable) {
// 捕获类型转换,空指针或非法参数异常
throw new CCException(ErrorCodeEnum.PARAM_ERROR.getCode(), name
+ " 时间类型的默认值不正确,只允许日期格式模板,例如“yyyyMMdd”", throwable);
}
}
/**
* 检查值是否是时间(例:2020-03-10)
*
* @return
*/
public Checker isDateyyyyMMdd() {
notBlank();
try {
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
dateFormat.parse((String) value);
return this;
} catch (Throwable throwable) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR.getCode(), name + " 时间类型的值不能为空,且只允许yyyy-MM-dd格式", throwable);
}
}
/**
* 检查两个字符是否相等
*
* @param equalsParam 需要比较的参数
* @param errorMessage 错误信息
* @return
*/
public Checker equals(String equalsParam, String errorMessage) {
notBlank();
if (StringUtils.equals((String) value, equalsParam)) {
return this;
} else {
throw new CCException(ErrorCodeEnum.PARAM_ERROR.getCode(), errorMessage);
}
}
/**
* 判断表达式是否成立,如不成立则抛出异常,异常为errorMessage
*
* @param errorMessage 表达式不成立
* @return
*/
public Checker isTrue(String errorMessage) {
if (!(value instanceof Boolean)) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR.getCode(), errorMessage);
}
if (!(Boolean) value) {
throw new CCException(ErrorCodeEnum.PARAM_ERROR.getCode(), errorMessage);
}
return this;
}
}
}
参数校验工具类
于 2022-08-29 14:06:39 首次发布
1191

被折叠的 条评论
为什么被折叠?



