验证类-各种判断

package com.ynkbny.config;

import com.ynkbny.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @title 验证类
 * @description 验证类
 */
public class JposAssert {

    private static final Logger logger = LoggerFactory.getLogger(JposAssert.class);

    /**
     * 验证手机号是否正确
     *
     * @param phone   手机号
     * @param field   字段名
     * @param message 错误信息
     * @throws BusinessException 异常
     */
    public static void hasMobile(String phone, String field, String message) throws BusinessException {
        if (!mobile(phone)) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 验证手机号是否正确
     *
     * @param phone   手机号
     * @param message 错误信息
     * @throws BusinessException 异常
     */
    public static void hasMobile(String phone, String message) throws BusinessException {
        hasMobile(phone, null, message);
    }

    /**
     * 判断 obj 参数是否为null,如果不为null则抛异常
     *
     * @param obj     对象
     * @param field   字段名
     * @param message 异常信息
     * @throws BusinessException 异常
     */
    public static void notNull(Object obj, String field, String message) throws BusinessException {
        if (obj != null) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断 obj 参数是否为null,如果不为null则抛异常
     *
     * @param obj     对象
     * @param message 异常信息
     * @throws BusinessException 异常
     */
    public static void notNull(Object obj, String message) throws BusinessException {
        notNull(obj, null, message);
    }

    /**
     * 判断 obj 参数是否为null,如果不为null则抛异常
     *
     * @param obj     对象
     * @param field   字段名
     * @param message 异常信息
     * @throws BusinessException 异常
     */
    public static void isNull(Object obj, String field, String message) throws BusinessException {
        if (obj == null) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断 obj 参数是否为null,如果为null则抛异常
     *
     * @param obj     对象
     * @param message 异常信息
     * @throws BusinessException 异常
     */
    public static void isNull(Object obj, String message) throws BusinessException {
        isNull(obj, null, message);
    }

    /**
     * 判断str是否为null,或“”、“  ”,如果是则抛异常
     *
     * @param str     字符串
     * @param field   字段名
     * @param message 错误信息
     * @throws BusinessException 异常
     */
    public static void hasText(String str, String field, String message) throws BusinessException {
        if (str == null || "".equals(str.trim())) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断str是否为null,或“”、“  ”,如果是则抛异常
     *
     * @param str     字符串
     * @param message 错误信息
     * @throws BusinessException 异常
     */
    public static void hasText(String str, String message) throws BusinessException {
        hasText(str, null, message);
    }

    /**
     * 判断是否为true,如果为false则抛异常
     *
     * @param b       判断boolean
     * @param field   字段名
     * @param message 错误结果
     * @throws BusinessException 异常
     */
    public static void isTrue(boolean b, String field, String message) throws BusinessException {
        if (!b) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断是否为true,如果为false则抛异常
     *
     * @param b       判断boolean
     * @param message 错误结果
     * @throws BusinessException 异常
     */
    public static void isTrue(boolean b, String message) throws BusinessException {
        isTrue(b, null, message);
    }

    /**
     * 正则验证
     *
     * @param text    文字
     * @param regex   正则
     * @param message 消息
     * @throws BusinessException 异常
     */
    public static void verifyRegex(String text, String regex, String message) throws BusinessException {
        verifyRegex(text, regex, null, message);
    }

    /**
     * 正则验证
     *
     * @param text    文字
     * @param regex   正则
     * @param field   字段名
     * @param message 消息
     * @throws BusinessException 异常
     */
    public static void verifyRegex(String text, String regex, String field, String message) throws BusinessException {
        if (!regex(text, regex)) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断是不是手机号码
     *
     * @param phone
     * @return 如果是手机号码 返回true 否则返回false
     */
    public static boolean mobile(String phone) {
        return regex(phone, "^1[2|3|4|5|6|7|8|9]\\d{9}$");
    }

    /**
     * 正则验证
     *
     * @param text  待验证的文字
     * @param regex 正则表达式
     * @return
     */
    public static boolean regex(String text, String regex) {
        if (text == null) {
            return false;
        }
        return text.matches(regex);
    }

    /**
     * 判断compare是否等于target, 若不相等,抛出message异常
     *
     * @param target  目标对象
     * @param compare 进行比较的对象
     * @param message 错误信息
     * @throws BusinessException 异常
     */
    public static void hasEquals(Object target, Object compare, String message) throws BusinessException {
        if (target == null) {
            throw new IllegalArgumentException("target is null");
        }
        if (!target.equals(compare)) {
            throw new BusinessException(500,message);
        }
    }

    /**
     * 比较两个数据是否相同
     *
     * @param source
     * @param target
     * @return 相同返回true,否则返回false  如果两个字符都是null 也是true
     */
    public static boolean compare(Object source, Object target) {
        if (source == null && target == null) {
            return true;
        } else if (source == null && target != null) {
            return false;
        } else if (source != null && target == null) {
            return false;
        } else {
            return source.equals(target);
        }
    }

    /**
     * 判断compare是否等于target, 若相等,抛出message异常
     *
     * @param target  目标对象
     * @param compare 进行比较的对象
     * @param message 错误信息
     * @throws BusinessException 异常
     */
    public static void notEquals(Object target, Object compare, String message) throws BusinessException {
        if (target == null) {
            throw new IllegalArgumentException("target is null");
        }
        if (target.equals(compare)) {
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断 text 文本长度,如果长度不等,则报错
     *
     * @param text    文本
     * @param length  预计差度过呢
     * @param field   字段名
     * @param message 错误信息
     */
    public static void equalsLength(String text, int length, String field, String message) throws BusinessException {
        if (text == null) {
            if (field != null) {
                errlog(field, "is null");
            }
            throw new IllegalArgumentException("text is null");
        }
        if (text.length() != length) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断 text 文本长度,如果长度不等,则报错
     *
     * @param text    文本
     * @param length  预计差度过呢
     * @param message 错误信息
     */
    public static void equalsLength(String text, int length, String message) throws BusinessException {
        equalsLength(text, length, null, message);
    }


    /**
     * 查看数组arr是否包含obj对象,如果不存在,抛出message错误
     *
     * @param arr     数组
     * @param obj     查找对象
     * @param field   字段名
     * @param message 错误信息
     * @throws BusinessException 异常
     */
    public static void contains(Object[] arr, Object obj, String field, String message) throws BusinessException {
        if (indexOf(arr, obj) == -1) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 查看数组arr是否包含obj对象,如果不存在,抛出message错误
     *
     * @param arr     数组
     * @param obj     查找对象
     * @param message 错误信息
     * @throws BusinessException 异常
     */
    public static void contains(Object[] arr, Object obj, String message) throws BusinessException {
        contains(arr, obj, null, message);
    }

    /**
     * 判断数组arr是否包含指定对象,如果包含,返回对象所在数组的索引,如果不存在,返回-1
     *
     * @param arr 数组
     * @param obj 包含的对象
     * @return 对象obj在数组arr中的下表    若为-1:不包含obj对象
     */
    public static int indexOf(Object[] arr, Object obj) {
        if (arr == null) {
            return -1;
        }
        for (int i = 0; i < arr.length; i++) {
            Object object = arr[i];
            if (object.equals(obj)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 打印字段异常
     *
     * @param field   字段名
     * @param message 异常原因
     */
    private static void errlog(String field, String message) {
        if (logger.isErrorEnabled()) {
            logger.error("字段:{}异常,异常原因为:{}", field, message);
        }
    }

    /**
     * 判断是否是纯数字
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        if (str.length() > 0) {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(str);
            if (!isNum.matches()) {
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 判断 List 参数是否为 空,如果不为 空 则抛异常
     *
     * @param list     集合
     * @param field   字段名
     * @param message 异常信息
     * @throws BusinessException 异常
     */
    public static void notListNull(List list, String field, String message) throws BusinessException {
        if (list.size() > 0) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断 List 参数是否为 空 ,如果 不为 空 则抛异常
     *
     * @param list     集合
     * @param message 异常信息
     * @throws BusinessException 异常
     */
    public static void notListNull(List list, String message) throws BusinessException {
        notListNull(list, null, message);
    }

    /**
     * 判断 List 参数是否为 空,如果 为 空 则抛异常
     *
     * @param list     集合
     * @param field   字段名
     * @param message 异常信息
     * @throws BusinessException 异常
     */
    public static  void isListNull(List list, String field, String message) throws BusinessException {
        if (list.size() <= 0) {
            if (field != null) {
                errlog(field, message);
            }
            throw new BusinessException(500,message);
        }
    }

    /**
     * 判断 List 参数是否为 空 ,如果  空 则抛异常
     *
     * @param list     集合
     * @param message 异常信息
     * @throws BusinessException 异常
     */
    public static  void isListNull(List list, String message) throws BusinessException {
        isListNull(list, null, message);
    }
}
验证手机号是否正确

判断 obj 参数是否为null,如果不为null则抛异常

判断 obj 参数是否为null,如果为null则抛异常

判断str是否为null,或“”、“  ”,如果是则抛异常

判断是否为true,如果为false则抛异常

正则验证

判断compare是否等于target, 若不相等,抛出message异常

判断compare是否等于target, 若相等,抛出message异常

判断 text 文本长度,如果长度不等,则报错

查看数组arr是否包含obj对象,如果不存在,抛出message错误

判断是否是纯数字

判断 List 参数是否为 空,如果不为 空 则抛异常

判断 List 参数是否为 空,如果 为 空 则抛异常

你可能感兴趣的:(笔记,windows,开发语言,java)