Skip to content

Package: IdentifierTransformer

IdentifierTransformer

nameinstructionbranchcomplexitylinemethod
IdentifierTransformer()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
initTransformers()
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
isValidIdentifierType(Class)
M: 0 C: 10
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$initTransformers$0(Object)
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
lambda$initTransformers$1(Object)
M: 9 C: 12
57%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 3
50%
M: 0 C: 1
100%
lambda$initTransformers$2(Object)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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%
transformToIdentifier(Object, Class)
M: 0 C: 26
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
valueAsUri(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%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
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: *
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.jopa.utils;
16:
17: import java.net.MalformedURLException;
18: import java.net.URI;
19: import java.net.URL;
20: import java.util.HashMap;
21: import java.util.Map;
22: import java.util.Objects;
23: import java.util.function.Function;
24:
25: import static cz.cvut.kbss.jopa.utils.Constants.SUPPORTED_IDENTIFIER_TYPES;
26:
27: public class IdentifierTransformer {
28:
29: private static final Map<Class<?>, Function<Object, ?>> TRANSFORMERS = initTransformers();
30:
31: private IdentifierTransformer() {
32: throw new AssertionError();
33: }
34:
35: private static Map<Class<?>, Function<Object, ?>> initTransformers() {
36: final Map<Class<?>, Function<Object, ?>> m = new HashMap<>(SUPPORTED_IDENTIFIER_TYPES.size());
37: m.put(URI.class, val -> {
38: Objects.requireNonNull(val);
39:• if (val instanceof URI) {
40: return val;
41: } else {
42: return URI.create(val.toString());
43: }
44: });
45: m.put(URL.class, val -> {
46: Objects.requireNonNull(val);
47:• if (val instanceof URL) {
48: return val;
49: }
50: try {
51: return new URL(val.toString());
52: } catch (MalformedURLException e) {
53: throw new IllegalArgumentException("The identifier is not a valid URL.", e);
54: }
55: });
56: m.put(String.class, val -> {
57: Objects.requireNonNull(val);
58: return val.toString();
59: });
60: return m;
61: }
62:
63: /**
64: * Transforms the specified value to the target identifier type (if possible).
65: *
66: * @param value The value to transform
67: * @param targetType Target type
68: * @return The transformed value
69: * @throws IllegalArgumentException If the target type is not a valid identifier type
70: */
71: public static Object transformToIdentifier(Object value, Class<?> targetType) {
72:• if (!isValidIdentifierType(targetType)) {
73: throw new IllegalArgumentException(
74: "The specified value " + value + " cannot be transformed to target type " + targetType);
75: }
76: return TRANSFORMERS.get(targetType).apply(value);
77: }
78:
79: public static URI valueAsUri(Object value) {
80: return (URI) TRANSFORMERS.get(URI.class).apply(value);
81: }
82:
83: /**
84: * Checks whether the specified type is an identifier type supported by JOPA.
85: *
86: * @param type The type to check
87: * @return {@code true} if type is supported identifier type, {@code false} otherwise
88: */
89: public static boolean isValidIdentifierType(Class<?> type) {
90:• return type != null && SUPPORTED_IDENTIFIER_TYPES.contains(type);
91: }
92: }