java Controller 基础 校验 类

此篇博客介绍了CheckUtils工具类,用于Java项目中对集合、对象、字符串长度、正则表达式等进行有效校验,以及获取登录用户信息和生成唯一ID。核心功能包括空值检查、长度限制、字符类型验证和业务逻辑集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.xiaobodata.biz.modules.indicatrix.util;

import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.sun.istack.Nullable;
import com.xiaobodata.biz.common.system.api.ISysBaseAPI;
import com.xiaobodata.biz.common.system.vo.DictModel;
import com.xiaobodata.biz.common.system.vo.LoginUser;
import com.xiaobodata.biz.modules.indicatrix.exception.XiaoBoDataException;
import com.xiaobodata.biz.modules.system.service.impl.SysBaseApiImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Classname CheckUtils
 * @Description TODO
 * @Created by 李杰
 * @Date 2022/2/8 10:10
 */
@Component
public class CheckUtils {

    private static ISysBaseAPI sysBaseAPI;
    @Autowired
    public void setSysBaseAPI(ISysBaseAPI sysBaseAPI) {
        CheckUtils.sysBaseAPI = sysBaseAPI;
    }
    /**
     * 判断集合是否为空
     * @param collectionName 集合名称
     * @param collection 集合参数
     */
    public static void checkCollections(@Nullable String collectionName, Collection<?> collection){
        if(CollectionUtils.isEmpty(collection)){
            throw new XiaoBoDataException(collectionName + "为空");
        }
    }

    /**
     * 判断对象是否为空
     */
    public static void checkNotNull(@Nullable String name,Object object){
        if(object == null){
            throw new XiaoBoDataException(name + "为空");
        }
    }

    /**
     * 判断字符串参数是否为空
     * @param paramName 参数名称
     * @param param 参数
     */
    public static void checkString(@Nullable String paramName, String param){
        if(StringUtils.isEmpty(param)){
            throw new XiaoBoDataException(paramName + "参数为空");
        }
    }

    /**
     * 校验字符串长度
     * @param paramName 返回字符串名称
     * @param param 参数
     * @param limit 长度
     */
    public static void checkLength(String paramName,String param,int limit){
        checkString(paramName,param);
        if(param.length() > limit){
            throw new XiaoBoDataException(paramName + "长度超出最大长度" + limit + " ,请重新输入");
        }
    }
    /**
     * 校验字符串长度 不检验为空
     * @param paramName 返回字符串名称
     * @param param 参数
     * @param limit 长度
     */
    public static void checkLengthNotCheckIsNull(String paramName,String param,int limit){
        if(param==null)
            return ;
        if(param.length() > limit){
            throw new XiaoBoDataException(paramName + "长度超出最大长度" + limit + " ,请重新输入");
        }
    }

//    /**
//     * 校验(m-n位)数字 正则不太对
//     * @param content
//     * @return
//     */
//    public static void isMToNFigures(String paramName,String content,Integer m,Integer n){
//        if(!Pattern.matches("^([0-9]{1,3}+\\.[0-9]){"+m+","+n+"}$", content)){
//            throw new XiaoBoDataException(paramName+" 仅支持"+m+"-"+n+"位数字");
//        }
//    }

    /**
     * 校验数字 英文 汉字
     * @param content
     * @return
     */
    public static void isChineseAndEnglishAndFigures(String paramName,String content,Integer lengthStart,Integer lengthEnd){
        if(!Pattern.matches("^[\\u4E00-\\u9FA5A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)){
            throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字");
        }
    }
    /**
     * 校验数字 英文 汉字 _
     * @param content
     * @return
     */
    public static void isChineseAndEnglishAndFiguresAndUnderline(String paramName,String content,Integer lengthStart,Integer lengthEnd){
        if (!Pattern.matches("^[\\u4E00-\\u9FA5_A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
            throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和_");
        }
    }
    /**
     * 校验数字 英文 汉字 ()
     * @param content
     * @return
     */
    public static void isChineseAndEnglishAndFiguresAndHaveBracket(String paramName,String content,Integer lengthStart,Integer lengthEnd){
        if (!Pattern.matches("^[\\u4E00-\\u9FA5()()A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
            throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和{}");
        }
    }
    /**
     * 校验数字 英文 汉字 ,
     * @param content
     * @return
     */
    public static void isChineseAndEnglishAndFiguresAndComma(String paramName,String content,Integer lengthStart,Integer lengthEnd){
        if (!Pattern.matches("^[\\u4E00-\\u9FA5,,A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
            throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和,");
        }
    }

    /**
     * 获取当前登录用户信息
     */
    public static LoginUser getCurrentUser(){
        return (LoginUser) SecurityUtils.getSubject().getPrincipal();
    }

    public static String getId(){
        return IdUtil.createSnowflake(0L,0L).nextIdStr();
    }

    public static void  CheckDictItem(String paramName,String code,String param){
        List<DictModel> dictItems = sysBaseAPI.getDictItems(code);
        if(!dictItems.isEmpty()){
            Set<String> collect = dictItems.stream().map(DictModel::getValue).collect(Collectors.toSet());
            if(!collect.contains(param)){
                throw new XiaoBoDataException("请选择正确的"+paramName);
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值