ObjectUtils.java
5.45 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package com.irrigation.icl.utils;
import com.google.common.collect.Lists;
import com.irrigation.icl.exception.ContextRuntimeException;
import org.springframework.beans.BeanUtils;
import java.util.*;
/**
* @author zhangchuanxi
* 对象工具类
* @version 1.0.1
*/
public class ObjectUtils {
/**
* 复制对象错误提示
*/
private final static String COPY_OBJECT_FAIL = "复制对象失败";
private ObjectUtils() {
}
/**
* 判断对象司是否是空
*
* @param obj
* @return
*/
public static boolean isNull(Object obj) {
return obj == null;
}
/**
* 判断对象司是否不是空
*
* @param obj
* @return
*/
public static boolean nonNull(Object obj) {
return obj != null;
}
/**
* 判断两个对象是否相等
*
* @param a
* @param b
* @return
*/
public static boolean equals(Object a, Object b) {
return a != null && a.equals(b);
}
/**
* 判断两个对象是否不相等
*
* @param a
* @param b
* @return
*/
public static boolean unequal(Object a, Object b) {
return (a != null && !a.equals(b)) || (b != null && !b.equals(a));
}
/**
* 判断集合是否是空
*
* @param collection
* @return
*/
public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}
/**
* 判断集合是否不是空
*
* @param collection
* @return
*/
public static boolean nonEmpty(Collection<?> collection) {
return !isEmpty(collection);
}
/**
* 判断map 是否是空
*
* @param map
* @return
*/
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
/**
* 判断map 是否不是空
*
* @param map
* @return
*/
public static boolean nonEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
/**
* 判断字符串 是否是 null 或 length=0
*
* @param cs
* @return
*/
public static boolean stringIsEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
/**
* 判断字符串 是否不是 null 并且 length!=0
*
* @param cs
* @return
*/
public static boolean stringNonEmpty(CharSequence cs) {
return !stringIsEmpty(cs);
}
/**
* 判断字符是否是空字符
*
* @param cs
* @return
*/
public static boolean stringIsBlank(CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
/**
* 判断字符是否是空字符
*
* @param cs
* @return
*/
public static boolean stringNonBlank(CharSequence cs) {
return !stringIsBlank(cs);
}
/**
* 去掉字符串前后空白字符
*
* @param str
* @return
*/
public static String stringTrim(final String str) {
return str == null ? null : str.trim();
}
/**
* 单个对象复制
*
* @param source
* @param targetClass
* @param <T>
* @return
*/
public static <T> T copyObject(Object source, Class<T> targetClass) {
if (isNull(source)) {
return null;
}
try {
T target = targetClass.newInstance();
BeanUtils.copyProperties(source, target);
return target;
} catch (Exception e) {
throw new ContextRuntimeException(COPY_OBJECT_FAIL, e);
}
}
/**
* 集合复制
*
* @param sourceList
* @param targetClass
* @param <T>
* @return
*/
public static <S, T> List<T> copyObjectList(List<S> sourceList, Class<T> targetClass) {
List<T> targetList = Lists.newArrayList();
if (nonEmpty(sourceList)) {
for (S source : sourceList) {
targetList.add(copyObject(source, targetClass));
}
}
return targetList;
}
/**
* 集合通过连接符号返回字符串
*
* @param iterable
* @param separator
* @return
*/
public static String stringJoin(Iterable<?> iterable, String separator) {
return iterable == null ? null : stringJoin(iterable.iterator(), separator);
}
private static String stringJoin(Iterator<?> iterator, String separator) {
if (iterator == null) {
return null;
} else if (!iterator.hasNext()) {
return "";
} else {
Object first = iterator.next();
if (!iterator.hasNext()) {
return Objects.toString(first, "");
} else {
StringBuilder buf = new StringBuilder(256);
if (first != null) {
buf.append(first);
}
while (iterator.hasNext()) {
if (separator != null) {
buf.append(separator);
}
Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
}
return buf.toString();
}
}
}
}