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%
getValidIdentifierTypes()
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%
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: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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) 2016 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.*;
21: import java.util.function.Function;
22:
23: public class IdentifierTransformer {
24:
25: private static final Set<Class<?>> ID_CLASSES = new HashSet<>(Arrays.asList(URI.class, URL.class, String.class));
26:
27: private static final Map<Class<?>, Function<Object, ?>> TRANSFORMERS = initTransformers();
28:
29: private static Map<Class<?>, Function<Object, ?>> initTransformers() {
30: final Map<Class<?>, Function<Object, ?>> m = new HashMap<>(ID_CLASSES.size());
31: m.put(URI.class, val -> {
32: Objects.requireNonNull(val);
33:• if (val instanceof URI) {
34: return val;
35: } else {
36: return URI.create(val.toString());
37: }
38: });
39: m.put(URL.class, val -> {
40: Objects.requireNonNull(val);
41:• if (val instanceof URL) {
42: return val;
43: }
44: try {
45: return new URL(val.toString());
46: } catch (MalformedURLException e) {
47: throw new IllegalArgumentException("The identifier is not a valid URL.", e);
48: }
49: });
50: m.put(String.class, val -> {
51: Objects.requireNonNull(val);
52: return val.toString();
53: });
54: return m;
55: }
56:
57: private IdentifierTransformer() {
58: throw new AssertionError();
59: }
60:
61: /**
62: * Transforms the specified value to the target identifier type (if possible).
63: *
64: * @param value The value to transform
65: * @param targetType Target type
66: * @return The transformed value
67: * @throws IllegalArgumentException If the target type is not a valid identifier type
68: */
69: public static Object transformToIdentifier(Object value, Class<?> targetType) {
70:• if (!isValidIdentifierType(targetType)) {
71: throw new IllegalArgumentException(
72: "The specified value " + value + " cannot be transformed to target type " + targetType);
73: }
74: return TRANSFORMERS.get(targetType).apply(value);
75: }
76:
77: public static URI valueAsUri(Object value) {
78: return (URI) TRANSFORMERS.get(URI.class).apply(value);
79: }
80:
81: /**
82: * Checks whether the specified type is an identifier type supported by JOPA.
83: *
84: * @param type The type to check
85: * @return {@code true} if type is supported identifier type, {@code false} otherwise
86: */
87: public static boolean isValidIdentifierType(Class<?> type) {
88:• return type != null && ID_CLASSES.contains(type);
89: }
90:
91: /**
92: * Gets supported identifier types.
93: *
94: * @return Unmodifiable set of supported id types
95: */
96: public static Set<Class<?>> getValidIdentifierTypes() {
97: return Collections.unmodifiableSet(ID_CLASSES);
98: }
99: }