StringToBooleanConverter.java

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 1 1. convert : negated conditional → RUN_ERROR
		if (source == null) {
51
			return null;
52
		}
53
		String value = ((String) source).trim();
54 1 1. convert : negated conditional → RUN_ERROR
		if ("".equals(value)) {
55
			return null;
56
		}
57 1 1. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → RUN_ERROR
		return toBoolean(source, value);
58
	}
59
60
	@Override
61
	public boolean supports(Class<?> sourceType, Class<?> targetType) {
62 3 1. supports : negated conditional → RUN_ERROR
2. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → RUN_ERROR
3. supports : negated conditional → RUN_ERROR
		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 1 1. toBoolean : negated conditional → RUN_ERROR
		if (trueValues.contains(value)) {
69 1 1. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → RUN_ERROR
			return (T) Boolean.TRUE;
70 1 1. toBoolean : negated conditional → RUN_ERROR
		} else if (falseValues.contains(value)) {
71 1 1. toBoolean : replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → RUN_ERROR
			return (T) Boolean.FALSE;
72
		} else {
73
			throw new ConversionException("Invalid boolean value '" + source + "'");
74
		}
75
	}
76
}

Mutations

50

1.1
Location : convert
Killed by :
negated conditional → RUN_ERROR

54

1.1
Location : convert
Killed by :
negated conditional → RUN_ERROR

57

1.1
Location : convert
Killed by :
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::convert → RUN_ERROR

62

1.1
Location : supports
Killed by :
negated conditional → RUN_ERROR

2.2
Location : supports
Killed by :
replaced boolean return with true for fr/sii/ogham/core/convert/StringToBooleanConverter::supports → RUN_ERROR

3.3
Location : supports
Killed by :
negated conditional → RUN_ERROR

68

1.1
Location : toBoolean
Killed by :
negated conditional → RUN_ERROR

69

1.1
Location : toBoolean
Killed by :
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → RUN_ERROR

70

1.1
Location : toBoolean
Killed by :
negated conditional → RUN_ERROR

71

1.1
Location : toBoolean
Killed by :
replaced return value with null for fr/sii/ogham/core/convert/StringToBooleanConverter::toBoolean → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1