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%
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 java.net.URI;
18: import java.util.Objects;
19: import java.util.Random;
20:
21: /**
22: * Utility for automatic identifier generation.
23: */
24: public class IdentifierUtils {
25:
26: private static final Random RANDOM = new Random();
27:
28: private IdentifierUtils() {
29: throw new AssertionError();
30: }
31:
32: /**
33: * Generates a (pseudo) random identifier based on the specified class URI.
34: * <p>
35: * The identifier consists of the class URI and then contains the string 'instance' and a random integer to ensure
36: * uniqueness. The 'instance' part is appended after a slash or a _, if the class URI contains a hash fragment.
37: *
38: * @param classUri Class URI used as identifier base
39: * @return Generated identifier
40: */
41: public static URI generateIdentifier(URI classUri) {
42: Objects.requireNonNull(classUri);
43:• if (classUri.getFragment() != null) {
44: return URI.create(classUri.toString() + "_instance" + RANDOM.nextInt());
45: } else {
46: String base = classUri.toString();
47:• if (base.endsWith("/")) {
48: return URI.create(base + "instance" + RANDOM.nextInt());
49: } else {
50: return URI.create(base + "/instance" + RANDOM.nextInt());
51: }
52: }
53: }
54: }