DelegateConverter.java

1
package fr.sii.ogham.core.convert;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
6
7
import fr.sii.ogham.core.exception.convert.ConversionException;
8
9
/**
10
 * Calls registered {@link SupportingConverter}s to make the real conversion. It
11
 * asks each {@link SupportingConverter} if it can make the conversion. If the
12
 * {@link SupportingConverter} can do the conversion, the conversion is applied
13
 * using that {@link SupportingConverter} and the result is immediately
14
 * returned.
15
 * 
16
 * If none of the registered {@link SupportingConverter}s can make the
17
 * conversion, then a {@link ConversionException} is thrown.
18
 * 
19
 * If the source is null, then the result is null too.
20
 * 
21
 * <strong>Registration order is important.</strong>
22
 * 
23
 * @author Aurélien Baudet
24
 *
25
 */
26
public class DelegateConverter implements Converter, ConverterRegistry {
27
	private final List<SupportingConverter> delegates;
28
29
	/**
30
	 * Registers none, one or several converters
31
	 * 
32
	 * @param delegates
33
	 *            the converters to register
34
	 */
35
	public DelegateConverter(SupportingConverter... delegates) {
36
		this(new ArrayList<>(Arrays.asList(delegates)));
37
	}
38
39
	/**
40
	 * Registers a list of converters. The list must not be null
41
	 * 
42
	 * @param delegates
43
	 *            the converters to register
44
	 */
45
	public DelegateConverter(List<SupportingConverter> delegates) {
46
		super();
47
		this.delegates = delegates;
48
	}
49
50
	@Override
51
	public ConverterRegistry register(SupportingConverter converter) {
52 1 1. register : negated conditional → RUN_ERROR
		if (!delegates.contains(converter)) {
53
			delegates.add(converter);
54
		}
55 1 1. register : replaced return value with null for fr/sii/ogham/core/convert/DelegateConverter::register → RUN_ERROR
		return this;
56
	}
57
58
	@Override
59
	public <T> T convert(Object source, Class<T> targetType) {
60 1 1. convert : negated conditional → RUN_ERROR
		if (source == null) {
61
			return null;
62
		}
63
		for (SupportingConverter converter : delegates) {
64 1 1. convert : negated conditional → RUN_ERROR
			if (converter.supports(source.getClass(), targetType)) {
65 1 1. convert : replaced return value with null for fr/sii/ogham/core/convert/DelegateConverter::convert → RUN_ERROR
				return converter.convert(source, targetType);
66
			}
67
		}
68
		throw new ConversionException("No converter available to convert " + source + " into " + targetType.getSimpleName());
69
	}
70
71
	@Override
72
	public List<SupportingConverter> getRegisteredConverters() {
73 1 1. getRegisteredConverters : replaced return value with Collections.emptyList for fr/sii/ogham/core/convert/DelegateConverter::getRegisteredConverters → NO_COVERAGE
		return delegates;
74
	}
75
76
}

Mutations

52

1.1
Location : register
Killed by :
negated conditional → RUN_ERROR

55

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

60

1.1
Location : convert
Killed by :
negated conditional → RUN_ERROR

64

1.1
Location : convert
Killed by :
negated conditional → RUN_ERROR

65

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

73

1.1
Location : getRegisteredConverters
Killed by :
replaced return value with Collections.emptyList for fr/sii/ogham/core/convert/DelegateConverter::getRegisteredConverters → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1