SimpleConverterBuilder.java

1
package fr.sii.ogham.core.builder.env;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
6
import fr.sii.ogham.core.builder.configuration.MayOverride;
7
import fr.sii.ogham.core.convert.Converter;
8
import fr.sii.ogham.core.convert.ConverterRegistry;
9
import fr.sii.ogham.core.convert.DefaultConverter;
10
import fr.sii.ogham.core.convert.SupportingConverter;
11
import fr.sii.ogham.core.fluent.AbstractParent;
12
13
/**
14
 * A {@link ConverterBuilder} that builds the converter:
15
 * <ul>
16
 * <li>If a custom converter is defined, use it</li>
17
 * <li>If no custom converter, use {@link DefaultConverter}</li>
18
 * <li>If {@link DefaultConverter} or custom converter that also implements
19
 * {@link ConverterRegistry}, register all previously registered
20
 * {@link SupportingConverter}s</li>
21
 * </ul>
22
 * 
23
 * @author Aurélien Baudet
24
 *
25
 * @param <P>
26
 *            the type of the parent builder (when calling {@link #and()}
27
 *            method)
28
 */
29
public class SimpleConverterBuilder<P> extends AbstractParent<P> implements ConverterBuilder<P> {
30
	private List<SupportingConverter> delegates;
31
	private Converter converter;
32
	private Converter defaultConverter;
33
34
	/**
35
	 * Initializes the builder with the provided parent. The list of
36
	 * {@link SupportingConverter}s is initialized with an empty list.
37
	 * 
38
	 * @param parent
39
	 *            the parent builder
40
	 */
41
	public SimpleConverterBuilder(P parent) {
42
		super(parent);
43
		delegates = new ArrayList<>();
44
	}
45
46
	/**
47
	 * Copies values from other parameter to this instance.
48
	 * 
49
	 * @param parent
50
	 *            the parent builder
51
	 * @param other
52
	 *            the other builder that needs to be copied
53
	 */
54
	public SimpleConverterBuilder(P parent, SimpleConverterBuilder<?> other) {
55
		this(parent);
56
		this.converter = other.converter;
57
		this.delegates = new ArrayList<>(other.delegates);
58
	}
59
60
	@Override
61
	public SimpleConverterBuilder<P> override(Converter converter) {
62
		this.converter = converter;
63 1 1. override : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::override → RUN_ERROR
		return this;
64
	}
65
66
	@Override
67
	public SimpleConverterBuilder<P> register(SupportingConverter converter) {
68
		delegates.add(converter);
69 1 1. register : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::register → RUN_ERROR
		return this;
70
	}
71
72
	@Override
73
	public ConverterBuilder<P> defaultConverter(MayOverride<Converter> converter) {
74
		defaultConverter = converter.override(defaultConverter);
75 1 1. defaultConverter : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::defaultConverter → RUN_ERROR
		return this;
76
	}
77
78
	/**
79
	 * Build the converter:
80
	 * <ul>
81
	 * <li>If a custom converter is defined, use it</li>
82
	 * <li>If no custom converter, use {@link DefaultConverter}</li>
83
	 * <li>If {@link DefaultConverter} or custom converter that also implements
84
	 * {@link ConverterRegistry}, register all previously registered
85
	 * {@link SupportingConverter}s</li>
86
	 * </ul>
87
	 */
88
	@Override
89
	public Converter build() {
90
		Converter builtConverter = buildBaseConverter();
91 1 1. build : negated conditional → RUN_ERROR
		if (builtConverter instanceof ConverterRegistry) {
92
			for (SupportingConverter conv : delegates) {
93
				((ConverterRegistry) builtConverter).register(conv);
94
			}
95
		}
96 1 1. build : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::build → RUN_ERROR
		return builtConverter;
97
	}
98
99
	private Converter buildBaseConverter() {
100 1 1. buildBaseConverter : negated conditional → RUN_ERROR
		if (converter != null) {
101 1 1. buildBaseConverter : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::buildBaseConverter → RUN_ERROR
			return converter;
102
		}
103 1 1. buildBaseConverter : negated conditional → RUN_ERROR
		if (defaultConverter != null) {
104 1 1. buildBaseConverter : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::buildBaseConverter → RUN_ERROR
			return defaultConverter;
105
		}
106 1 1. buildBaseConverter : replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::buildBaseConverter → NO_COVERAGE
		return new DefaultConverter();
107
	}
108
}

Mutations

63

1.1
Location : override
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::override → RUN_ERROR

69

1.1
Location : register
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::register → RUN_ERROR

75

1.1
Location : defaultConverter
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::defaultConverter → RUN_ERROR

91

1.1
Location : build
Killed by :
negated conditional → RUN_ERROR

96

1.1
Location : build
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::build → RUN_ERROR

100

1.1
Location : buildBaseConverter
Killed by :
negated conditional → RUN_ERROR

101

1.1
Location : buildBaseConverter
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::buildBaseConverter → RUN_ERROR

103

1.1
Location : buildBaseConverter
Killed by :
negated conditional → RUN_ERROR

104

1.1
Location : buildBaseConverter
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::buildBaseConverter → RUN_ERROR

106

1.1
Location : buildBaseConverter
Killed by :
replaced return value with null for fr/sii/ogham/core/builder/env/SimpleConverterBuilder::buildBaseConverter → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1