1 |
|
package net.technearts; |
2 |
|
|
3 |
|
import java.lang.reflect.InvocationTargetException; |
4 |
|
import java.math.BigDecimal; |
5 |
|
import java.math.RoundingMode; |
6 |
|
import java.util.regex.Matcher; |
7 |
|
import java.util.regex.Pattern; |
8 |
|
|
9 |
|
import org.apache.commons.lang3.ArrayUtils; |
10 |
|
import org.apache.poi.ss.usermodel.DateUtil; |
11 |
|
|
|
|
| 58% |
Uncovered Elements: 21 (50) |
Complexity: 20 |
Complexity Density: 0.53 |
|
12 |
|
public final class ConverterFactory { |
13 |
|
|
14 |
|
private static final Pattern DECIMAL = Pattern.compile("decimal\\((\\d+)\\)"); |
15 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
16 |
0 |
private ConverterFactory() {}... |
17 |
|
|
|
|
| 41.7% |
Uncovered Elements: 7 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
18 |
10 |
@SuppressWarnings("unchecked")... |
19 |
|
private static final <T> Converter<T> converter(Class<?> klazz) { |
20 |
10 |
if (klazz.isEnum()) { |
21 |
10 |
return a -> { |
22 |
10 |
try { |
23 |
10 |
return (T) klazz.getMethod("valueOf", String.class).invoke(klazz, a); |
24 |
|
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException |
25 |
|
| NoSuchMethodException | SecurityException e) { |
26 |
0 |
throw new IllegalArgumentException("Unable to find enum " + klazz.getName()); |
27 |
|
} |
28 |
|
}; |
29 |
0 |
} else if (ArrayUtils.contains(klazz.getInterfaces(), Converter.class)) { |
30 |
0 |
return Converter.class.cast(klazz); |
31 |
|
} else { |
32 |
0 |
return a -> (T) a.toString(); |
33 |
|
} |
34 |
|
} |
35 |
|
|
|
|
| 58.1% |
Uncovered Elements: 13 (31) |
Complexity: 13 |
Complexity Density: 0.48 |
|
36 |
125 |
@SuppressWarnings("unchecked")... |
37 |
|
private static final <T> Converter<T> converter(String type) { |
38 |
125 |
final String nonNullType = type == null ? "" : type; |
39 |
125 |
Matcher m = DECIMAL.matcher(nonNullType); |
40 |
125 |
if (m.matches()) { |
41 |
15 |
int scale = Integer.valueOf(m.group(1)); |
42 |
15 |
return a -> (T) new BigDecimal(a).setScale(scale, RoundingMode.HALF_UP); |
43 |
|
} |
44 |
110 |
switch (nonNullType) { |
45 |
0 |
case "byte": |
46 |
0 |
return a -> (T) Byte.valueOf(a); |
47 |
0 |
case "short": |
48 |
0 |
return a -> (T) Short.valueOf(a); |
49 |
15 |
case "integer": |
50 |
15 |
return a -> (T) Integer.valueOf(a); |
51 |
0 |
case "long": |
52 |
0 |
return a -> (T) Long.valueOf(a); |
53 |
0 |
case "boolean": |
54 |
0 |
return a -> (T) Boolean.valueOf(a); |
55 |
15 |
case "date": |
56 |
15 |
return a -> (T) DateUtil.getJavaDate(Double.valueOf(a), true); |
57 |
0 |
case "float": |
58 |
0 |
return a -> (T) Float.valueOf(a); |
59 |
15 |
case "double": |
60 |
15 |
return a -> (T) Double.valueOf(a); |
61 |
0 |
case "char": |
62 |
0 |
return a -> (T) Character.valueOf(a.charAt(0)); |
63 |
0 |
case "string": |
64 |
65 |
default: |
65 |
65 |
return a -> (T) a.toString(); |
66 |
|
} |
67 |
|
} |
68 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
69 |
135 |
public static final <T> Converter<T> converter(Member member) {... |
70 |
135 |
try { |
71 |
135 |
return converter(Class.forName(member.getConverter())); |
72 |
|
} catch (ClassNotFoundException | NullPointerException e) { |
73 |
125 |
return converter(member.getConverter()); |
74 |
|
} |
75 |
|
} |
76 |
|
} |