[ 
https://issues.apache.org/jira/browse/GROOVY-7530?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14729668#comment-14729668
 ] 

Kevin Tung commented on GROOVY-7530:
------------------------------------

{code:title=compare.groovy}
class Foo {

    private String name

    Foo(String name) {
        this.name = name
    }

    boolean equals(o) {
        if (this.is(o)) return true
        if (getClass() != o.class) return false

        Foo that = (Foo) o

        if (name != that.name) return false

        return true
    }

    int hashCode() {
        return Objects.hash(name)
    }

}


def a = [new Foo("foo")]
def b = [new Foo("foo")] as Object[]

a.removeAll(b)
assert !a

def c = [new Foo("foo")]
def d = [new Foo("foo")] as Object[]

c.retainAll(d)
assert c
{code}

Seems like this will also fail for certain implementations of removeAll and 
retainAll.  Since NumberAwareComparator seems to be the root cause of this 
issue, there could be quite a few extension methods required to workaround this 
issue.  Would you happen to know if there are additional instances where 
NumberAwareComparator is the only source of comparison?

Also, while it may be possible to implement Comparable on objects in which I 
control the source, any objects that are imported are beyond my control.

> disjoint() does not work correctly if objects don't implement Comparable
> ------------------------------------------------------------------------
>
>                 Key: GROOVY-7530
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7530
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.4.4
>            Reporter: Tobias Ahlers
>
> {code:java}
> class Foo {
>     private String name
>     Foo(String name) {
>         this.name = name
>     }
>     public boolean equals(Object o) {
>         if (this == o) return true
>         if (o == null || getClass() != o.getClass()) return false
>         Foo that = (Foo) o
>         return Objects.equals(name, that.name)
>     }
>     public int hashCode() {
>         return Objects.hash(name)
>     }
> }
> def a = [new Foo("foo")]
> def b = [new Foo("foo")]    
> assert !a.disjoint(b)
> {code}
> If disjoint() is used on a list with objects not implementing Comparable the 
> wrong result is returned.
> intersect() shows the same wrong behavior.
> It's looks like the NumberAwareComparator not implementing the equals case as 
> of commit 286532c is the problem.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to