Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-200

ObjectId breaks Java equals() symmetric property

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.2
    • API
    • None
    • 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;
      }

      Attachments

        Activity

          People

            eliot Eliot Horowitz (Inactive)
            t.broyer Thomas Broyer
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: