12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package cn.com.ty.lift.common.verify;
- import cn.hutool.core.util.ObjectUtil;
- import java.util.Objects;
- /**
- * <p>
- * 条件判断,如果不符合条件,抛出异常
- * </p>
- * @author wcz
- * @since 2020/1/9 8:46
- */
- public interface Validate {
- static ValidateException validateException(String message){
- return new ValidateException(message);
- }
- /**
- * 判断表达式是否为真,如果为假,直接抛出异常,返回message
- *
- * @param expression 需要判断的布尔表达式
- * @param message 抛出异常的消息
- */
- static void isTrue(boolean expression, String message) {
- if (!expression) {
- throw validateException(message);
- }
- }
- /**
- * 判断表达式是否为真,如果为真,直接抛出异常,返回message
- *
- * @param expression 需要判断的布尔表达式
- * @param message 抛出异常的消息
- */
- static void notTrue(boolean expression, String message) {
- if (expression) {
- throw validateException(message);
- }
- }
- /**
- * 判断对象object是否为空,如果不为空,直接抛出异常,返回message
- *
- * @param object 参数值
- * @param message 抛出异常的消息
- */
- static void isNull(Object object, String message) {
- isTrue(Objects.isNull(object), message);
- }
- /**
- * 判断对象object是否为空,如果为真,直接抛出异常,返回message
- *
- * @param object 参数值
- * @param message 抛出异常的消息
- */
- static void notNull(Object object, String message) {
- isTrue(Objects.nonNull(object), message);
- }
- static void between(int value, int min, int max, String message){
- isTrue(value >= min && value <= max, message);
- }
- static void notBetween(int value, int min, int max, String message){
- isTrue(value < min || value > max, message);
- }
- static void equals(int one, int other, String message){
- isTrue(one == other, message);
- }
- static void notEquals(int one, int other, String message){
- isTrue(one != other, message);
- }
- static void greater(int value, int bounds, String message){
- isTrue(value > bounds, message);
- }
- static void less(int value, int bounds, String message){
- isTrue(value < bounds, message);
- }
- static void notEmpty(Object object, String message) {
- isTrue(ObjectUtil.isNotEmpty(object), message);
- }
- static void isEmpty(Object object, String message) {
- isTrue(ObjectUtil.isEmpty(object), message);
- }
- static void isBlank(CharSequence cs, String message) {
- isTrue(Objects.isNull(cs) || cs.toString().trim().isEmpty(), message);
- }
- static void notBlank(CharSequence cs, String message) {
- isTrue(Objects.nonNull(cs) && cs.toString().trim().length() > 0, message);
- }
- }
|