入参校验工具类

package com.utils;


import com.isstech.iot.devicelink.process.exception.ArgumentException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author
 * @FileName
 * @create 2017-07-07 8:40
 * @description 校验数据工具类
 * @modifier
 * @modify Time
 * @modify desc
 */
public class ArgumentUtil {
    private static final Logger logger = LoggerFactory.getLogger(ArgumentUtil.class);

    /**
     * 校验字符串是否为空,是否超长
     * @param name 字符串
     * @param length 限制长度
     */
    public static void checkStringIsEmptyOrExceedingLen(String name,int length,String desc) {
        logger.info("arg:"+name+"==length:"+length+"==desc:"+desc);
        //校验是否为空
        if (name == null || name.equals("")) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc)+"==name"+name+"==length:"+length+"==desc:"+desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }

        //校验是否超出长度限制
        if (length > 0 ) {
            if (name.length() > length) {
                logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc)+"==name"+name+"==length:"+length+"==desc:"+desc);
                throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
            }
        }
    }

    /**
     * 校验字符串是否超长
     * @param name 字符串
     * @param length 限制长度
     * @param desc 描述
     */
    public static void checkStringExceedingLen(String name , int length,String desc){
        logger.info("arg:"+name+"==length:"+length+"==desc:"+desc);
        //校验是否超出长度限制
        if (length > 0 ) {
            if (name != null || !name.equals("")) {
                if (name.length() > length) {
                    logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc)+"==name"+name+"==length:"+length+"==desc:"+desc);
                    throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
                }
            }
        }
    }



    /**
     * 校验集合不为null
     * @param list 集合
     * @param desc 字段描述
     */
    public static void checkListIsNotNull(List list,String desc) {
        logger.info("List:" + list + "==desc:" + desc);
        if (null == list || list.size() == 0) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc)+"==List:" + list + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }


    /**
     * 校验集合不为null
     * @param arrays 集合
     * @param desc 字段描述
     */
    public static void checkStringArrayIsNotNull(String[] arrays, String desc) {
        logger.info("List:" + arrays.toString() + "==desc:" + desc);
        if (null == arrays || arrays.length == 0) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc)+"==List:" + arrays.toString() + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }


    /**
     * 校验 int类型参数的最大值和最小值
     * @param min 最小值
     * @param max 最大值
     * @param arg 参数
     * @param desc 描述
     */
    public static void checkIntegerMaxMin(Integer arg, int min, int max, String desc) {
        logger.info("arg:" + arg + "==min:" + min + "==max:" + max + "==desc:" + desc);
        if (null == arg || arg < min || arg > max) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc)+"==arg:" + arg + "==min:" + min + "==max:" + max + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }

    /**
     * 校验 int类型参数的最小值
     * @param min 最小值
     * @param arg 参数
     * @param desc 描述
     */
    public static void checkIntegerMin(Integer arg, int min, String desc){
        logger.info("arg:" + arg + "==min:" + min  + "==desc:" + desc);
        if (null == arg || arg < min) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc)+"==arg:" + arg + "==min:" + min + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }

    /**
     * 校验 字符串参数非空
     * @param arg 参数
     * @param desc 描述
     */
    public static void checkStringIsEmpty(String arg, String desc) {
        logger.info("arg:" + arg + "==desc:" + desc);
        if (StringUtils.isBlank(arg)) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==arg:" + arg + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }


    /**
     * 校验 字符串参数非空
     * @param arg 参数
     */
    public static boolean checkStringIsEmpty(String arg) {
        logger.info("arg:" + arg );
        if (StringUtils.isBlank(arg)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 非NULL校验
     * @param arg 参数
     * @param desc 描述
     */
    public static void checkIntegerIsNotNull(Integer arg, String desc) {
        logger.info("arg:" + arg + "==desc:" + desc );
        if (arg == null || arg < 0) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==arg:" + arg + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }

    /**
     * 非NULL校验
     * @param arg 参数
     */
    public static boolean checkIntegerIsNotNull(Integer arg) {
        logger.info("arg:" + arg);
        if (arg == null || arg <= 0) {
            return false;
        }
        return true;
    }

    /**
     * 非NULL校验
     * @param arg 参数
     * @param desc 描述
     */
    public static void checkDoubleIsNotNull(Double arg, String desc) {
        logger.info("arg:" + arg + "==desc:" + desc );
        if (arg == null || arg < 0) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==arg:" + arg + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }

    /**
     * 非NULL校验
     * @param arg 参数
     */
    public static boolean checkDoubleIsNotNull(Double arg) {
        logger.info("arg:" + arg);
        if (arg == null || arg <= 0) {
            return false;
        }
        return true;
    }


    /**
     * 校验 UUID
     * @param arg 参数
     * @param desc 描述
     */
    public static void checkUUID(String arg, String desc) {
        logger.info("arg:" + arg + "==desc:" + desc );
        if (arg == null || arg.length() != 32) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==arg:" + arg + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }

    /**
      * @description: TODO 验证主键id长度:18
      * @param String,String
      * @throws ArgumentException
      * @author
      * @date 2021/11/29 11:26
      */
    public static void checkPKeyLenth(String arg, String desc) {
        logger.info("arg:" + arg + "==desc:" + desc );
        if (arg == null || "".equals(arg)) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==arg:" + arg + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }

    /**
       * @description: TODO 检查字符串长度是否在范围内
       * @param String,String
       * @throws ArgumentException
       * @author
       * @date 2021/11/29 11:26
       */
    public static void checkStringLenth(String arg, int minLenth,int maxLength,String desc) {
        logger.info("arg:" + arg + "==desc:" + desc );
        if (arg == null || arg.length() < minLenth || arg.length()>maxLength) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==arg:" + arg + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }

    /**
     * 非NULL校验
     * @param arg 参数
     * @param desc 描述
     */
    public static void checkObjIsNotNull(Object arg, String desc) {
        logger.info("arg:" + arg + "==desc:" + desc );
        if (arg == null) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==arg:" + arg + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }


    /**
     * 非NULL校验
     * @param arg 参数
     * @param desc 描述
     */
    public static void checkLongIsNotNull(Long arg, String desc) {
        logger.info("arg:" + arg + "==desc:" + desc );
        if (arg == null || arg.intValue() < 0) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==arg:" + arg + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }

    /**
     * 非NULL校验
     * @param arg 参数
     */
    public static boolean checkLongIsNotNull(Long arg) {
        logger.info("arg:" + arg);
        if (arg == null || arg.intValue () <= 0) {
            return false;
        }
        return true;
    }

    /**
     * 检查是否是整数
     * @param numStr
     * @param desc
     */
    public static void checkIfIsInteger(String numStr, String desc) {
        logger.info("numStr:" + numStr + "==desc:" + desc );
        if(!StringUtils.isNumeric(numStr)){
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==numStr:" + numStr + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }


    /**
     * 校验 字符串参数是否在范围内
     * @param arg 参数
     * @param desc 描述
     * @param range 值范围
     */
    public static void checkStringRange(String arg, String desc, String... range) {
        logger.info("arg:" + arg + "==desc:" + desc + "==rang:" + range);
        if (arg == null || StringUtils.isBlank(arg)) {
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "参数")+"==arg:" + arg + "==range:" + range);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "参数"));
        }

        for (String value : range) {
            if(value.equals(arg)) {
                return;
            }
        }
        logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc)+"==arg:" + arg + "==desc:" + desc + "==rang:" + range);
        throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
    }

    /**
     * 判断对象是否为空
     * @param obj
     * @return
     */
    public static boolean checkObjIsEmpty(Object obj){
        logger.info("obj:" + obj.getClass().getName());
        if(obj == null){
            return true;
        }else if(obj instanceof String){
            return ((String) obj).isEmpty();
        }else if(obj instanceof Collection){
            return ((Collection) obj).isEmpty();
        }else{
            return false;
        }
    }

    /**
     * 判断对象是否为空
     * @param obj
     * @return
     */
    public static void checkObjIsEmpty(Object obj, String desc){
        logger.info("obj:" + obj.getClass().getName());
        if(obj == null){
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }else if(obj instanceof String  && ((String) obj).isEmpty()){
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }else if(obj instanceof Collection && ((Collection) obj).isEmpty()){
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc));
        }
    }
    /**
      * @description: TODO 检查字符串是否含有特殊字符
      * @param String String
      * @throws
      * @author
      * @date 2021/11/29 15:45
      */
    public static void checkStringHasSpecialChar(String str,String desc) {
        logger.info("str:" + str + "==desc:" + desc);
        String regEx = "[`~!@#$%^&*()\\+=<>?:\"{}|,.\\/;'\\\\[\\]·~!@#¥%……&*()\\+={}|《》?:“”【】、;‘’,。、]]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        if(m.find()){
            logger.warn(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, desc) + "==str:" + str + "==desc:" + desc);
            throw new ArgumentException(MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "参数"));
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容