Skip to content

Package: Converters

Converters

nameinstructionbranchcomplexitylinemethod
Converters(Configuration)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getCustomConverter(Class)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getDefaultConverter(Class)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getDefaultConverters()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
initConverters(Configuration)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
initDefaultConverters()
M: 0 C: 84
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
registerConverter(Class, ConverterWrapper)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.model.metamodel;
14:
15: import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
16: import cz.cvut.kbss.jopa.oom.converter.*;
17: import cz.cvut.kbss.jopa.oom.converter.datetime.*;
18: import cz.cvut.kbss.jopa.utils.Configuration;
19:
20: import java.time.Instant;
21: import java.time.LocalDateTime;
22: import java.time.LocalTime;
23: import java.time.ZonedDateTime;
24: import java.util.*;
25:
26: /**
27: * Manages attribute converters.
28: */
29: public class Converters {
30:
31: private static final Map<Class<?>, ConverterWrapper<?, ?>> DEFAULT_CONVERTERS = initDefaultConverters();
32:
33: private final Map<Class<?>, ConverterWrapper<?, ?>> converters = new HashMap<>();
34:
35: Converters(Configuration configuration) {
36: initConverters(configuration);
37: }
38:
39: private void initConverters(Configuration configuration) {
40: converters.put(Object.class,
41: new ObjectConverter(configuration.is(JOPAPersistenceProperties.PREFER_MULTILINGUAL_STRING)));
42: }
43:
44: Optional<ConverterWrapper<?, ?>> getCustomConverter(Class<?> attributeType) {
45: return Optional.ofNullable(converters.get(attributeType));
46: }
47:
48: void registerConverter(Class<?> attributeType, ConverterWrapper<?, ?> converter) {
49: converters.put(attributeType, converter);
50: }
51:
52: public static Optional<ConverterWrapper<?, ?>> getDefaultConverter(Class<?> attributeType) {
53: return Optional.ofNullable(DEFAULT_CONVERTERS.get(attributeType));
54: }
55:
56: private static Map<Class<?>, ConverterWrapper<?, ?>> initDefaultConverters() {
57: final Map<Class<?>, ConverterWrapper<?, ?>> converters = new HashMap<>();
58: converters.put(LocalDateTime.class, new LocalDateTimeConverter());
59: converters.put(LocalTime.class, new LocalTimeConverter());
60: converters.put(Instant.class, new InstantConverter());
61: converters.put(ZonedDateTime.class, new ZonedDateTimeConverter());
62: converters.put(Date.class, new DateConverter());
63: converters.put(Short.class, new ToShortConverter());
64: converters.put(Integer.class, new ToIntegerConverter());
65: converters.put(Long.class, new ToLongConverter());
66: converters.put(Float.class, new ToFloatConverter());
67: converters.put(Double.class, new ToDoubleConverter());
68: converters.put(String.class, new ToStringConverter());
69: return Collections.unmodifiableMap(converters);
70: }
71:
72: /**
73: * Gets a map of default, built-in converters.
74: *
75: * @return Map of built-in converters, where the key is the target attribute type
76: */
77: public static Map<Class<?>, ConverterWrapper<?, ?>> getDefaultConverters() {
78: return DEFAULT_CONVERTERS;
79: }
80: }