forked from shanehan/opt-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptStatusPolicy.java
58 lines (51 loc) · 1.46 KB
/
OptStatusPolicy.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
package kim.hanjie.common.opt;
import java.lang.reflect.Method;
import java.util.Optional;
/**
* @author han
* @date 2021/3/10
*/
public class OptStatusPolicy implements IOptStatusPolicy {
/**
* 方法抛出异常后,判断操作状态
*
* @param throwable 异常
* @return OptStatus
*/
@Override
public OptStatus exceptionStatus(Throwable throwable) {
return OptStatus.EXCEPTION;
}
/**
* 根据返回对象判断操作状态
*
* @param retObj 调用方法的返回值
* @return OptStatus
*/
@Override
public OptStatus returnStatus(Method method, Object retObj) {
if (method.getReturnType() == void.class) {
return OptStatus.SUCCESS;
}
if (retObj == null) {
return nullReturnStatus(method);
}
OptStatus optStatus = notNullReturnStatus(method, retObj);
if (optStatus != null) {
return optStatus;
}
return OptStatus.SUCCESS;
}
protected OptStatus nullReturnStatus(Method method) {
return OptStatus.FAIL;
}
protected OptStatus notNullReturnStatus(Method method, Object retObj) {
if (retObj instanceof Boolean) {
return ((Boolean) retObj) ? OptStatus.SUCCESS : OptStatus.FAIL;
}
if (retObj instanceof Optional) {
return ((Optional) retObj).isPresent() ? OptStatus.SUCCESS : OptStatus.FAIL;
}
return null;
}
}