« Return to Thread: [Fwd: [grails-user] Problem with findAll() in Grails after 1.1 (or Groovy after 1.6?)]

[Fwd: [grails-user] Problem with findAll() in Grails after 1.1 (or Groovy after 1.6?)]

by Cyril Picat-2 :: Rate this Message:

Reply to Author | View in Thread


-------- Original Message --------
Subject: [grails-user] Problem with findAll() in Grails after 1.1 (or
Groovy after 1.6?)
Date: Tue, 30 Jun 2009 17:11:59 +0200
From: Cyril Picat <cpicat@...>
To: user@..., user@...


I have been hitting a strange problem lately with some Grails code
working on Grails 1.0.4 (Groovy 1.5.7).

I have the feeling that the behaviour of findAll() have changed between
these two versions of Grails/Groovy. I suspect this is more a Groovy bug
but I have posted it to both lists actually.

If you do findAll() on a TreeMap now (after 1.6), you will get a new
TreeMap with your search result in it. Before you were getting a HashMap
I think. Until there why not.

The thing is that if you have set a custom comparator in your TreeMap,
the newed TreeMap doesn't get its comparator set so you get a
ClassCastException as the elements in your map do not implement Comparable.

I have put below a groovy file that I have tested in groovyConsole 1.6.3
that seems to reproduce the problem.

Is this a bug? A functionality?

Thanks in advance,

Cyril

-----

class MyComparator implements Comparator {

     public int compare(Object o1, Object o2){

         if( (!o1)&&(!o2) ) return 0

         if( !(o1&&o2) ) return o2 ? 1 : -1

         def sub
         try {
             def id1 = o1.id
             def id2 = o2.id
             if(id1 && id2) {
                 sub = id1 - id2
             }
             else {
                 // handle it
             }
         }
         catch(Throwable e) {

             sub = o1.hashCode()- o2.hashCode()
         }

         if( sub == 0 ) return 0
         return (sub > 0) ? 1 : -1
     }
}

class City {
     String name
}

class MyDomainClass {

     int    id
     String name
     City   city
}


def john = new MyDomainClass(id: 1, name: "John", city: new City(name:
"Atlanta"))
def jack = new MyDomainClass(id: 2, name: "Jack", city: new City(name:
"Montreal"))

def myTree = new TreeMap(new MyComparator())
myTree.put(john.city, john)
myTree.put(jack.city, jack)

def listJohn = myTree.findAll() { it.value.name == "John" }

assert listJohn.size() == 1

def paul = new MyDomainClass(id: 3, name: "Paul", city: new City(name:
"York"))
listJohn.add(paul)





---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: [Fwd: [grails-user] Problem with findAll() in Grails after 1.1 (or Groovy after 1.6?)]