-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmptyUtils.java
More file actions
executable file
·72 lines (64 loc) · 2.02 KB
/
EmptyUtils.java
File metadata and controls
executable file
·72 lines (64 loc) · 2.02 KB
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
package com.youth.xframe.utils;
import android.os.Build;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.util.SparseLongArray;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
/**
* 判断是否为空工具类
*/
public class XEmptyUtils {
private XEmptyUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* 判断对象是否为null或长度数量为0
*
* @param obj 对象
* @return {@code true}: 为空<br>{@code false}: 不为空
*/
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
}
if (obj instanceof String && obj.toString().length() == 0) {
return true;
}
if (obj.getClass().isArray() && Array.getLength(obj) == 0) {
return true;
}
if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
return true;
}
if (obj instanceof Map && ((Map) obj).isEmpty()) {
return true;
}
if (obj instanceof SparseArray && ((SparseArray) obj).size() == 0) {
return true;
}
if (obj instanceof SparseBooleanArray && ((SparseBooleanArray) obj).size() == 0) {
return true;
}
if (obj instanceof SparseIntArray && ((SparseIntArray) obj).size() == 0) {
return true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (obj instanceof SparseLongArray && ((SparseLongArray) obj).size() == 0) {
return true;
}
}
return false;
}
/**
* 判断字符串是否为null或全为空格
*
* @param s 待校验字符串
* @return {@code true}: null或全空格<br> {@code false}: 不为null且不全空格
*/
public static boolean isSpace(String s) {
return (s == null || s.trim().length() == 0);
}
}