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