1 package com.github.mygreen.supercsv.util;
2
3 import java.lang.annotation.Annotation;
4 import java.lang.reflect.Method;
5 import java.util.Collection;
6 import java.util.Locale;
7 import java.util.Map;
8 import java.util.Objects;
9 import java.util.Optional;
10
11 import com.github.mygreen.supercsv.annotation.PatternFlag;
12 import com.github.mygreen.supercsv.builder.BuildCase;
13
14
15
16
17
18
19
20
21
22 public class Utils {
23
24
25
26
27 public static final boolean ENABLED_LIB_JODA_TIME;
28 static {
29 boolean enabled;
30 try {
31 Class.forName("org.joda.time.LocalDateTime");
32 enabled = true;
33 } catch(ClassNotFoundException e) {
34 enabled = false;
35 }
36 ENABLED_LIB_JODA_TIME = enabled;
37 }
38
39
40
41
42
43
44 public static boolean isEmpty(final String str) {
45 if(str == null || str.isEmpty()) {
46 return true;
47 }
48
49 if(str.length() == 1) {
50 return str.charAt(0) == '\u0000';
51 }
52
53 return false;
54 }
55
56
57
58
59
60
61 public static boolean isNotEmpty(final String str) {
62 return !isEmpty(str);
63 }
64
65
66
67
68
69
70 public static boolean isEmpty(final Collection<?> collection) {
71 if(collection == null || collection.isEmpty()) {
72 return true;
73 }
74
75 return false;
76 }
77
78 public static boolean isNotEmpty(final Collection<?> collection) {
79 return !isEmpty(collection);
80 }
81
82
83
84
85
86
87 public static boolean isEmpty(final Map<?, ?> map) {
88 if(map == null || map.isEmpty()) {
89 return true;
90 }
91
92 return false;
93 }
94
95 public static boolean isNotEmpty(final Map<?, ?> map) {
96 return !isEmpty(map);
97 }
98
99
100
101
102
103
104 public static boolean isEmpty(final Object[] arrays) {
105 if(arrays == null || arrays.length == 0) {
106 return true;
107 }
108
109 return false;
110 }
111
112
113
114
115
116
117 public static boolean isNotEmpty(final Object[] arrays) {
118 return !isEmpty(arrays);
119 }
120
121
122
123
124
125
126
127
128 public static Locale getLocale(final String str) {
129
130 if(isEmpty(str)) {
131 return Locale.getDefault();
132 }
133
134 if(!str.contains("_")) {
135 return new Locale(str);
136 }
137
138 final String[] split = str.split("_");
139 if(split.length == 2) {
140 return new Locale(split[0], split[1]);
141
142 } else {
143 return new Locale(split[0], split[1], split[2]);
144 }
145
146 }
147
148
149
150
151
152
153
154
155
156 @SuppressWarnings("unchecked")
157 public static <T> Optional<T> getAnnotationAttribute(final Annotation anno, final String attrName, final Class<T> attrType) {
158
159 try {
160 final Method method = anno.annotationType().getMethod(attrName);
161 method.setAccessible(true);
162 if(!attrType.equals(method.getReturnType())) {
163 return Optional.empty();
164 }
165
166 final Object value = method.invoke(anno);
167 return Optional.of((T)value);
168
169 } catch (Exception e) {
170 return Optional.empty();
171 }
172
173 }
174
175
176
177
178
179
180
181
182
183 public static <T> boolean hasAnnotationAttribute(final Annotation anno, final String attrName, final Class<T> attrType) {
184
185 return getAnnotationAttribute(anno, attrName, attrType).isPresent();
186
187 }
188
189
190
191
192
193
194
195
196
197
198 public static boolean containsBuildCase(final Annotation anno, final BuildCase buildCase) {
199
200 Objects.requireNonNull(anno);
201 Objects.requireNonNull(buildCase);
202
203 final Optional<BuildCase[]> attrCases = getAnnotationAttribute(anno, "cases", BuildCase[].class);
204 if(attrCases.isPresent()) {
205 final BuildCase[] casesValue = attrCases.get();
206 if(casesValue.length == 0) {
207
208 return true;
209 }
210
211 for(BuildCase value : casesValue) {
212 if(value == buildCase) {
213 return true;
214 }
215 }
216
217 return false;
218 }
219
220
221 return true;
222 }
223
224
225
226
227
228 public static boolean isEnabledJodaTime() {
229 return ENABLED_LIB_JODA_TIME;
230 }
231
232
233
234
235
236
237
238 public static Object getPrimitiveDefaultValue(final Class<?> type) {
239
240 Objects.requireNonNull(type, "type should not be null.");
241
242 if(!type.isPrimitive()) {
243 return null;
244
245 } else if(boolean.class.isAssignableFrom(type)) {
246 return false;
247
248 } else if(char.class.isAssignableFrom(type)) {
249 return '\u0000';
250
251 } else if(byte.class.isAssignableFrom(type)) {
252 return (byte)0;
253
254 } else if(short.class.isAssignableFrom(type)) {
255 return (short)0;
256
257 } else if(int.class.isAssignableFrom(type)) {
258 return 0;
259
260 } else if(long.class.isAssignableFrom(type)) {
261 return 0l;
262
263 } else if(float.class.isAssignableFrom(type)) {
264 return 0.0f;
265
266 } else if(double.class.isAssignableFrom(type)) {
267 return 0.0d;
268 }
269
270 return null;
271
272 }
273
274
275
276
277
278
279
280 public static String[] concat(final String[] array1, final String[] array2) {
281
282 if(array1 == null || array1.length == 0) {
283 return clone(array2);
284
285 } else if(array2 == null || array2.length == 0) {
286 return clone(array1);
287 }
288
289 final String[] joinedArray = new String[array1.length + array2.length];
290 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
291 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
292 return joinedArray;
293
294 }
295
296
297
298
299
300
301
302 public static String[] clone(final String[] array) {
303 if (array == null) {
304 return null;
305 }
306 return array.clone();
307 }
308
309
310
311
312
313
314
315 public static int[] toArray(final Collection<Integer> collection) {
316 Objects.requireNonNull(collection);
317
318 final int size = collection.size();
319 final int[] array = new int[size];
320
321 int i=0;
322 for(Integer value : collection) {
323 array[i] = value;
324 i++;
325 }
326
327 return array;
328 }
329
330
331
332
333
334
335 public static int buildRegexFlags(final PatternFlag[] flags) {
336
337 int intFlag = 0;
338 for(PatternFlag flag : flags) {
339 intFlag = intFlag | flag.getValue();
340 }
341
342 return intFlag;
343
344 }
345
346
347
348
349
350
351 public static String uncapitalize(final String str) {
352 final int strLen;
353 if(str == null || (strLen = str.length()) == 0) {
354 return str;
355 }
356
357 return new StringBuilder(strLen)
358 .append(String.valueOf(str.charAt(0)).toLowerCase())
359 .append(str.substring(1))
360 .toString();
361 }
362
363
364
365
366
367
368
369 public static String trim(final String value, final boolean trimmed) {
370 if(!trimmed || value == null) {
371 return value;
372 }
373
374 return value.trim();
375
376 }
377
378
379
380
381
382
383
384
385 public static boolean toBoolean(final String value, final boolean defaultValue) {
386 String text = trim(value, true);
387 if(isEmpty(text)) {
388 return defaultValue;
389 }
390
391 return Boolean.valueOf(text.toLowerCase());
392 }
393
394 }