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$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%
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: 86
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
transformValue(Object, Class)
M: 0 C: 45
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

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