1. Project Clover database Sun May 19 2019 23:30:46 BRT
  2. Package net.technearts

File ConverterFactory.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
83% of files have more coverage

Code metrics

8
38
4
1
76
68
20
0.53
9.5
4
5

Classes

Class Line # Actions
ConverterFactory 12 38 0% 20 21
0.5858%
 

Contributing tests

No tests hitting this source file were found.

Source view

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   
 
12    public final class ConverterFactory {
13   
14    private static final Pattern DECIMAL = Pattern.compile("decimal\\((\\d+)\\)");
15   
 
16  0 toggle private ConverterFactory() {}
17   
 
18  10 toggle @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   
 
36  125 toggle @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   
 
69  135 toggle 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    }