You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2005/05/14 18:27:34 UTC

DO NOT REPLY [Bug 34917] New: - Flat3Map.equals() bug

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=34917>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=34917

           Summary: Flat3Map.equals() bug
           Product: Commons
           Version: unspecified
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: major
          Priority: P2
         Component: Collections
        AssignedTo: commons-dev@jakarta.apache.org
        ReportedBy: stachoo@carrot-search.com


Flat3Map.equals() fails a simple test:

    public void testEquals()
    {
        Flat3Map map = new Flat3Map();
        map.put("a", "testA");
        map.put("b", "testB");
        Flat3Map mapClone = new Flat3Map();
        mapClone.put("a", "testB");
        mapClone.put("b", "testA");
        
        assertFalse("Maps should not be equal", map.equals(mapClone));
    }

The code of Flat3Map.equals() is:

   public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (delegateMap != null) {
            return delegateMap.equals(obj);
        }
        if (obj instanceof Map == false) {
            return false;
        }
        Map other = (Map) obj;
        if (size != other.size()) {
            return false;
        }
        if (size > 0) {
            Object otherValue = null;
            switch (size) {  // drop through
                case 3:
                    if (other.containsKey(key3) == false) {
                        otherValue = other.get(key3);
                        if (value3 == null ? otherValue != null :
!value3.equals(otherValue)) {
                            return false;
                        }
                    }
                case 2:
                    if (other.containsKey(key2) == false) {
                        otherValue = other.get(key2);
                        if (value2 == null ? otherValue != null :
!value2.equals(otherValue)) {
                            return false;
                        }
                    }
                case 1:
                    if (other.containsKey(key1) == false) {
                        otherValue = other.get(key1);
                        if (value1 == null ? otherValue != null :
!value1.equals(otherValue)) {
                            return false;
                        }
                    }
            }
        }
        return true;
    }

while it should probably be:

   public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (delegateMap != null) {
            return delegateMap.equals(obj);
        }
        if (obj instanceof Map == false) {
            return false;
        }
        Map other = (Map) obj;
        if (size != other.size()) {
            return false;
        }
        if (size > 0) {
            Object otherValue = null;
            switch (size) {  // drop through
                case 3:
                    if (other.containsKey(key3)) {
                        otherValue = other.get(key3);
                        if (value3 == null ? otherValue != null :
!value3.equals(otherValue)) {
                            return false;
                        }
                    }
                case 2:
                    if (other.containsKey(key2)) {
                        otherValue = other.get(key2);
                        if (value2 == null ? otherValue != null :
!value2.equals(otherValue)) {
                            return false;
                        }
                    }
                case 1:
                    if (other.containsKey(key1)) {
                        otherValue = other.get(key1);
                        if (value1 == null ? otherValue != null :
!value1.equals(otherValue)) {
                            return false;
                        }
                    }
            }
        }
        return true;
    }

--
Stanislaw Osinski
http://www.carrot-search.com

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org