jinlin
2025-03-01 86f02fee03614fef275c6e0c355d73318ca3025e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有,侵权必究!
 */
 
package com.example.server.validator;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import com.example.client.entity.ErrorCode;
import com.example.client.entity.RenException;
import org.apache.commons.lang3.StringUtils;
 
import java.util.List;
import java.util.Map;
 
/**
 * 校验工具类
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
public class AssertUtils {
 
    public static void isBlank(String str, String... params) {
        isBlank(str, ErrorCode.NOT_NULL.getCode(), params);
    }
 
    public static void isBlank(String str, Integer code, String... params) {
        if(code == null){
            throw new RenException(ErrorCode.NOT_NULL.getCode(), "code");
        }
 
        if (StringUtils.isBlank(str)) {
            throw new RenException(code, params);
        }
    }
 
    public static void isNull(Object object, String... params) {
        isNull(object, ErrorCode.NOT_NULL.getCode(), params);
    }
 
    public static void isNull(Object object, Integer code, String... params) {
        if(code == null){
            throw new RenException(ErrorCode.NOT_NULL.getCode(), "code");
        }
 
        if (object == null) {
            throw new RenException(code, params);
        }
    }
 
    public static void isArrayEmpty(Object[] array, String... params) {
        isArrayEmpty(array, ErrorCode.NOT_NULL.getCode(), params);
    }
 
    public static void isArrayEmpty(Object[] array, Integer code, String... params) {
        if(code == null){
            throw new RenException(ErrorCode.NOT_NULL.getCode(), "code");
        }
 
        if(ArrayUtil.isEmpty(array)){
            throw new RenException(code, params);
        }
    }
 
    public static void isListEmpty(List<?> list, String... params) {
        isListEmpty(list, ErrorCode.NOT_NULL.getCode(), params);
    }
 
    public static void isListEmpty(List<?> list, Integer code, String... params) {
        if(code == null){
            throw new RenException(ErrorCode.NOT_NULL.getCode(), "code");
        }
 
        if(CollUtil.isEmpty(list)){
            throw new RenException(code, params);
        }
    }
 
    public static void isMapEmpty(Map map, String... params) {
        isMapEmpty(map, ErrorCode.NOT_NULL.getCode(), params);
    }
 
    public static void isMapEmpty(Map map, Integer code, String... params) {
        if(code == null){
            throw new RenException(ErrorCode.NOT_NULL.getCode(), "code");
        }
 
        if(MapUtil.isEmpty(map)){
            throw new RenException(code, params);
        }
    }
}