This is an automated email from the ASF dual-hosted git repository.
arnebdt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
The following commit(s) were added to refs/heads/main by this push:
new 192824ad50 GH-2808: Implemented Object#equals and #hashCode in
BaseDatatype
192824ad50 is described below
commit 192824ad50739291f0773556540c0b8c81d94b1d
Author: arne-bdt <[email protected]>
AuthorDate: Tue Oct 29 21:23:02 2024 +0100
GH-2808: Implemented Object#equals and #hashCode in BaseDatatype
- Added implementations for `org.apache.jena.datatypes.BaseDatatype#equals`
and `#hashCode` based on `RDFDatatype.getURI`, aligning with the
principle:
"Same URI, same datatype" for RDF datatypes.
- Added corresponding tests in `org.apache.jena.datatypes.TestDatatypes`
to verify equality and hash code behavior.
---
.../org/apache/jena/datatypes/BaseDatatype.java | 27 +++++++++
.../org/apache/jena/datatypes/TestDatatypes.java | 67 ++++++++++++++++++++++
2 files changed, 94 insertions(+)
diff --git
a/jena-core/src/main/java/org/apache/jena/datatypes/BaseDatatype.java
b/jena-core/src/main/java/org/apache/jena/datatypes/BaseDatatype.java
index 11a69bd96c..c315666955 100644
--- a/jena-core/src/main/java/org/apache/jena/datatypes/BaseDatatype.java
+++ b/jena-core/src/main/java/org/apache/jena/datatypes/BaseDatatype.java
@@ -247,4 +247,31 @@ public class BaseDatatype implements RDFDatatype {
+ (getJavaClass() == null ? "" : " -> " + getJavaClass())
+ "]";
}
+
+ /**
+ * Same URI, same datatype.
+ * <p />
+ * See <a
href="https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal">RDF 1.1
Concepts and Abstract Syntax - 3.3 Literals</a>
+ * and <a
href="https://www.w3.org/TR/rdf11-concepts/#section-Datatypes">RDF 1.1 Concepts
and Abstract Syntax - 5. Datatypes</a>
+ * @param other
+ * @return true if the URIs are the same
+ */
+ @Override
+ public final boolean equals(Object other) {
+ if (this == other) return true;
+ if (other instanceof RDFDatatype that) {
+ return Objects.equals(this.getURI(), that.getURI());
+ }
+ return false;
+ }
+
+ /**
+ * The hash code of a datatype is the hash code of its URI.
+ * @see #equals(Object)
+ * @return the hash code of the URI
+ */
+ @Override
+ public final int hashCode() {
+ return Objects.hashCode(this.getURI());
+ }
}
diff --git
a/jena-core/src/test/java/org/apache/jena/datatypes/TestDatatypes.java
b/jena-core/src/test/java/org/apache/jena/datatypes/TestDatatypes.java
index dffc15a5f2..37315a2fdd 100644
--- a/jena-core/src/test/java/org/apache/jena/datatypes/TestDatatypes.java
+++ b/jena-core/src/test/java/org/apache/jena/datatypes/TestDatatypes.java
@@ -20,6 +20,7 @@ package org.apache.jena.datatypes;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertFalse ;
+import static org.junit.Assert.assertNotEquals ;
import static org.junit.Assert.assertNotNull ;
import static org.junit.Assert.assertTrue ;
@@ -27,6 +28,7 @@ import java.math.BigDecimal ;
import java.util.UUID;
import org.apache.jena.datatypes.xsd.XSDDatatype ;
+import org.apache.jena.datatypes.xsd.impl.XSDDouble ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.NodeFactory ;
import org.apache.jena.rdf.model.Resource ;
@@ -197,6 +199,71 @@ public class TestDatatypes {
testLiteralIsCorrectType("5.55", XSDDatatype.XSDfloat) ;
}
+ @Test public void baseDataTypeEquality() {
+ var rdfDataType1 = new BaseDatatype("urn:x-hp-dt:unknown");
+ assertEquals(rdfDataType1, rdfDataType1);
+
+ var rdfDataType2 = new BaseDatatype("urn:x-hp-dt:unknown");
+ assertEquals(rdfDataType1, rdfDataType2);
+ assertEquals(rdfDataType2, rdfDataType1);
+
+ assertEquals(rdfDataType1.hashCode(), rdfDataType2.hashCode());
+ }
+
+ @Test public void baseDataTypeEmptyEquality() {
+ var rdfDataType1 = new BaseDatatype("");
+ assertEquals(rdfDataType1, rdfDataType1);
+
+ var rdfDataType2 = new BaseDatatype("");
+ assertEquals(rdfDataType1, rdfDataType2);
+ assertEquals(rdfDataType2, rdfDataType1);
+
+ assertEquals(rdfDataType1.hashCode(), rdfDataType2.hashCode());
+ }
+
+ @Test public void baseDataTypeNullEquality() {
+ var rdfDataType1 = new BaseDatatype(null);
+ assertEquals(rdfDataType1, rdfDataType1);
+
+ var rdfDataType2 = new BaseDatatype(null);
+ assertEquals(rdfDataType1, rdfDataType2);
+ assertEquals(rdfDataType2, rdfDataType1);
+
+ assertEquals(rdfDataType1.hashCode(), rdfDataType2.hashCode());
+ }
+
+ @Test public void xsdDoubleEquality() {
+ var rdfDataType1 = new XSDDouble("double", Double.class);
+ assertEquals(rdfDataType1, rdfDataType1);
+
+ var rdfDataType2 = XSDDatatype.XSDdouble;
+ assertEquals(rdfDataType1, rdfDataType2);
+ assertEquals(rdfDataType2, rdfDataType1);
+
+ assertEquals(rdfDataType1.hashCode(), rdfDataType2.hashCode());
+ }
+
+ @Test public void baseDataTypeNotEquals() {
+ var rdfDataType1 = new BaseDatatype("urn:x-hp-dt:unknownA");
+ var rdfDataType2 = new BaseDatatype("urn:x-hp-dt:unknownB");
+
+ assertNotEquals(rdfDataType1, rdfDataType2);
+ assertNotEquals(rdfDataType2, rdfDataType1);
+ }
+
+ @Test public void baseDataTypeNullNotEquals() {
+ var rdfDataType1 = new BaseDatatype("urn:x-hp-dt:unknownA");
+ var rdfDataType2 = new BaseDatatype(null);
+
+ assertNotEquals(rdfDataType1, rdfDataType2);
+ assertNotEquals(rdfDataType2, rdfDataType1);
+ }
+
+ @Test public void hashCodeEqualsUriHashCode() {
+ var rdfDataType = new BaseDatatype("urn:x-hp-dt:unknown");
+ assertEquals(rdfDataType.getURI().hashCode(), rdfDataType.hashCode());
+ }
+
private void testValueToLex(Object value, XSDDatatype datatype) {
Node node = NodeFactory.createLiteralByValue(value, datatype) ;
assertTrue("Not valid lexical form "+value+" -> "+node,
datatype.isValid(node.getLiteralLexicalForm())) ;