1 | package fr.sii.ogham.core.convert; | |
2 | ||
3 | import static java.util.Locale.ENGLISH; | |
4 | ||
5 | import java.util.HashSet; | |
6 | import java.util.Set; | |
7 | ||
8 | import fr.sii.ogham.core.exception.convert.ConversionException; | |
9 | ||
10 | /** | |
11 | * Converts a string to a boolean value. | |
12 | * | |
13 | * Strings that represent a {@code true} value are: | |
14 | * <ul> | |
15 | * <li>"true"</li> | |
16 | * <li>"on"</li> | |
17 | * <li>"yes"</li> | |
18 | * <li>"1"</li> | |
19 | * </ul> | |
20 | * | |
21 | * Strings that represent a {@code false} value are: | |
22 | * <ul> | |
23 | * <li>"false"</li> | |
24 | * <li>"off"</li> | |
25 | * <li>"no"</li> | |
26 | * <li>"0"</li> | |
27 | * </ul> | |
28 | * | |
29 | * @author Aurélien Baudet | |
30 | * | |
31 | */ | |
32 | public class StringToBooleanConverter implements SupportingConverter { | |
33 | private static final Set<String> trueValues = new HashSet<>(4); | |
34 | private static final Set<String> falseValues = new HashSet<>(4); | |
35 | ||
36 | static { | |
37 | trueValues.add("true"); | |
38 | trueValues.add("on"); | |
39 | trueValues.add("yes"); | |
40 | trueValues.add("1"); | |
41 | ||
42 | falseValues.add("false"); | |
43 | falseValues.add("off"); | |
44 | falseValues.add("no"); | |
45 | falseValues.add("0"); | |
46 | } | |
47 | ||
48 | @Override | |
49 | public <T> T convert(Object source, Class<T> targetType) { | |
50 |
4
1. convert : negated conditional → NO_COVERAGE 2. convert : negated conditional → KILLED 3. convert : negated conditional → KILLED 4. convert : negated conditional → KILLED |
if (source == null) { |
51 | return null; | |
52 | } | |
53 | String value = ((String) source).trim(); | |
54 |
4
1. convert : negated conditional → NO_COVERAGE 2. convert : negated conditional → KILLED 3. convert : negated conditional → KILLED 4. convert : negated conditional → KILLED |
if ("".equals(value)) { |
55 | return null; | |
56 | } | |
57 |
4
1. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → NO_COVERAGE 2. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED 3. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED 4. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → KILLED |
return toBoolean(source, value); |
58 | } | |
59 | ||
60 | @Override | |
61 | public boolean supports(Class<?> sourceType, Class<?> targetType) { | |
62 |
20
1. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → NO_COVERAGE 2. supports : negated conditional → NO_COVERAGE 3. supports : negated conditional → SURVIVED 4. supports : negated conditional → NO_COVERAGE 5. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → TIMED_OUT 6. supports : negated conditional → TIMED_OUT 7. supports : negated conditional → TIMED_OUT 8. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED 9. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED 10. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED 11. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED 12. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → KILLED 13. supports : negated conditional → 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 |
return String.class.isAssignableFrom(sourceType) && Boolean.class.isAssignableFrom(targetType); |
63 | } | |
64 | ||
65 | @SuppressWarnings("unchecked") | |
66 | private static <T> T toBoolean(Object source, String value) { | |
67 | value = value.toLowerCase(ENGLISH); | |
68 |
4
1. toBoolean : negated conditional → NO_COVERAGE 2. toBoolean : negated conditional → KILLED 3. toBoolean : negated conditional → KILLED 4. toBoolean : negated conditional → KILLED |
if (trueValues.contains(value)) { |
69 |
3
1. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → NO_COVERAGE 2. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED 3. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED |
return (T) Boolean.TRUE; |
70 |
3
1. toBoolean : negated conditional → NO_COVERAGE 2. toBoolean : negated conditional → KILLED 3. toBoolean : negated conditional → KILLED |
} else if (falseValues.contains(value)) { |
71 |
3
1. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → NO_COVERAGE 2. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED 3. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → KILLED |
return (T) Boolean.FALSE; |
72 | } else { | |
73 | throw new ConversionException("Invalid boolean value '" + source + "'"); | |
74 | } | |
75 | } | |
76 | } | |
Mutations | ||
50 |
1.1 2.2 3.3 4.4 |
|
54 |
1.1 2.2 3.3 4.4 |
|
57 |
1.1 2.2 3.3 4.4 |
|
62 |
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 |
|
68 |
1.1 2.2 3.3 4.4 |
|
69 |
1.1 2.2 3.3 |
|
70 |
1.1 2.2 3.3 |
|
71 |
1.1 2.2 3.3 |