public class AssertTools extends Object
本工具类不封装过多判断逻辑,鼓励充分使用项目中的工具类进行逻辑判断。
checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
checkCondition(!CollectionUtils.isEmpty(roles),
() -> new InvalidInputException("The roles cannot be empty."));
checkCondition(RegexTools.matches(email, PatternConsts.EMAIL),
"must be a well-formed email address");
| Modifier and Type | Method and Description |
|---|---|
static void |
checkArgument(boolean condition)
检查实参
|
static void |
checkArgument(boolean condition,
String errorMessage)
检查实参
|
static void |
checkArgument(boolean condition,
String errorMessageTemplate,
Object... errorMessageArgs)
检查实参
|
static void |
checkArgument(boolean condition,
Supplier<String> errorMessageSupplier)
检查实参
|
static <T> T |
checkArgumentNotNull(T obj)
判断入参不为
null |
static <T> T |
checkArgumentNotNull(T obj,
String errorMessage)
判断入参不为
null |
static <T> T |
checkArgumentNotNull(T obj,
String errorMessageTemplate,
Object... errorMessageArgs)
判断入参不为
null |
static <T> T |
checkArgumentNotNull(T obj,
Supplier<String> errorMessageSupplier)
判断入参不为
null |
static <T extends Exception> |
checkCondition(boolean condition,
Supplier<T> e)
当条件不满足时抛出异常。
|
static <T> T |
checkExists(Optional<T> optional)
检查数据是否存在
|
static <T> T |
checkExists(Optional<T> optional,
String errorMessage)
检查数据是否存在
|
static <T> T |
checkExists(Optional<T> optional,
String errorMessageTemplate,
Object... errorMessageArgs)
检查数据是否存在
|
static <T> T |
checkExists(Optional<T> optional,
Supplier<String> errorMessageSupplier)
检查数据是否存在
|
static <T> T |
checkExists(T obj)
检查数据是否存在
|
static <T> T |
checkExists(T obj,
String errorMessage)
检查数据是否存在
|
static <T> T |
checkExists(T obj,
String errorMessageTemplate,
Object... errorMessageArgs)
检查数据是否存在
|
static <T> T |
checkExists(T obj,
Supplier<String> errorMessageSupplier)
检查数据是否存在
|
static <T> void |
checkNotNull(T obj)
判空
|
static <T> void |
checkNotNull(T obj,
String errorMessage)
判空
|
static <T> void |
checkNotNull(T obj,
String errorMessageTemplate,
Object... errorMessageArgs)
判空
|
static <T> void |
checkNotNull(T obj,
Supplier<String> errorMessageSupplier)
判空
|
static void |
checkState(boolean condition)
检查状态
|
static void |
checkState(boolean condition,
String errorMessage)
检查状态
|
static void |
checkState(boolean condition,
String errorMessageTemplate,
Object... errorMessageArgs)
检查状态
|
static void |
checkState(boolean condition,
Supplier<String> errorMessageSupplier)
检查状态
|
public static void checkArgument(boolean condition)
condition - 判断参数是否符合条件的结果IllegalArgumentException - 当条件不满足时抛出public static void checkArgument(boolean condition,
@Nullable
String errorMessage)
condition - 判断参数是否符合条件的结果errorMessage - 异常信息IllegalArgumentException - 当条件不满足时抛出public static void checkArgument(boolean condition,
Supplier<String> errorMessageSupplier)
condition - 判断参数是否符合条件的结果errorMessageSupplier - 异常信息IllegalArgumentException - 当条件不满足时抛出public static void checkArgument(boolean condition,
String errorMessageTemplate,
Object... errorMessageArgs)
condition - 判断参数是否符合条件的结果errorMessageTemplate - 异常信息模板errorMessageArgs - 异常信息参数IllegalArgumentException - 当条件不满足时抛出public static <T> T checkArgumentNotNull(@Nullable T obj)
nullT - 入参类型obj - 入参IllegalArgumentException - 当 obj 为 null 时抛出public static <T> T checkArgumentNotNull(@Nullable T obj, String errorMessage)
nullT - 入参类型obj - 入参errorMessage - 异常信息IllegalArgumentException - 当 obj 为 null 时抛出public static <T> T checkArgumentNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier)
nullT - 入参类型obj - 入参errorMessageSupplier - 异常信息IllegalArgumentException - 当 obj 为 null 时抛出public static <T> T checkArgumentNotNull(@Nullable T obj, String errorMessageTemplate, Object... errorMessageArgs)
nullT - 入参类型obj - 入参errorMessageTemplate - 异常信息模板errorMessageArgs - 异常信息参数IllegalArgumentException - 当 obj 为 null 时抛出public static void checkState(boolean condition)
condition - 判断状态是否符合条件的结果IllegalStateException - 当条件不满足时抛出public static void checkState(boolean condition,
@Nullable
String errorMessage)
condition - 判断状态是否符合条件的结果errorMessage - 异常信息IllegalStateException - 当条件不满足时抛出public static void checkState(boolean condition,
Supplier<String> errorMessageSupplier)
condition - 判断状态是否符合条件的结果errorMessageSupplier - 异常信息IllegalStateException - 当条件不满足时抛出public static void checkState(boolean condition,
String errorMessageTemplate,
Object... errorMessageArgs)
condition - 判断状态是否符合条件的结果errorMessageTemplate - 异常信息模板errorMessageArgs - 异常信息参数IllegalStateException - 当条件不满足时抛出public static <T> void checkNotNull(@Nullable T obj)
T - 入参类型obj - 入参NullPointerException - 当 obj 为 null 时抛出public static <T> void checkNotNull(@Nullable T obj, String errorMessage)
T - 入参类型obj - 入参errorMessage - 异常信息NullPointerException - 当 obj 为 null 时抛出public static <T> void checkNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier)
T - 入参类型obj - 入参errorMessageSupplier - 异常信息NullPointerException - 当 obj 为 null 时抛出public static <T> void checkNotNull(@Nullable T obj, String errorMessageTemplate, Object... errorMessageArgs)
T - 入参类型obj - 入参errorMessageTemplate - 异常信息模板errorMessageArgs - 异常信息参数NullPointerException - 当 obj 为 null 时抛出public static <T> T checkExists(@Nullable T obj) throws DataNotExistsException
T - 入参类型obj - 入参obj 存在,返回 obj 本身DataNotExistsException - 当 obj 不存在时抛出public static <T> T checkExists(@Nullable T obj, String errorMessage) throws DataNotExistsException
T - 入参类型obj - 入参errorMessage - 异常信息obj 存在,返回 obj 本身DataNotExistsException - 当 obj 不存在时抛出public static <T> T checkExists(@Nullable T obj, Supplier<String> errorMessageSupplier) throws DataNotExistsException
T - 入参类型obj - 入参errorMessageSupplier - 异常信息obj 存在,返回 obj 本身DataNotExistsException - 当 obj 不存在时抛出public static <T> T checkExists(@Nullable T obj, String errorMessageTemplate, Object... errorMessageArgs) throws DataNotExistsException
T - 入参类型obj - 入参errorMessageTemplate - 异常信息模板errorMessageArgs - 异常信息参数obj 存在,返回 obj 本身DataNotExistsException - 当 obj 不存在时抛出public static <T> T checkExists(Optional<T> optional) throws DataNotExistsException
T - 入参类型optional - 入参optional 存在,返回 optional 包含的值DataNotExistsException - 当 optional 的值不存在时抛出public static <T> T checkExists(Optional<T> optional, String errorMessage) throws DataNotExistsException
T - 入参类型optional - 入参errorMessage - 异常信息optional 存在,返回 optional 包含的值DataNotExistsException - 当 optional 的值不存在时抛出public static <T> T checkExists(Optional<T> optional, Supplier<String> errorMessageSupplier) throws DataNotExistsException
T - 入参类型optional - 入参errorMessageSupplier - 异常信息optional 存在,返回 optional 包含的值DataNotExistsException - 当 optional 的值不存在时抛出public static <T> T checkExists(Optional<T> optional, String errorMessageTemplate, Object... errorMessageArgs) throws DataNotExistsException
T - 入参类型optional - 入参errorMessageTemplate - 异常信息模板errorMessageArgs - 异常信息参数optional 存在,返回 optional 包含的值DataNotExistsException - 当 optional 的值不存在时抛出Copyright © 2026. All rights reserved.