czt
2024-07-13 bd01ba57ed8526a0ee94a50e1f52034251bb54f6
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
package com.ld.igds.util;
 
import com.ld.igds.exception.BaseException;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.util.Collection;
import java.util.Map;
 
/**
 * @author: andy.jia
 * @description: 自定义工具类调整抛出异常信息,在异常中支持添加outId
 * @date:2019.03.06
 **/
public abstract class AssertUtil extends Assert {
 
 
    /**
     * @param object
     * @param message
     * @param outId
     * @throws BaseException
     */
    public static void isNull(Object object, String message, String outId) {
        if (object != null) {
            throw new BaseException(message, outId);
        }
    }
 
    /**
     * Assert that an object is not {@code null}.
     * <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre>
     *
     * @param object  the object to check
     * @param message the exception message to use if the assertion fails
     * @throws IllegalArgumentException if the object is {@code null}
     */
    public static void notNull(Object object, String message, String outId) {
        if (object == null) {
            throw new BaseException(message, outId);
        }
    }
 
    /**
     * Assert that the given String contains valid text content; that is, it must not
     * be {@code null} and must contain at least one non-whitespace character.
     * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre>
     *
     * @param text    the String to check
     * @param message the exception message to use if the assertion fails
     * @throws IllegalArgumentException if the text does not contain valid text content
     * @see StringUtils#hasText
     */
    public static void hasText(String text, String message, String outId) {
        if (!StringUtils.hasText(text)) {
            throw new BaseException(message, outId);
        }
    }
 
    /**
     * Assert that a collection contains elements; that is, it must not be
     * {@code null} and must contain at least one element.
     * <pre class="code">Assert.notEmpty(collection, "Collection must contain elements");</pre>
     *
     * @param collection the collection to check
     * @param message    the exception message to use if the assertion fails
     * @throws IllegalArgumentException if the collection is {@code null} or
     *                                  contains no elements
     */
    public static void notEmpty(Collection<?> collection, String message, String outId) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BaseException(message, outId);
        }
    }
 
    /**
     * Assert that a Map contains entries; that is, it must not be {@code null}
     * and must contain at least one entry.
     * <pre class="code">Assert.notEmpty(map, "Map must contain entries");</pre>
     *
     * @param map     the map to check
     * @param message the exception message to use if the assertion fails
     * @throws IllegalArgumentException if the map is {@code null} or contains no entries
     */
    public static void notEmpty(Map<?, ?> map, String message, String outId) {
        if (CollectionUtils.isEmpty(map)) {
            throw new BaseException(message, outId);
        }
    }
}