概述
package tf56.hermesRuleConfig.util.validate;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.tf56.core.exception.BizException;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Set;
/**
* 断言工具类
*/
public class AssertUtil {
/**
* 断言指定group通过注解方式的检验
*/
public static void validate(Object value) {
notEmpty(value, "bean不能为空");
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Object>> validateProperty = validator.validate(value);
for (ConstraintViolation constraintViolation : validateProperty) {
throw new BizException(constraintViolation.getPropertyPath() + constraintViolation.getMessage());
}
}
/**
* 断言指定group通过注解方式的检验
*/
public static void validate(Object value, Class<?>... groups) {
notEmpty(value, "bean不能为空");
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Object>> validateProperty = validator.validate(value, groups);
for (ConstraintViolation constraintViolation : validateProperty) {
throw new BizException(constraintViolation.getPropertyPath() + constraintViolation.getMessage());
}
}
/**
* 断言为空 支持各种类型
*/
public static void empty(Object value, String msg) {
isTrue(ObjectUtil.isEmpty(value), msg);
}
/**
* 断言不为空,支持多种类型
*/
public static void notEmpty(Object value, String msg) {
isTrue(ObjectUtil.isNotEmpty(value), msg);
}
/**
* 断言bean的所有属性都不为空
*/
public static void notEmptyBean(Object value) {
notEmpty(value, "bean为空");
Class clz = value.getClass();
for (Field field : ReflectUtil.getFields(clz)) {
Object propertyValue = ReflectUtil.getFieldValue(value, field);
notEmpty(propertyValue, field.getName() + "属性值为空");
}
}
/**
* 断言等于0
*/
public static void eqZero(Object value, String msg) {
BigDecimal num = Convert.toBigDecimal(value);
notEmpty(value, msg);
equal(BigDecimal.ZERO, num, msg);
}
/**
* 断言大于0
*/
public static void gtZero(Object value, String msg) {
BigDecimal valueBigDecimal = Convert.toBigDecimal(value);
notEmpty(value, msg);
isTrue(NumberUtil.isGreater(valueBigDecimal, BigDecimal.ZERO), msg);
}
/**
* 断言大于等于0
*/
public static void gteZero(Object value, String msg) {
BigDecimal valueBigDecimal = Convert.toBigDecimal(value);
notEmpty(value, msg);
isTrue(NumberUtil.isGreaterOrEqual(valueBigDecimal, BigDecimal.ZERO), msg);
}
/**
* 断言小于0
*/
public static void ltZero(Object value, String msg) {
BigDecimal num = Convert.toBigDecimal(value);
notEmpty(value, msg);
isTrue(NumberUtil.isLess(num, BigDecimal.ZERO), msg);
}
/**
* 断言小于等于0
*/
public static void lteZero(Object value, String msg) {
BigDecimal num = Convert.toBigDecimal(value);
notEmpty(value, msg);
isTrue(NumberUtil.isLessOrEqual(num, BigDecimal.ZERO), msg);
}
/**
* 断言value1 小于等于 value2
*/
public static void lte(Object value1, Object value2, String msg) {
BigDecimal big1 = Convert.toBigDecimal(value1);
BigDecimal big2 = Convert.toBigDecimal(value2);
isTrue(NumberUtil.isLessOrEqual(big1, big2), msg);
}
/**
* 断言value1小于value2
*/
public static void lt(Object value1, Object value2, String msg) {
BigDecimal big1 = Convert.toBigDecimal(value1);
BigDecimal big2 = Convert.toBigDecimal(value2);
isTrue(NumberUtil.isLess(big1, big2), msg);
}
/**
* 断言value1 >= value2
*/
public static void gte(Object value1, Object value2, String msg) {
BigDecimal big1 = Convert.toBigDecimal(value1);
BigDecimal big2 = Convert.toBigDecimal(value2);
isTrue(NumberUtil.isGreaterOrEqual(big1, big2), msg);
}
/**
* 断言value1 > value2
*/
public static void gt(Object value1, Object value2, String msg) {
BigDecimal big1 = Convert.toBigDecimal(value1);
BigDecimal big2 = Convert.toBigDecimal(value2);
isTrue(NumberUtil.isGreater(big1, big2), msg);
}
/**
* 断言两者相同
*/
public static void equal(Object obj1, Object obj2, String msg) {
isTrue(ObjectUtil.equal(obj1, obj2), msg);
}
/**
* 断言两者不相同
*/
public static void notEqual(Object obj1, Object obj2, String msg) {
isTrue(ObjectUtil.notEqual(obj1, obj2), msg);
}
/**
* 断言两者相同
*/
public static void equalsAnyIgnoreCase(String value, String[] array, String msg) {
isTrue(StrUtil.equalsAnyIgnoreCase(value, array), msg);
}
/**
* 断言小于等于max
*/
public static void max(Number value, int max, String msg) {
notEmpty(value, msg);
double valueDouble = value.doubleValue();
isTrue(valueDouble <= max, msg);
}
/**
* 断言小于等于max
*/
public static void min(Number value, int min, String msg) {
notEmpty(value, msg);
double valueDouble = value.doubleValue();
isTrue(valueDouble >= min, msg);
}
/**
* 断言value长度在指定范围或等于
*/
public static void maxSize(Object value, int max, String msg) {
int valueLength = ObjectUtil.length(value);
isTrue(valueLength <= max, msg);
}
/**
* 断言value长度在指定范围或等于
*/
public static void minSize(Object value, int min, String msg) {
int valueLength = ObjectUtil.length(value);
isTrue(valueLength >= min, msg);
}
/**
* 断言value是否在指定范围内或等于
*/
public static void betweenOrEquals(Object value, Object min, Object max, String msg) {
double valueDouble = Convert.toDouble(value);
double minDouble = Convert.toDouble(min);
double maxDouble = Convert.toDouble(max);
notEmpty(value, "目标值不能为空");
notEmpty(min, "最小值不能为空");
notEmpty(max, "最大值不能为空");
isTrue(minDouble <= valueDouble && maxDouble >= valueDouble, msg);
}
/**
* 断言是否为真
*/
public static void isTrue(boolean flag, String msg) {
isFalse(!flag, msg);
}
/**
* 断言是否为假
*/
public static void isFalse(boolean flag, String msg) {
if (flag == false) {
return;
}
throw new BizException(msg);
}
}
public class BizException extends RuntimeException {
public BizException() {
}
public BizException(String message) {
super(message);
}
public BizException(String message, Throwable cause) {
super(message, cause);
}
}
最后
以上就是朴素往事为你收集整理的AssertUtil断言工具类的全部内容,希望文章能够帮你解决AssertUtil断言工具类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复