You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Julien Aymé (JIRA)" <ji...@apache.org> on 2008/01/03 16:16:34 UTC

[jira] Commented: (LANG-394) Equalsbuilder fails at using unmodifiable collection

    [ https://issues.apache.org/jira/browse/LANG-394?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12555588#action_12555588 ] 

Julien Aymé commented on LANG-394:
----------------------------------

Hi, this is not an EqualsBuilder bug, but rather a misuse of the Collection interface:

The general contract for Collection#equals(Object) is the same as Object#equals(Object), ie a Collection is only equals to itself.
(see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#equals(java.lang.Object))

Whereas the contract for List#equals(Object) is that a List is equals to any List of the same size which contains the same objects in the same order.
(see http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html#equals(java.lang.Object))

That's why your current test fails; instead of returning an unmodifiableCollection, try returning an unmodifiableList (which compares to other lists by value and not by reference).

Hope this help,
Julien

> Equalsbuilder fails at using unmodifiable collection
> ----------------------------------------------------
>
>                 Key: LANG-394
>                 URL: https://issues.apache.org/jira/browse/LANG-394
>             Project: Commons Lang
>          Issue Type: Bug
>    Affects Versions: 2.3
>         Environment: Win XP SP2, JRE 1.5.0_12
>            Reporter: Benno Markiewicz
>
> When using the equalsbuilder to build a equals-method it comes to failures, when comparing object which have unmodifiable list properties.
> Look at the simplified example. A class Foo with a collection (A,B,C) is build up. The equals method uses the equalsbuilder to append the list to it. The getter of the list returns a unmodifiable collection. This situation leads to the test failure. Without capsulation in a unmodifiable collection the test succeeds. What's wrong? I want to compare such objects by value not by reference.
>  @Test
>   public void testInEqualsBuilder() {
>     class Foo {
>       private final Collection<String> list;
>       public Foo() {
>         this.list = new ArrayList<String>();
>         this.list.add("A");
>         this.list.add("B");
>         this.list.add("C");
>       }
>       @Override
>       public boolean equals(final Object object) {
>         if (object == this) {
>           return true;
>         }
>         if (!(object instanceof Foo)) {
>           return false;
>         }
>         final Foo rhs = (Foo) object;
>         
>         final EqualsBuilder eb = new EqualsBuilder();
>         eb.append(this.list, rhs.getList());
>         return eb.isEquals();
>       }
>       private Collection<String> getList() {
>          return Collections.unmodifiableCollection(this.list);
>        }
>     }
>     final Foo x = new Foo();
>     assertTrue(x.equals(x));
>    
>     // FAILURE list [A,B,C] is not equal to list [A,B,C], when using
>     // unmodifiableCollection?
>     assertTrue(x.equals(new Foo()));
>   }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.