复制代码
1 /**
2 * 一个校验对象是否为null的豪华大礼包
3 * 可以校验:Collection,Map,String,Enumeration,Iterator,以及所有数组类型
4 *
5 */
6 public final class ValidatorPlus {
7
8 /**
9 * 禁止实例化这个类
10 */
11 private ValidatorPlus() {
12 //AssertionError不是必须的. 但它可以避免不小心在类的内部调用构造器. 保证该类在任何情况下都不会被实例化.
13 throw new AssertionError(getClass().getName() + " 禁止实例化!");
14 }
15
16 /**
17 * 判断对象是否不为Null或者Empty,调用 #isNull(Object) 方法
18 *
19 * @param value 可以是Collection,Map,String,Enumeration,Iterator,以及所有数组类型
20 * @return 如果是null, 返回false
21 * 如果是空也返回false
22 * 其他情况返回true
23 * 如果不是上述类型,不判断empty,返回true
24 */
25 public static boolean isNotNull(Object value) {
26 return !isNull(value);
27 }
28
29 /**
30 * 判断对象是否为Null或者Empty
31 *
32 * @param value 可以是Collection,Map,String,Enumeration,Iterator,以及所有数组类型
33 * @return 如果是null, 返回true
34 * 其他情况返回false
35 * 如果不是上述类型,不判断empty,返回false
36 */
37 public static boolean isNull(Object value) {
38 if (Objects.isNull(value)) {
39 return true;
40 }
41
42 // 字符串
43 if (value instanceof String) {
44 return isBlank(value.toString());
45 }
46
47 // 集合
48 if (value instanceof Collection) {
49 return ((Collection<?>) value).isEmpty();
50 }
51
52 // map
53 if (value instanceof Map) {
54 return ((Map<?, ?>) value).isEmpty();
55 }
56
57 // 枚举
58 if (value instanceof Enumeration) {
59 return !((Enumeration<?>) value).hasMoreElements();
60 }
61
62 // Iterator迭代器
63 if (value instanceof Iterator) {
64 return !((Iterator<?>) value).hasNext();
65 }
66
67 // 数组
68 if (arrayIsNull(value)) {
69 return true;
70 }
71 // 这里可以扩展
72 return false;
73 }
74
75 /**
76 * 数组 类型的验证,区分 primitive 和包装类型.
77 *
78 * @param value 可以是
79 * Object[] 二维数组属于这个类型
80 * byte[]
81 * boolean[]
82 * char[]
83 * int[]
84 * long[]
85 * short[]
86 * float[]
87 * double[]
88 * @return 如果是数组类型(区分 primitive和包装类型), 判断其length==0;
89 * 如果不是 直接返回false
90 */
91 private static boolean arrayIsNull(Object value) {
92 // 数组 Integer/String...自定义的对象User.等数组也 instanceof Object[]
93 if (value instanceof Object[]) {
94 return ((Object[]) value).length == 0;
95 }
96
97 // primitive ints
98 if (value instanceof int[]) {
99 return ((int[]) value).length == 0;
100 }
101
102 // primitive long
103 if (value instanceof long[]) {
104 return ((long[]) value).length == 0;
105 }
106
107 // primitive float
108 if (value instanceof float[]) {
109 return ((float[]) value).length == 0;
110 }
111
112 // primitive double
113 if (value instanceof double[]) {
114 return ((double[]) value).length == 0;
115 }
116
117 // primitive char
118 if (value instanceof char[]) {
119 return ((char[]) value).length == 0;
120 }
121
122 // primitive boolean
123 if (value instanceof boolean[]) {
124 return ((boolean[]) value).length == 0;
125 }
126
127 // primitive byte
128 if (value instanceof byte[]) {
129 return ((byte[]) value).length == 0;
130 }
131
132 // primitive short
133 if (value instanceof short[]) {
134 return ((short[]) value).length == 0;
135 }
136 return false;
137 }
138
139 /**
140 * <p>检查 String 是否为空格、空 ("") 或 null。</p>
141 *
142 * <pre>
143 * StringUtils.isBlank(null) = true
144 * StringUtils.isBlank("") = true
145 * StringUtils.isBlank(" ") = true
146 * StringUtils.isBlank("bob") = false
147 * StringUtils.isBlank(" bob ") = false
148 * </pre>
149 *
150 * @param str 要检查的 String 可能为 null
151 * @return <code>true</code> 如果 String 为 null、空或空格
152 */
153 private static boolean isBlank(String str) {
154
155 if (str == null || str.isEmpty()) {
156 return true;
157 }
158
159 for (int i = 0; i < str.length(); i++) {
160 if ((!Character.isWhitespace(str.charAt(i)))) {
161 return false;
162 }
163 }
164 return true;
165 }
166 }