Skip to content
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Commit 6d84be0

Browse files
committed
optimize checker for bean
1 parent c429f65 commit 6d84be0

File tree

3 files changed

+12
-57
lines changed

3 files changed

+12
-57
lines changed

src/main/java/com/zhazhapan/util/Checker.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public static <T> CheckResult<T> checkBean(Object bean, Map<String, Object> map)
280280
CheckResult<T> result = new CheckResult<>();
281281
if (isNull(bean)) {
282282
result.passed = false;
283-
result.resultObject = result.errorResult();
283+
result.resultObject = CheckResult.getErrorResult();
284284
return result;
285285
}
286286
Field[] fields = bean.getClass().getDeclaredFields();

src/main/java/com/zhazhapan/util/model/CheckResult.java

+5-56
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class CheckResult<T> {
2626
*
2727
* @since 1.1.0
2828
*/
29-
public static ResultObject getErrorResult(int code, String message) {
30-
return new ResultObject(code, message, ValueConsts.ERROR_EN);
29+
public static <T> ResultObject<T> getErrorResult(int code, String message) {
30+
return new ResultObject<>(code, message, ValueConsts.ERROR_EN);
3131
}
3232

3333
/**
@@ -37,7 +37,7 @@ public static ResultObject getErrorResult(int code, String message) {
3737
*
3838
* @since 1.1.0
3939
*/
40-
public static ResultObject getErrorResult() {
40+
public static <T> ResultObject<T> getErrorResult() {
4141
return getErrorResult(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE);
4242
}
4343

@@ -50,7 +50,7 @@ public static ResultObject getErrorResult() {
5050
*
5151
* @since 1.1.0
5252
*/
53-
public static ResultObject getErrorResult(int code) {
53+
public static <T> ResultObject<T> getErrorResult(int code) {
5454
return getErrorResult(code, DEFAULT_ERROR_MESSAGE);
5555
}
5656

@@ -63,58 +63,7 @@ public static ResultObject getErrorResult(int code) {
6363
*
6464
* @since 1.1.0
6565
*/
66-
public static ResultObject getErrorResult(String message) {
66+
public static <T> ResultObject<T> getErrorResult(String message) {
6767
return getErrorResult(DEFAULT_ERROR_CODE, message);
6868
}
69-
70-
/**
71-
* 获取失败的结果
72-
*
73-
* @param code 错误码
74-
* @param message 消息
75-
*
76-
* @return {@link ResultObject}
77-
*
78-
* @since 1.1.1
79-
*/
80-
public ResultObject<T> errorResult(int code, String message) {
81-
return new ResultObject<>(code, message, ValueConsts.ERROR_EN);
82-
}
83-
84-
/**
85-
* 获取失败的结果
86-
*
87-
* @return {@link ResultObject}
88-
*
89-
* @since 1.1.1
90-
*/
91-
public ResultObject<T> errorResult() {
92-
return errorResult(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE);
93-
}
94-
95-
/**
96-
* 获取失败的结果
97-
*
98-
* @param code 错误码
99-
*
100-
* @return {@link ResultObject}
101-
*
102-
* @since 1.1.1
103-
*/
104-
public ResultObject<T> errorResult(int code) {
105-
return errorResult(code, DEFAULT_ERROR_MESSAGE);
106-
}
107-
108-
/**
109-
* 获取失败的结果
110-
*
111-
* @param message 消息
112-
*
113-
* @return {@link ResultObject}
114-
*
115-
* @since 1.1.1
116-
*/
117-
public ResultObject<T> errorResult(String message) {
118-
return errorResult(DEFAULT_ERROR_CODE, message);
119-
}
12069
}

src/test/java/com/zhazhapan/util/CheckerTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ public void isNotIn1() {
306306

307307
@Test
308308
public void checkBean() {
309+
// 测试NULL
310+
CheckResult<TestBean> errorResult = Checker.checkBean(null);
311+
assert !errorResult.passed;
312+
309313
TestBean bean = new TestBean();
310314
bean.none = false;
311315

@@ -346,6 +350,8 @@ public void checkBean() {
346350
result = Checker.checkBean(bean);
347351
System.out.println(JSONObject.toJSON(result.resultObject));
348352
assert result.passed;
353+
354+
System.out.println(errorResult.resultObject.setData(bean));
349355
System.out.println(result.resultObject.setData(bean));
350356
}
351357

0 commit comments

Comments
 (0)