1 | package fr.sii.ogham.core.convert; | |
2 | ||
3 | import java.lang.reflect.Array; | |
4 | ||
5 | /** | |
6 | * Converts a string to an array of anything. The string is split on | |
7 | * {@literal ,} by default. You can also provide your own separator. | |
8 | * | |
9 | * Each split value is trimmed to remove any spaces before or after. For | |
10 | * example: | |
11 | * | |
12 | * <pre> | |
13 | * String source = " foo , bar, abc"; | |
14 | * // calling the converter | |
15 | * String[] result = converter.convert(source, String[].class); | |
16 | * for (String value : result) { | |
17 | * System.out.println("'" + value + "'"); | |
18 | * } | |
19 | * // prints: | |
20 | * // 'foo' | |
21 | * // 'bar' | |
22 | * // 'abc' | |
23 | * </pre> | |
24 | * | |
25 | * This converter is also able to convert the string to an array of objects. For | |
26 | * example: | |
27 | * | |
28 | * <pre> | |
29 | * String source = "1, 2, 3"; | |
30 | * Integer[] numbers = converter.convert(source, Integer[].class); | |
31 | * </pre> | |
32 | * | |
33 | * If you have registered a custom converter to handle elements, you can | |
34 | * directly convert string to your objects: | |
35 | * | |
36 | * <pre> | |
37 | * String source = "bob, joe"; | |
38 | * Person[] persons = converter.convert(source, Person[].class); | |
39 | * </pre> | |
40 | * | |
41 | * @author Aurélien Baudet | |
42 | * | |
43 | */ | |
44 | public class StringToArrayConverter implements SupportingConverter { | |
45 | private final Converter elementsConverter; | |
46 | private final String splitPattern; | |
47 | ||
48 | /** | |
49 | * Initializes with the default separator {@literal ,} and another converter | |
50 | * that is used to convert each split element. | |
51 | * | |
52 | * @param elementsConverter | |
53 | * converts each element to the target type | |
54 | */ | |
55 | public StringToArrayConverter(Converter elementsConverter) { | |
56 | this(elementsConverter, ",\\s*"); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Initializes with the provided separator and another converter that is | |
61 | * used to convert each split element. | |
62 | * | |
63 | * @param elementsConverter | |
64 | * converts each element to the target type | |
65 | * @param splitPattern | |
66 | * the separator that is used to split the source string | |
67 | */ | |
68 | public StringToArrayConverter(Converter elementsConverter, String splitPattern) { | |
69 | super(); | |
70 | this.elementsConverter = elementsConverter; | |
71 | this.splitPattern = splitPattern; | |
72 | } | |
73 | ||
74 | @SuppressWarnings("unchecked") | |
75 | @Override | |
76 | public <T> T convert(Object source, Class<T> targetType) { | |
77 |
4
1. convert : negated conditional → NO_COVERAGE 2. convert : negated conditional → TIMED_OUT 3. convert : negated conditional → KILLED 4. convert : negated conditional → KILLED |
if (source == null) { |
78 | return null; | |
79 | } | |
80 | String s = (String) source; | |
81 | String[] parts = s.split(splitPattern); | |
82 | Object target = Array.newInstance(targetType.getComponentType(), parts.length); | |
83 |
8
1. convert : changed conditional boundary → NO_COVERAGE 2. convert : negated conditional → NO_COVERAGE 3. convert : changed conditional boundary → KILLED 4. convert : changed conditional boundary → KILLED 5. convert : changed conditional boundary → KILLED 6. convert : negated conditional → KILLED 7. convert : negated conditional → KILLED 8. convert : negated conditional → KILLED |
for (int i = 0; i < parts.length; i++) { |
84 | String sourceElement = parts[i]; | |
85 | Object targetElement = elementsConverter.convert(sourceElement.trim(), targetType.getComponentType()); | |
86 |
4
1. convert : removed call to java/lang/reflect/Array::set → NO_COVERAGE 2. convert : removed call to java/lang/reflect/Array::set → KILLED 3. convert : removed call to java/lang/reflect/Array::set → KILLED 4. convert : removed call to java/lang/reflect/Array::set → KILLED |
Array.set(target, i, targetElement); |
87 | } | |
88 |
4
1. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → NO_COVERAGE 2. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → TIMED_OUT 3. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → KILLED 4. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → KILLED |
return (T) target; |
89 | } | |
90 | ||
91 | @Override | |
92 | public boolean supports(Class<?> sourceType, Class<?> targetType) { | |
93 |
24
1. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → NO_COVERAGE 2. supports : negated conditional → NO_COVERAGE 3. supports : negated conditional → SURVIVED 4. supports : negated conditional → NO_COVERAGE 5. supports : negated conditional → NO_COVERAGE 6. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → TIMED_OUT 7. supports : negated conditional → TIMED_OUT 8. supports : negated conditional → TIMED_OUT 9. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED 10. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED 11. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED 12. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED 13. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → KILLED 14. supports : negated conditional → KILLED 15. supports : negated conditional → KILLED 16. supports : negated conditional → KILLED 17. supports : negated conditional → KILLED 18. supports : negated conditional → KILLED 19. supports : negated conditional → KILLED 20. supports : negated conditional → KILLED 21. supports : negated conditional → KILLED 22. supports : negated conditional → KILLED 23. supports : negated conditional → KILLED 24. supports : negated conditional → KILLED |
return String.class.isAssignableFrom(sourceType) && Object[].class.isAssignableFrom(targetType) && elementsConverterSupports(sourceType, targetType); |
94 | } | |
95 | ||
96 | private boolean elementsConverterSupports(Class<?> sourceType, Class<?> targetType) { | |
97 |
4
1. elementsConverterSupports : negated conditional → NO_COVERAGE 2. elementsConverterSupports : negated conditional → KILLED 3. elementsConverterSupports : negated conditional → KILLED 4. elementsConverterSupports : negated conditional → KILLED |
if (elementsConverter instanceof SupportingConverter) { |
98 |
4
1. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE 2. elementsConverterSupports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE 3. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED 4. elementsConverterSupports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED |
return ((SupportingConverter) elementsConverter).supports(sourceType, targetType); |
99 | } | |
100 |
4
1. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → NO_COVERAGE 2. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED 3. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED 4. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → KILLED |
return true; |
101 | } | |
102 | ||
103 | } | |
Mutations | ||
77 |
1.1 2.2 3.3 4.4 |
|
83 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
86 |
1.1 2.2 3.3 4.4 |
|
88 |
1.1 2.2 3.3 4.4 |
|
93 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 11.11 12.12 13.13 14.14 15.15 16.16 17.17 18.18 19.19 20.20 21.21 22.22 23.23 24.24 |
|
97 |
1.1 2.2 3.3 4.4 |
|
98 |
1.1 2.2 3.3 4.4 |
|
100 |
1.1 2.2 3.3 4.4 |