Skip to content

Commit 1dfd824

Browse files
committed
GH-5015 improvements to AbstractIRI and models
1 parent e71e876 commit 1dfd824

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

core/model-api/src/main/java/org/eclipse/rdf4j/model/base/AbstractIRI.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,23 @@ public boolean equals(Object o) {
4545
return false;
4646
}
4747

48+
private int cachedHashCode = 0;
49+
4850
@Override
4951
public int hashCode() {
50-
return stringValue().hashCode();
52+
int cached = cachedHashCode;
53+
if (cached == 0) {
54+
synchronized (this) {
55+
cached = cachedHashCode;
56+
if (cached == 0) {
57+
cached = stringValue().hashCode();
58+
cachedHashCode = cached;
59+
}
60+
}
61+
cached = stringValue().hashCode();
62+
cachedHashCode = cached;
63+
}
64+
return cached;
5165
}
5266

5367
@Override

core/model/src/main/java/org/eclipse/rdf4j/model/impl/AbstractModel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
public abstract class AbstractModel extends AbstractSet<Statement> implements Model {
3232

3333
private static final long serialVersionUID = 4254119331281455614L;
34+
public static final Resource[] NULL_CONTEXT = { null };
3435

3536
@Override
3637
public Model unmodifiable() {
@@ -179,6 +180,9 @@ public boolean remove(Object o) {
179180
public boolean contains(Object o) {
180181
if (o instanceof Statement) {
181182
Statement st = (Statement) o;
183+
if (st.getContext() == null) {
184+
return contains(st.getSubject(), st.getPredicate(), st.getObject(), NULL_CONTEXT);
185+
}
182186
return contains(st.getSubject(), st.getPredicate(), st.getObject(), st.getContext());
183187
}
184188
return false;

core/model/src/main/java/org/eclipse/rdf4j/model/impl/LinkedHashModel.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,13 @@ public boolean equals(Object other) {
420420
if (this == other) {
421421
return true;
422422
}
423+
if (other == null) {
424+
return false;
425+
}
426+
if (other == statement) {
427+
return true;
428+
}
429+
423430
if (!super.equals(other)) {
424431
return false;
425432
}

0 commit comments

Comments
 (0)