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