Skip to content

Package: IdentifierUtils

IdentifierUtils

nameinstructionbranchcomplexitylinemethod
IdentifierUtils()
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%
generateIdentifier(URI)
M: 0 C: 53
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
isResourceIdentifier(Object)
M: 0 C: 26
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 8
100%
M: 0 C: 1
100%
static {...}
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%

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.ontodriver.util;
16:
17: import cz.cvut.kbss.ontodriver.model.NamedResource;
18:
19: import java.net.URI;
20: import java.net.URL;
21: import java.util.Objects;
22: import java.util.Random;
23:
24: /**
25: * Utility for automatic identifier generation.
26: */
27: public class IdentifierUtils {
28:
29: private static final Random RANDOM = new Random();
30:
31: private IdentifierUtils() {
32: throw new AssertionError();
33: }
34:
35: /**
36: * Generates a (pseudo) random identifier based on the specified class URI.
37: * <p>
38: * The identifier consists of the class URI and then contains the string 'instance' and a random integer to ensure
39: * uniqueness. The 'instance' part is appended after a slash or a _, if the class URI contains a hash fragment.
40: *
41: * @param classUri Class URI used as identifier base
42: * @return Generated identifier
43: */
44: public static URI generateIdentifier(URI classUri) {
45: Objects.requireNonNull(classUri);
46:• if (classUri.getFragment() != null) {
47: return URI.create(classUri.toString() + "_instance" + RANDOM.nextInt());
48: } else {
49: String base = classUri.toString();
50:• if (base.endsWith("/")) {
51: return URI.create(base + "instance" + RANDOM.nextInt());
52: } else {
53: return URI.create(base + "/instance" + RANDOM.nextInt());
54: }
55: }
56: }
57:
58: /**
59: * Resolves whether the specified value is a resource identifier.
60: * <p>
61: * Only absolute IRIs are supported (i.e. no blank node identifiers).
62: *
63: * @param value The value to check
64: * @return {@code true} if the value is either an URI or an URL
65: */
66: public static boolean isResourceIdentifier(Object value) {
67:• if (value instanceof NamedResource || value instanceof java.net.URI || value instanceof URL) {
68: return true;
69: }
70:• if (!(value instanceof String)) {
71: return false;
72: }
73: try {
74: final java.net.URI uri = java.net.URI.create(value.toString());
75: return uri.isAbsolute();
76: } catch (IllegalArgumentException e) {
77: return false;
78: }
79: }
80: }