[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(); obj.equals(str) => true 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: 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. |