Skip to content

Package: DataTypeTransformer

DataTypeTransformer

nameinstructionbranchcomplexitylinemethod
DataTypeTransformer()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
initCustomTransformers()
M: 0 C: 69
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
lambda$initCustomTransformers$0(Object)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$initCustomTransformers$1(Object)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$initCustomTransformers$2(Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$initCustomTransformers$3(Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$initCustomTransformers$4(Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$initCustomTransformers$5(Object)
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%
lambda$initCustomTransformers$6(Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$transformIndividualToEnumConstant$7(String, Enum, String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$transformIndividualToEnumConstant$8(Enum, String)
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%
lambda$transformIndividualToEnumConstant$9(String, Class)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
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%
transformIndividualToEnumConstant(String, Class)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
transformLiteralToEnumConstant(Object, 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%
transformValue(Object, Class)
M: 0 C: 47
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 11
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
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: * <p>
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.jsonld.deserialization.util;
16:
17: import cz.cvut.kbss.jopa.datatype.DatatypeTransformer;
18: import cz.cvut.kbss.jopa.datatype.util.Pair;
19: import cz.cvut.kbss.jopa.model.MultilingualString;
20: import cz.cvut.kbss.jsonld.common.EnumUtil;
21: import cz.cvut.kbss.jsonld.exception.InvalidEnumMappingException;
22:
23: import java.time.*;
24: import java.util.*;
25: import java.util.function.Function;
26:
27: /**
28: * Provides transformation of values to various target types.
29: */
30: public class DataTypeTransformer {
31:
32: private static final Map<Pair, Function<Object, ?>> CUSTOM_TRANSFORMERS = initCustomTransformers();
33:
34: private static Map<Pair, Function<Object, ?>> initCustomTransformers() {
35: final Map<Pair, Function<Object, ?>> map = new HashMap<>();
36: map.put(new Pair<>(LangString.class, MultilingualString.class), src -> {
37: final LangString ls = (LangString) src;
38: return new MultilingualString(Collections.singletonMap(ls.getLanguage().orElse(null), ls.getValue()));
39: });
40: map.put(new Pair<>(String.class, MultilingualString.class),
41: src -> new MultilingualString(Collections.singletonMap(null, src.toString())));
42: map.put(new Pair<>(OffsetDateTime.class, LocalDateTime.class), src -> ((OffsetDateTime) src).toLocalDateTime());
43: map.put(new Pair<>(OffsetDateTime.class, ZonedDateTime.class), src -> ((OffsetDateTime) src).toZonedDateTime());
44: map.put(new Pair<>(OffsetDateTime.class, Instant.class), src -> ((OffsetDateTime) src).toInstant());
45: map.put(new Pair<>(OffsetDateTime.class, Date.class), src -> Date.from(((OffsetDateTime) src).toInstant()));
46: map.put(new Pair<>(OffsetTime.class, LocalTime.class), src -> ((OffsetTime) src).toLocalTime());
47: return map;
48: }
49:
50: public static <T> T transformValue(Object value, Class<T> targetClass) {
51: Objects.requireNonNull(value);
52: Objects.requireNonNull(targetClass);
53: final Class<?> sourceClass = value.getClass();
54:• if (targetClass.isAssignableFrom(sourceClass)) {
55: return targetClass.cast(value);
56: }
57:• if (targetClass.isEnum()) {
58: return (T) transformLiteralToEnumConstant(value, (Class) targetClass);
59: }
60: final Pair<Class<?>, Class<?>> key = new Pair<>(sourceClass, targetClass);
61:• if (CUSTOM_TRANSFORMERS.containsKey(key)) {
62: return targetClass.cast(CUSTOM_TRANSFORMERS.get(key).apply(value));
63: }
64: return DatatypeTransformer.transform(value, targetClass);
65: }
66:
67: private static <T extends Enum<T>> T transformLiteralToEnumConstant(Object value, Class<T> targetClass) {
68: return Enum.valueOf(targetClass, value.toString());
69: }
70:
71: /**
72: * Transforms the specified individual identifier to the corresponding enum constant.
73: * <p>
74: * This transformation uses the {@link cz.cvut.kbss.jopa.model.annotations.Individual} mapping of enum constants.
75: *
76: * @param identifier Individual identifier
77: * @param targetClass Target enum
78: * @param <T> Enum type
79: * @return Matching enum constant
80: * @throws InvalidEnumMappingException When no matching enum constant is found for the specified identifier
81: */
82: public static <T extends Enum<T>> T transformIndividualToEnumConstant(String identifier, Class<T> targetClass) {
83: return EnumUtil.findMatchingConstant(targetClass, (e, iri) -> iri.equals(identifier), (e, iri) -> e)
84: .orElseThrow(() -> new InvalidEnumMappingException(
85: "No matching constant found for individual <" + identifier + "> in target class " + targetClass));
86: }
87: }