[JAVA-4713] Implement equals and hashCode in model classes across the entire driver Created: 24/Aug/22  Updated: 30/Aug/22

Status: Backlog
Project: Java Driver
Component/s: API
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Unknown
Reporter: Dmitry Ryabtsev Assignee: Unassigned
Resolution: Unresolved Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Case:

 Description   

At present equals() is inherited from Object, thus comparing two CreateIndexOptions is not straightforward as the inherited method does not actually compare the content of the objects:

        CreateIndexOptions io = new CreateIndexOptions();
        CreateIndexOptions io2 = new CreateIndexOptions();
        boolean result = io.equals(io2);
        boolean result2 = io.equals(io);
        System.out.println(result);
        System.out.println(result2);
        System.out.println(io.toString());
        System.out.println(io2.toString());

The above yields:

false
true
CreateIndexOptions{maxTimeMS=0, commitQuorum=null}
CreateIndexOptions{maxTimeMS=0, commitQuorum=null}

The same goes for nearly every model class in the driver.



 Comments   
Comment by Jeffrey Yemin [ 24/Aug/22 ]

dmitry.ryabtsev@mongodb.com we would prefer not to take on the burden of maintaining equals/hashCode methods in all of our classes. It's a surprisingly large maintenance burden, given:

  • there are numerous (over a hundred) classes that could potentially require these additions
  • they have to be hand-written in Java (though the IDE will auto-generate them
  • it's easy to forget to update them when a new field is added, and I'm not aware of a linter that will catch mistakes.

Happy to leave it in the backlog to see if it collects votes, but not inclined to take it on right now.

Generated at Thu Feb 08 09:02:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.