[JAVA-200] ObjectId breaks Java equals() symmetric property Created: 23/Oct/10  Updated: 29/Oct/10  Resolved: 23/Oct/10

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

Type: Bug Priority: Major - P3
Reporter: Thomas Broyer Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

equals() in Java must be symmetric, but an ObjectId might compare equal to a String, but (obviously) not the other way around.

ObjectId obj = new ObjectId();
String str = obj.toString();
assertEquals(obj.equals(str), str.equals(obj);

obj.equals(str) => true
str.equals(obj) => false

This is because ObjectId.equals() calls massageToObjectId which will turn the String into an ObjectId (if it isValid); but String.equals() doesn't turn "any object" to its String representation.

The fix is fortunately easy: implement equals() without calling massageToObjectId:
public boolean equals( Object o ){
if ( this == o )
return true;

if ( o instanceof ObjectId )

{ ObjectId other = (ObjectId)o; return _time == other._time && _machine == other._machine && _inc == other._inc; }

return false;
}



 Comments   
Comment by Eliot Horowitz (Inactive) [ 23/Oct/10 ]

The general consensus a while back was that this was the desired behavior.
Would have to get a big majority of people to want to change behavior.

Generated at Thu Feb 08 08:51:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.