StringToArrayConverter.java

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 1 1. convert : negated conditional → RUN_ERROR
		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 2 1. convert : changed conditional boundary → RUN_ERROR
2. convert : negated conditional → RUN_ERROR
		for (int i = 0; i < parts.length; i++) {
84
			String sourceElement = parts[i];
85
			Object targetElement = elementsConverter.convert(sourceElement.trim(), targetType.getComponentType());
86 1 1. convert : removed call to java/lang/reflect/Array::set → RUN_ERROR
			Array.set(target, i, targetElement);
87
		}
88 1 1. convert : replaced return value with null for fr/sii/ogham/core/convert/StringToArrayConverter::convert → RUN_ERROR
		return (T) target;
89
	}
90
91
	@Override
92
	public boolean supports(Class<?> sourceType, Class<?> targetType) {
93 4 1. supports : negated conditional → RUN_ERROR
2. supports : negated conditional → RUN_ERROR
3. supports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::supports → RUN_ERROR
4. supports : negated conditional → RUN_ERROR
		return String.class.isAssignableFrom(sourceType) && Object[].class.isAssignableFrom(targetType) && elementsConverterSupports(sourceType, targetType);
94
	}
95
96
	private boolean elementsConverterSupports(Class<?> sourceType, Class<?> targetType) {
97 1 1. elementsConverterSupports : negated conditional → RUN_ERROR
		if (elementsConverter instanceof SupportingConverter) {
98 2 1. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → RUN_ERROR
2. elementsConverterSupports : replaced boolean return with true for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → RUN_ERROR
			return ((SupportingConverter) elementsConverter).supports(sourceType, targetType);
99
		}
100 1 1. elementsConverterSupports : replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → RUN_ERROR
		return true;
101
	}
102
103
}

Mutations

77

1.1
Location : convert
Killed by :
negated conditional → RUN_ERROR

83

1.1
Location : convert
Killed by :
changed conditional boundary → RUN_ERROR

2.2
Location : convert
Killed by :
negated conditional → RUN_ERROR

86

1.1
Location : convert
Killed by :
removed call to java/lang/reflect/Array::set → RUN_ERROR

88

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

93

1.1
Location : supports
Killed by :
negated conditional → RUN_ERROR

2.2
Location : supports
Killed by :
negated conditional → RUN_ERROR

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

4.4
Location : supports
Killed by :
negated conditional → RUN_ERROR

97

1.1
Location : elementsConverterSupports
Killed by :
negated conditional → RUN_ERROR

98

1.1
Location : elementsConverterSupports
Killed by :
replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → RUN_ERROR

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

100

1.1
Location : elementsConverterSupports
Killed by :
replaced boolean return with false for fr/sii/ogham/core/convert/StringToArrayConverter::elementsConverterSupports → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1