Skip to content

Package: ConverterResolver

ConverterResolver

nameinstructionbranchcomplexitylinemethod
ConverterResolver(Converters)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
resolveConverter(Field, PropertyAttributes)
M: 0 C: 32
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.model.metamodel;
16:
17: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
18: import cz.cvut.kbss.jopa.oom.converter.EnumConverter;
19: import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter;
20:
21: import java.lang.reflect.Field;
22: import java.util.Optional;
23:
24: /**
25: * Determines potential converters which may be used on a field.
26: * <p>
27: * Currently, only built-in converters for data and annotation property attributes are supported, but in the future,
28: * custom converters should be also supported.
29: */
30: class ConverterResolver {
31:
32: private final Converters converters;
33:
34: ConverterResolver(Converters converters) {
35: this.converters = converters;
36: }
37:
38: /**
39: * Determines converter which should be used for transformation of values to and from the specified field.
40: * <p>
41: * Besides custom converters, the system supports a number of built-in converters, which ensure that e.g. widening
42: * conversion or mapping to Java 8 Date/Time API is supported.
43: *
44: * @param field The field for which converter should be determined
45: * @param config Mapping configuration extracted during metamodel building
46: * @return Possible converter instance to be used for transformation of values of the specified field. Returns empty
47: * {@code Optional} if no suitable converter is found (or needed)
48: */
49: public Optional<ConverterWrapper<?, ?>> resolveConverter(Field field, PropertyAttributes config) {
50:• if (config.getPersistentAttributeType() == Attribute.PersistentAttributeType.OBJECT) {
51: return Optional.empty();
52: }
53: final Class<?> attValueType = config.getType().getJavaType();
54:• if (attValueType.isEnum()) {
55: return Optional.of(new EnumConverter(attValueType));
56: }
57:• if (config.isLexicalForm()) {
58: return Optional.of(new ToLexicalFormConverter());
59: }
60: return converters.getConverter(attValueType);
61: }
62: }