import cn.hutool.core.util.StrUtil;
import java.util.Objects;
public class PreconditionsUtil {
// 例子
public static void main(String[] args) {
checkParams(1 < 0, "warmupPeriod must not be negative: {}", 1111);
}
public static void checkParams(Boolean conditionBool, String errorMessageTemplate,Object... args ) {
check(new IllegalArgumentException(),conditionBool, errorMessageTemplate , args);
}
public static void checkState(Boolean conditionBool, String errorMessageTemplate,Object... args ) {
check(new IllegalStateException(),conditionBool, errorMessageTemplate , args);
}
private static void check(Throwable throwable, Boolean conditionBool, String errorMessageTemplate, Object... args){
if (!Boolean.TRUE.equals(conditionBool)) {
if (Objects.isNull(args)) {
throw new RuntimeException(errorMessageTemplate,throwable);
} else {
throw new RuntimeException(StrUtil.format(errorMessageTemplate, args),throwable);
}
}
}
}
校验小工具
最新推荐文章于 2025-02-03 21:58:19 发布