Skip to content

Method: initTransformers()

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