优化:优化校验工具类的使用及部分模板文本写法

1.优化校验工具类,支持传入 {} 模板文本
2.校验工具类增加 throwIf 重载方法,适合于 boolean 类型参数的情况
3.优化一些模板文本的写法
4.优化一些其他细节
This commit is contained in:
2023-03-20 20:44:52 +08:00
parent 139cb337d7
commit 6d3ba478e9
13 changed files with 265 additions and 177 deletions

View File

@@ -40,19 +40,7 @@ public class CheckUtils extends Validator {
private static final Class<ServiceException> EXCEPTION_TYPE = ServiceException.class;
/**
* 如果为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
*/
public static void throwIfNull(Object obj, String message) {
throwIfNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
* 如果不存在,抛出异常
*
* @param obj
* 被检测的对象
@@ -63,22 +51,38 @@ public class CheckUtils extends Validator {
* @param fieldValue
* 字段值
*/
public static void throwIfNull(Object obj, String entityName, String fieldName, Object fieldValue) {
public static void throwIfNotExists(Object obj, String entityName, String fieldName, Object fieldValue) {
String message =
String.format("%s 为 [%s] 的 %s 记录已不存在", fieldName, fieldValue, StrUtil.replace(entityName, "DO", ""));
throwIfNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
* @param obj
* 被检测的对象
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNull(Object obj, String template, Object... params) {
throwIfNull(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotNull(Object obj, String message) {
throwIfNotNull(obj, message, EXCEPTION_TYPE);
public static void throwIfNotNull(Object obj, String template, Object... params) {
throwIfNotNull(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -103,11 +107,13 @@ public class CheckUtils extends Validator {
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfEmpty(Object obj, String message) {
throwIfEmpty(obj, message, EXCEPTION_TYPE);
public static void throwIfEmpty(Object obj, String template, Object... params) {
throwIfEmpty(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -115,11 +121,13 @@ public class CheckUtils extends Validator {
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotEmpty(Object obj, String message) {
throwIfNotEmpty(obj, message, EXCEPTION_TYPE);
public static void throwIfNotEmpty(Object obj, String template, Object... params) {
throwIfNotEmpty(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -127,11 +135,13 @@ public class CheckUtils extends Validator {
*
* @param str
* 被检测的字符串
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfBlank(CharSequence str, String message) {
throwIfBlank(str, message, EXCEPTION_TYPE);
public static void throwIfBlank(CharSequence str, String template, Object... params) {
throwIfBlank(str, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -139,11 +149,13 @@ public class CheckUtils extends Validator {
*
* @param str
* 被检测的字符串
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotBlank(CharSequence str, String message) {
throwIfNotBlank(str, message, EXCEPTION_TYPE);
public static void throwIfNotBlank(CharSequence str, String template, Object... params) {
throwIfNotBlank(str, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -153,11 +165,13 @@ public class CheckUtils extends Validator {
* 要比较的对象1
* @param obj2
* 要比较的对象2
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfEqual(Object obj1, Object obj2, String message) {
throwIfEqual(obj1, obj2, message, EXCEPTION_TYPE);
public static void throwIfEqual(Object obj1, Object obj2, String template, Object... params) {
throwIfEqual(obj1, obj2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -167,11 +181,13 @@ public class CheckUtils extends Validator {
* 要比较的对象1
* @param obj2
* 要比较的对象2
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotEqual(Object obj1, Object obj2, String message) {
throwIfNotEqual(obj1, obj2, message, EXCEPTION_TYPE);
public static void throwIfNotEqual(Object obj1, Object obj2, String template, Object... params) {
throwIfNotEqual(obj1, obj2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -181,11 +197,13 @@ public class CheckUtils extends Validator {
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String message) {
throwIfEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
public static void throwIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String template, Object... params) {
throwIfEqualIgnoreCase(str1, str2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -195,11 +213,28 @@ public class CheckUtils extends Validator {
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String message) {
throwIfNotEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
public static void throwIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String template,
Object... params) {
throwIfNotEqualIgnoreCase(str1, str2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果条件成立,抛出异常
*
* @param condition
* 条件
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIf(boolean condition, String template, Object... params) {
throwIf(condition, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -207,10 +242,12 @@ public class CheckUtils extends Validator {
*
* @param conditionSupplier
* 条件
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIf(BooleanSupplier conditionSupplier, String message) {
throwIf(conditionSupplier, message, EXCEPTION_TYPE);
public static void throwIf(BooleanSupplier conditionSupplier, String template, Object... params) {
throwIf(conditionSupplier, StrUtil.format(template, params), EXCEPTION_TYPE);
}
}

View File

@@ -22,6 +22,8 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.exception.BadRequestException;
/**
@@ -42,11 +44,13 @@ public class ValidationUtils extends Validator {
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNull(Object obj, String message) {
throwIfNull(obj, message, EXCEPTION_TYPE);
public static void throwIfNull(Object obj, String template, Object... params) {
throwIfNull(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -54,11 +58,13 @@ public class ValidationUtils extends Validator {
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotNull(Object obj, String message) {
throwIfNotNull(obj, message, EXCEPTION_TYPE);
public static void throwIfNotNull(Object obj, String template, Object... params) {
throwIfNotNull(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -66,11 +72,13 @@ public class ValidationUtils extends Validator {
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfEmpty(Object obj, String message) {
throwIfEmpty(obj, message, EXCEPTION_TYPE);
public static void throwIfEmpty(Object obj, String template, Object... params) {
throwIfEmpty(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -78,11 +86,13 @@ public class ValidationUtils extends Validator {
*
* @param obj
* 被检测的对象
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotEmpty(Object obj, String message) {
throwIfNotEmpty(obj, message, EXCEPTION_TYPE);
public static void throwIfNotEmpty(Object obj, String template, Object... params) {
throwIfNotEmpty(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -90,11 +100,13 @@ public class ValidationUtils extends Validator {
*
* @param str
* 被检测的字符串
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfBlank(CharSequence str, String message) {
throwIfBlank(str, message, EXCEPTION_TYPE);
public static void throwIfBlank(CharSequence str, String template, Object... params) {
throwIfBlank(str, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -102,11 +114,13 @@ public class ValidationUtils extends Validator {
*
* @param str
* 被检测的字符串
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotBlank(CharSequence str, String message) {
throwIfNotBlank(str, message, EXCEPTION_TYPE);
public static void throwIfNotBlank(CharSequence str, String template, Object... params) {
throwIfNotBlank(str, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -116,11 +130,13 @@ public class ValidationUtils extends Validator {
* 要比较的对象1
* @param obj2
* 要比较的对象2
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfEqual(Object obj1, Object obj2, String message) {
throwIfEqual(obj1, obj2, message, EXCEPTION_TYPE);
public static void throwIfEqual(Object obj1, Object obj2, String template, Object... params) {
throwIfEqual(obj1, obj2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -130,11 +146,13 @@ public class ValidationUtils extends Validator {
* 要比较的对象1
* @param obj2
* 要比较的对象2
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotEqual(Object obj1, Object obj2, String message) {
throwIfNotEqual(obj1, obj2, message, EXCEPTION_TYPE);
public static void throwIfNotEqual(Object obj1, Object obj2, String template, Object... params) {
throwIfNotEqual(obj1, obj2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -144,11 +162,13 @@ public class ValidationUtils extends Validator {
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String message) {
throwIfEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
public static void throwIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String template, Object... params) {
throwIfEqualIgnoreCase(str1, str2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -158,11 +178,28 @@ public class ValidationUtils extends Validator {
* 要比较的字符串1
* @param str2
* 要比较的字符串2
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String message) {
throwIfNotEqualIgnoreCase(str1, str2, message, EXCEPTION_TYPE);
public static void throwIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String template,
Object... params) {
throwIfNotEqualIgnoreCase(str1, str2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果条件成立,抛出异常
*
* @param condition
* 条件
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIf(boolean condition, String template, Object... params) {
throwIf(condition, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
@@ -170,10 +207,12 @@ public class ValidationUtils extends Validator {
*
* @param conditionSupplier
* 条件
* @param message
* 错误信息
* @param template
* 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params
* 参数值
*/
public static void throwIf(BooleanSupplier conditionSupplier, String message) {
throwIf(conditionSupplier, message, EXCEPTION_TYPE);
public static void throwIf(BooleanSupplier conditionSupplier, String template, Object... params) {
throwIf(conditionSupplier, StrUtil.format(template, params), EXCEPTION_TYPE);
}
}

View File

@@ -47,7 +47,7 @@ public class Validator {
* 异常类型
*/
protected static void throwIfNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(() -> obj == null, message, exceptionType);
throwIf(obj == null, message, exceptionType);
}
/**
@@ -61,7 +61,7 @@ public class Validator {
* 异常类型
*/
protected static void throwIfNotNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(() -> obj != null, message, exceptionType);
throwIf(obj != null, message, exceptionType);
}
/**
@@ -75,7 +75,7 @@ public class Validator {
* 异常类型
*/
protected static void throwIfEmpty(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(() -> ObjectUtil.isEmpty(obj), message, exceptionType);
throwIf(ObjectUtil.isEmpty(obj), message, exceptionType);
}
/**
@@ -89,7 +89,7 @@ public class Validator {
* 异常类型
*/
protected static void throwIfNotEmpty(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(() -> ObjectUtil.isNotEmpty(obj), message, exceptionType);
throwIf(ObjectUtil.isNotEmpty(obj), message, exceptionType);
}
/**
@@ -104,7 +104,7 @@ public class Validator {
*/
protected static void throwIfBlank(CharSequence str, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(() -> StrUtil.isBlank(str), message, exceptionType);
throwIf(StrUtil.isBlank(str), message, exceptionType);
}
/**
@@ -119,7 +119,7 @@ public class Validator {
*/
protected static void throwIfNotBlank(CharSequence str, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(() -> StrUtil.isNotBlank(str), message, exceptionType);
throwIf(StrUtil.isNotBlank(str), message, exceptionType);
}
/**
@@ -136,7 +136,7 @@ public class Validator {
*/
protected static void throwIfEqual(Object obj1, Object obj2, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(() -> ObjectUtil.equal(obj1, obj2), message, exceptionType);
throwIf(ObjectUtil.equal(obj1, obj2), message, exceptionType);
}
/**
@@ -153,7 +153,7 @@ public class Validator {
*/
protected static void throwIfNotEqual(Object obj1, Object obj2, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(() -> ObjectUtil.notEqual(obj1, obj2), message, exceptionType);
throwIf(ObjectUtil.notEqual(obj1, obj2), message, exceptionType);
}
/**
@@ -170,7 +170,7 @@ public class Validator {
*/
protected static void throwIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(() -> StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
throwIf(StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
}
/**
@@ -187,7 +187,24 @@ public class Validator {
*/
protected static void throwIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(() -> !StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
throwIf(!StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
}
/**
* 如果条件成立,抛出异常
*
* @param condition
* 条件
* @param message
* 错误信息
* @param exceptionType
* 异常类型
*/
protected static void throwIf(boolean condition, String message, Class<? extends RuntimeException> exceptionType) {
if (condition) {
log.error(message);
throw ReflectUtil.newInstance(exceptionType, message);
}
}
/**