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