Skip to content

Commit 3cbba2a

Browse files
authored
Merge pull request #24 from lunasaw/2.5.6
🔖 2.5.6 AssertUtil
2 parents bf07dd1 + 2afc87a commit 3cbba2a

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>io.github.lunasaw</groupId>
88
<artifactId>luna-common</artifactId>
99
<name>luna-common</name>
10-
<version>2.5.5</version>
10+
<version>2.5.6</version>
1111
<description>common is project which contains common utils</description>
1212
<url>https://github.com/lunasaw/luna-common</url>
1313

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.luna.common.check;
2+
3+
import java.util.Collection;
4+
import java.util.Map;
5+
6+
import org.apache.commons.collections4.CollectionUtils;
7+
import org.apache.commons.collections4.MapUtils;
8+
import org.apache.commons.lang3.StringUtils;
9+
10+
import com.luna.common.exception.BaseException;
11+
12+
/**
13+
* 校验辅助类
14+
*
15+
* @author luna
16+
**/
17+
public class AssertUtil {
18+
19+
/**
20+
* 私有构造方法
21+
*/
22+
private AssertUtil() {}
23+
24+
/**
25+
* 校验指定对象不能为null
26+
*
27+
* @param object 对象
28+
* @param baseException 运行时异常
29+
*/
30+
public static void notNull(Object object, BaseException baseException, String... extendInfos) {
31+
isTrue(object != null, baseException, extendInfos);
32+
}
33+
34+
/**
35+
* 校验指定集合不能为空
36+
*
37+
* @param collection 集合
38+
* @param baseException 运行时异常
39+
*/
40+
public static void notEmpty(Collection collection, BaseException baseException,
41+
String... extendInfos) {
42+
isTrue(CollectionUtils.isNotEmpty(collection),
43+
baseException, extendInfos);
44+
}
45+
46+
/**
47+
* 校验指定对象不能为null
48+
*
49+
* @param map map集合
50+
* @param baseException 运行时异常
51+
*/
52+
public static void notEmpty(Map map, BaseException baseException,
53+
String... extendInfos) {
54+
isTrue(MapUtils.isNotEmpty(map), baseException, extendInfos);
55+
}
56+
57+
/**
58+
* 校验字符串不能为空
59+
*
60+
* @param str 字符串
61+
* @param baseException 运行时异常
62+
*/
63+
public static void notBlank(String str, BaseException baseException,
64+
String... extendInfos) {
65+
if (StringUtils.isBlank(str)) {
66+
isTrue(false, baseException, extendInfos);
67+
}
68+
}
69+
70+
/**
71+
* 校验指定条件为true
72+
*
73+
* @param condition 条件
74+
* @param baseException 运行时异常
75+
*/
76+
public static void isFalse(boolean condition, BaseException baseException,
77+
String... extendInfos) {
78+
if (condition) {
79+
fail(baseException, extendInfos);
80+
}
81+
}
82+
83+
/**
84+
* 校验指定条件为true
85+
*
86+
* @param condition 条件
87+
* @param baseException 运行时异常
88+
*/
89+
public static void isTrue(boolean condition, BaseException baseException,
90+
String... extendInfos) {
91+
if (!condition) {
92+
fail(baseException, extendInfos);
93+
}
94+
}
95+
96+
/**
97+
* 抛出指定运行时异常
98+
*
99+
* @param baseException 运行时异常
100+
*/
101+
public static void fail(BaseException baseException, String... extendInfos) {
102+
Assert.notNull(baseException);
103+
104+
throw new BaseException(baseException, extendInfos);
105+
}
106+
}

src/main/java/com/luna/common/exception/BaseException.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@
66
* @author luna
77
*/
88
public class BaseException extends RuntimeException {
9+
public static final BaseException SYSTEM_ERROR = new BaseException(100000, "系统异常");
10+
public static final BaseException UNKNOWN = new BaseException(600000, "系统错误");
11+
public static final BaseException PARAMETER_ERROR = new BaseException(600001, "参数错误");
12+
public static final BaseException MISSING = new BaseException(600002, "信息不存在");
13+
public static final BaseException PARAMETER_OVERFLOW = new BaseException(600003, "参数个数超限");
14+
public static final BaseException REPEAT_OPERATION = new BaseException(600004, "重复操作(幂等校验失败)");
15+
public static final BaseException UN_SUPPORT_ENCRYPT_TYPE = new BaseException(600005, "不支持该数据加密类型");
16+
917
/** 错误码 {@link ResultCode} */
10-
private int code;
18+
private int code;
1119
/** 错误消息 */
12-
private String message;
20+
private String message;
21+
22+
public BaseException(BaseException baseException, String... extendMessage) {
23+
this(baseException.getCode(),
24+
String.format(baseException.getMessage(), extendMessage));
25+
}
1326

1427
public BaseException(int status, String msg, Throwable throwable) {
1528
super(throwable);

0 commit comments

Comments
 (0)