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%
lambda$static$0(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$static$1(Object)
M: 14 C: 6
30%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
lambda$static$10(Object)
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%
lambda$static$11(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$static$2(Object)
M: 14 C: 9
39%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
lambda$static$3(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$static$4(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$static$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$static$6(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$static$7(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$static$8(Object)
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%
lambda$static$9(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%
registerTransformationRule(Class, Class, Function)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 113
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
transformToEnumConstant(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: 52
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 11
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 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.jsonld.deserialization.util;
14:
15: import cz.cvut.kbss.jopa.model.MultilingualString;
16:
17: import java.net.MalformedURLException;
18: import java.net.URI;
19: import java.net.URL;
20: import java.text.ParseException;
21: import java.text.SimpleDateFormat;
22: import java.time.LocalDateTime;
23: import java.time.ZonedDateTime;
24: import java.util.*;
25: import java.util.function.Function;
26:
27: /**
28: * Provides transformations of values to various target types.
29: */
30: public class DataTypeTransformer {
31:
32: private static final Map<TransformationRuleIdentifier<?, ?>, Function> rules = new HashMap<>();
33:
34: static {
35: rules.put(new TransformationRuleIdentifier<>(String.class, URI.class), (src) -> URI.create(src.toString()));
36: rules.put(new TransformationRuleIdentifier<>(String.class, URL.class), (src) -> {
37: try {
38: return new URL(src.toString());
39: } catch (MalformedURLException e) {
40: throw new IllegalArgumentException("Invalid URL " + src, e);
41: }
42: });
43: rules.put(new TransformationRuleIdentifier<>(String.class, Date.class), (src) -> {
44: try {
45: // This format corresponds to the one produced by java.util.Date#toString()
46: return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(src.toString());
47: } catch (ParseException e) {
48: throw new IllegalArgumentException("Unable to parse date " + src, e);
49: }
50: });
51: rules.put(new TransformationRuleIdentifier<>(Integer.class, Long.class), (src) -> ((Integer) src).longValue());
52: rules.put(new TransformationRuleIdentifier<>(Integer.class, Float.class),
53: (src) -> ((Integer) src).floatValue());
54: rules.put(new TransformationRuleIdentifier<>(Integer.class, Double.class),
55: (src) -> ((Integer) src).doubleValue());
56: rules.put(new TransformationRuleIdentifier<>(Long.class, Float.class), (src) -> ((Long) src).floatValue());
57: rules.put(new TransformationRuleIdentifier<>(Long.class, Double.class), (src) -> ((Long) src).doubleValue());
58: rules.put(new TransformationRuleIdentifier<>(Long.class, Date.class), (src) -> new Date((Long) src));
59: rules.put(new TransformationRuleIdentifier<>(ZonedDateTime.class, LocalDateTime.class),
60: src -> ((ZonedDateTime) src).toLocalDateTime());
61: rules.put(new TransformationRuleIdentifier<>(LangString.class, MultilingualString.class), src -> {
62: final LangString ls = (LangString) src;
63: return new MultilingualString(Collections.singletonMap(ls.getLanguage(), ls.getValue()));
64: });
65: rules.put(new TransformationRuleIdentifier<>(String.class, MultilingualString.class),
66: src -> new MultilingualString(Collections.singletonMap(null, src.toString())));
67: }
68:
69: /**
70: * Registers transformation rule for the specified source and target types.
71: * <p>
72: * Overrides any previously defined rule for the source and target classes.
73: *
74: * @param sourceClass Source class
75: * @param targetClass Target class
76: * @param rule The rule to apply
77: * @param <T> Source type
78: * @param <R> Target type
79: */
80: public static <T, R> void registerTransformationRule(Class<T> sourceClass, Class<R> targetClass,
81: Function<T, R> rule) {
82: Objects.requireNonNull(sourceClass);
83: Objects.requireNonNull(targetClass);
84: Objects.requireNonNull(rule);
85: rules.put(new TransformationRuleIdentifier<>(sourceClass, targetClass), rule);
86: }
87:
88: public static <T> T transformValue(Object value, Class<T> targetClass) {
89: Objects.requireNonNull(value);
90: Objects.requireNonNull(targetClass);
91: final Class<?> sourceClass = value.getClass();
92:• if (targetClass.isAssignableFrom(sourceClass)) {
93: return targetClass.cast(value);
94: }
95:• if (targetClass.isEnum()) {
96: return (T) transformToEnumConstant(value, (Class<? extends Enum>) targetClass);
97: }
98:• if (targetClass.equals(String.class)) {
99: return targetClass.cast(value.toString());
100: }
101: final TransformationRuleIdentifier<?, ?> identifier = new TransformationRuleIdentifier<>(sourceClass,
102: targetClass);
103:• return rules.containsKey(identifier) ? (T) rules.get(identifier).apply(value) : null;
104: }
105:
106: private static <T extends Enum<T>> T transformToEnumConstant(Object value, Class<T> targetClass) {
107: return Enum.valueOf(targetClass, value.toString());
108: }
109:
110: public static class TransformationRuleIdentifier<S, T> {
111: private final Class<S> sourceType;
112: private final Class<T> targetType;
113:
114: public TransformationRuleIdentifier(Class<S> sourceType, Class<T> targetType) {
115: this.sourceType = Objects.requireNonNull(sourceType);
116: this.targetType = Objects.requireNonNull(targetType);
117: }
118:
119: @Override
120: public boolean equals(Object o) {
121: if (this == o) return true;
122: if (o == null || getClass() != o.getClass()) return false;
123:
124: TransformationRuleIdentifier<?, ?> that = (TransformationRuleIdentifier<?, ?>) o;
125:
126: return sourceType.equals(that.sourceType) && targetType.equals(that.targetType);
127:
128: }
129:
130: @Override
131: public int hashCode() {
132: int result = sourceType.hashCode();
133: result = 31 * result + targetType.hashCode();
134: return result;
135: }
136: }
137: }