1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rdf.simple;
19
20 import java.nio.charset.StandardCharsets;
21 import java.util.Objects;
22 import java.util.UUID;
23 import java.util.concurrent.atomic.AtomicLong;
24
25 import org.apache.commons.rdf.api.BlankNode;
26 import org.apache.commons.rdf.simple.SimpleRDF.SimpleRDFTerm;
27
28
29
30
31 final class BlankNodeImpl implements BlankNode, SimpleRDFTerm {
32
33 private static final UUID SALT = UUID.randomUUID();
34 private static final AtomicLong COUNTER = new AtomicLong();
35
36 private final String uniqueReference;
37
38 public BlankNodeImpl() {
39 this(SALT, Long.toString(COUNTER.incrementAndGet()));
40 }
41
42 public BlankNodeImpl(final UUID uuidSalt, final String name) {
43 if (Objects.requireNonNull(name).isEmpty()) {
44 throw new IllegalArgumentException("Invalid blank node id: " + name);
45 }
46
47
48
49
50
51 final String uuidInput = "urn:uuid:" + uuidSalt + "#" + name;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 this.uniqueReference = UUID.nameUUIDFromBytes(uuidInput.getBytes(StandardCharsets.UTF_8)).toString();
68 }
69
70 @Override
71 public String uniqueReference() {
72 return uniqueReference;
73 }
74
75 @Override
76 public String ntriplesString() {
77 return "_:" + uniqueReference;
78 }
79
80 @Override
81 public String toString() {
82 return ntriplesString();
83 }
84
85 @Override
86 public int hashCode() {
87 return uniqueReference.hashCode();
88 }
89
90 @Override
91 public boolean equals(final Object obj) {
92 if (this == obj) {
93 return true;
94 }
95 if (obj == null) {
96 return false;
97 }
98
99 if (!(obj instanceof BlankNodeImpl)) {
100 return false;
101 }
102 final BlankNodeImpl other = (BlankNodeImpl) obj;
103 if (uniqueReference == null) {
104 if (other.uniqueReference != null) {
105 return false;
106 }
107 } else if (!uniqueReference.equals(other.uniqueReference)) {
108 return false;
109 }
110 return true;
111 }
112
113 }