arne-bdt opened a new issue, #2808:
URL: https://github.com/apache/jena/issues/2808

   ### Version
   
   5.3.0-SNAPSHOT
   
   ### Question
   
   In [apache/jena#2795](https://github.com/apache/jena/issues/2795), I 
discovered that no implementation of `RDFDatatype` provides an override for 
`Object#equals` or `Object#hashCode`, resulting in all comparisons being 
performed by reference only.
   
   For instance, these tests fail:
   
   ```java
   @Test public void baseDataTypeEquality() {
       var rdfDataType1 = new BaseDatatype("urn:x-hp-dt:unknown");
       var rdfDataType2 = new BaseDatatype("urn:x-hp-dt:unknown");
   
       assertEquals(rdfDataType1, rdfDataType2);
   }
   
   @Test public void xsdDoubleEquality() {
       var rdfDataType1 = new XSDDouble("double", Double.class);
       var rdfDataType2 = XSDDatatype.XSDdouble;
   
       assertEquals(rdfDataType1, rdfDataType2);
   }
   ```
   
   To further illustrate the inconsistency, here’s a scenario where the first 
test fails, but the second one succeeds:
   
   ```java
   @Test
   public void testEqualityA() {
       var l1 = LiteralLabelFactory.create("foo", new 
BaseDatatype("urn:x-hp-dt:unknown"));
       var l2 = LiteralLabelFactory.create("foo", new 
BaseDatatype("urn:x-hp-dt:unknown"));
   
       assertEquals(l1, l2);
   }
   
   @Test
   public void testEqualityB() {
       var m = ModelFactory.createDefaultModel();
       var l1 = m.createTypedLiteral("foo", "urn:x-hp-dt:unknown");
       var l2 = m.createTypedLiteral("foo", "urn:x-hp-dt:unknown");
   
       assertEquals(l1, l2);
   }
   ```
   
   I propose implementing `Object#equals` and `Object#hashCode` in 
`BaseDatatype` to compare instances by their class and URI, as shown below:
   
   ```java
   @Override
   public boolean equals(Object o) {
       if (this == o) return true;
       if (o == null || getClass() != o.getClass()) return false;
       final BaseDatatype that = (BaseDatatype) o;
       return Objects.equals(uri, that.uri);
   }
   
   @Override
   public int hashCode() {
       return Objects.hashCode(uri);
   }
   ```
   
   I couldn’t find any subclass of `BaseDatatype` where uniqueness isn’t 
defined by the URI, so this approach should cover all cases without needing 
additional overrides.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to