Args 工具类的使用(参数判断)

配合全局异常处理,遇到不能容忍的错误就往外抛

notNull:字段判空

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public JSONObject useCoupon(String coupon_code, JSONObject in) {
// 1、获取参数
String openid = in.getByPath("openid", String.class);
String appid = in.getByPath("appid", String.class);

// 2、非空判断
Args.notNull(coupon_code, "URL 'coupon_code'");
Args.notNull(openid, "requestBody 'openid'");
Args.notNull(appid, "requestBody 'appid'");

// 3、根据 coupon_code 找到 stockId
CouponEntity couponEntity = couponDao.selectOne(new LambdaQueryWrapper<CouponEntity>()
.eq(CouponEntity::getCode, coupon_code));
Args.notNull(couponEntity, String.format("'coupon_code' %s 未找到对应的 stockId", coupon_code));

notEmpty:集合判空

1
2
List<JSONObject> coupon_list = coupon.toList(JSONObject.class);
Args.notEmpty(coupon_list, "'coupon.[]'");

check:校验

1
2
3
4
5
6
7
public int updatePrice(String orderId) {
// 1、校验参数
Args.notNull(orderId, "'order_id'");

// 2、查询数据
OrderEntity orderEntity = baseMapper.selectById(orderId);
Args.check(orderEntity != null, "'order_id' 在数据库中不存在~");

positive:不能为负

1
2
3
/* 1、检查 非数组字段 */
Integer order_amount = in.getByPath("order_amount", Integer.class);// 订单总金额,单位为分
Args.positive(order_amount, "'order_amount'");

check:枚举校验

1
2
3
4
5
6
7
8
9
// 2.3 检查字段
String coupon_status = c.getByPath("coupon_status", String.class);// SENDED已发送,但未使用, USED已使用,EXPIRED已到期,EXCHANGED券到时间未支付,系统再退回券
Integer coupon_type = c.getByPath("coupon_type", Integer.class);// 1: 支付券 2 商家券
Integer to_type = c.getByPath("to_type", Integer.class);// 1 :计入 2 :不计入

// 检查 参数合法性。
EnumUtils.check(Constant.CouponStatus.class, coupon_status, "coupon.[].coupon_status");
EnumUtils.checkFields(Constant.ToType.class, "num", to_type, "coupon.[].to_type");
EnumUtils.checkFields(Constant.CouponType.class, "num", coupon_type, "coupon.[].coupon_type");
Constant.java
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
package com.decathlon.easypromo.util;

import lombok.Getter;

/**
* 常量
*
* @author 陶攀峰
*/
public class Constant {

/**
* TO 类型。
* 1 Record to TO,2 Do not record to TO【平安=1,默认=2】
*
* @author 陶攀峰
* @date 2021/3/24 上午9:51
*/
@Getter
public enum ToType {
RECORD_TO(1),
NOT_RECORD_TO(2);

private int num;

ToType(int num) {
this.num = num;
}
}

/**
* 优惠卷四种状态
*
* @author 陶攀峰
* @date 2021/3/24 上午9:37
*/
public enum CouponStatus {
SENDED,//已发送,但未使用
USED,//已使用
EXPIRED,//已到期
EXCHANGED,//券到时间未支付,系统再退回券
}

/**
* 优惠卷类型
*
* @author 陶攀峰
* @date 2021/4/9 下午3:10
*/
@Getter
public enum CouponType {
PAYMENT(1),// 支付券
MERCHANT(2);// 商家券

private int num;

CouponType(int num) {
this.num = num;
}
}
}
EnumUtils.java
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
package com.decathlon.easypromo.util;

import cn.hutool.core.util.EnumUtil;
import org.apache.http.util.Args;

import java.util.List;

/**
* 枚举工具类
*
* @author 陶攀峰
* @version 1.0
* @date 2021/4/9 下午2:46
*/
public class EnumUtils {

/**
* 传入一个值 var,检查是否存在枚举列表 clazz 中
*
* @param clazz 枚举类的 class 对象
* @param var 传入参数
* @param paramName 参数名
* @author 陶攀峰
* @date 2021/4/9 下午2:52
*/
public static void check(Class clazz, Object var, String paramName) {
Args.check(EnumUtil.contains(clazz, var.toString().toUpperCase()), "'%s' should in %s", paramName, EnumUtil.getNames(clazz));
}

/**
* 传入一个值 var,检查是否存在枚举类的属性 fieldName 列表中
*
* @param clazz 枚举类的 class 对象
* @param fieldName 枚举类成员属性名称
* @param var 传入参数
* @param paramName 参数名
* @author 陶攀峰
* @date 2021/4/9 下午4:59
*/
public static void checkFields(Class clazz, String fieldName, Object var, String paramName) {
List<Object> fieldValues = EnumUtil.getFieldValues(clazz, fieldName);
Args.check(fieldValues.contains(var), "'%s' should in %s", paramName, fieldValues);
}

}

Args:源码

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
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
99
100
101
102
103
104

package org.apache.http.util;

import java.util.Collection;

public class Args {

public static void check(final boolean expression, final String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
}

public static void check(final boolean expression, final String message, final Object... args) {
if (!expression) {
throw new IllegalArgumentException(String.format(message, args));
}
}

public static void check(final boolean expression, final String message, final Object arg) {
if (!expression) {
throw new IllegalArgumentException(String.format(message, arg));
}
}

public static <T> T notNull(final T argument, final String name) {
if (argument == null) {
throw new IllegalArgumentException(name + " may not be null");
}
return argument;
}

public static <T extends CharSequence> T notEmpty(final T argument, final String name) {
if (argument == null) {
throw new IllegalArgumentException(name + " may not be null");
}
if (TextUtils.isEmpty(argument)) {
throw new IllegalArgumentException(name + " may not be empty");
}
return argument;
}

public static <T extends CharSequence> T notBlank(final T argument, final String name) {
if (argument == null) {
throw new IllegalArgumentException(name + " may not be null");
}
if (TextUtils.isBlank(argument)) {
throw new IllegalArgumentException(name + " may not be blank");
}
return argument;
}

public static <T extends CharSequence> T containsNoBlanks(final T argument, final String name) {
if (argument == null) {
throw new IllegalArgumentException(name + " may not be null");
}
if (argument.length() == 0) {
throw new IllegalArgumentException(name + " may not be empty");
}
if (TextUtils.containsBlanks(argument)) {
throw new IllegalArgumentException(name + " may not contain blanks");
}
return argument;
}

public static <E, T extends Collection<E>> T notEmpty(final T argument, final String name) {
if (argument == null) {
throw new IllegalArgumentException(name + " may not be null");
}
if (argument.isEmpty()) {
throw new IllegalArgumentException(name + " may not be empty");
}
return argument;
}

public static int positive(final int n, final String name) {
if (n <= 0) {
throw new IllegalArgumentException(name + " may not be negative or zero");
}
return n;
}

public static long positive(final long n, final String name) {
if (n <= 0) {
throw new IllegalArgumentException(name + " may not be negative or zero");
}
return n;
}

public static int notNegative(final int n, final String name) {
if (n < 0) {
throw new IllegalArgumentException(name + " may not be negative");
}
return n;
}

public static long notNegative(final long n, final String name) {
if (n < 0) {
throw new IllegalArgumentException(name + " may not be negative");
}
return n;
}

}