WORK-NOTE

校验

判空:

工具类Tools,Tools.isEmpty(); Tools.isNotEmpty();。适用于对象判空。

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
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
} else if (obj instanceof CharSequence) {
return ((CharSequence)obj).length() == 0;
} else if (obj instanceof Collection) {
return ((Collection)obj).isEmpty();
} else if (obj instanceof Map) {
return ((Map)obj).isEmpty();
} else if (!(obj instanceof Object[])) {
return false;
} else {
Object[] object = (Object[])((Object[])obj);
if (object.length == 0) {
return true;
} else {
boolean empty = true;

for(int i = 0; i < object.length; ++i) {
if (!isEmpty(object[i])) {
empty = false;
break;
}
}
return empty;
}
}
}

工具类CollectionUtils,CollectionUtils.isEmpty(); CollectionUtils.isNotEmpty();。适用于集合判空。

1
2
3
4
5
6
7
8
9
10
public static boolean isEmpty(Collection coll) {
return coll == null || coll.isEmpty();
}

// 低级示范
if(list != null && !list.isEmpty()){
// 这个里面取list中的值
}else{
// 做其他处理
}

工具类EmptyCheckUtil,EmptyCheckUtil.check(Object obj, boolean exclude, Sring... params); EmptyCheckUtil.checkAll();

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
public static void check(Object bean, boolean exclude, String... params) throws Exception {
if (bean == null) {
throw new ServiceException(DefaultResultStatus.PARAM_NOT_EMPTY);
} else {
Class type = bean.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
boolean needCheck = exclude;
for(int i = 0; i < propertyDescriptors.length; ++i) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean);
String[] var12 = params;
int var13 = params.length;
for(int var14 = 0; var14 < var13; ++var14) {
String param = var12[var14];
if (param.equals(propertyName)) {
needCheck = !exclude;
break;
}
}
if (needCheck && (result == null || "".equals(result))) {
throw new ServiceException(DefaultResultStatus.PARAM_NOT_EMPTY.getStatusCode(), propertyName + "不能为空!");
needCheck = exclude;
}
}
}

字符串判空,标准写法

1
2
3
4
String test = null;
if(result == null || "".equals(result)){
// true
}

注解

springboot注解

@Cacheable 缓存注解