Aug 18th, 2009, 02:22 PM
make roo generated Comparable methods
Now that the tiles code seems stable (sort of) i ´m continuing with my work in a ordering feature for the generated lists in Roo.
I want to generate Comparable entities, that implies two things:
First Make entities implement Comparable
Second generate the compareTo method
The second part is easy, but i have no idea how can i make the entities implement an interface (i missed duck typing )
Can anyone point me to some addon wich makes a class implement an interface to study the code??
By the way, till the moment i´ve modified the controller addon to add a sort method and the jsp addon to generate a clickable headers pointing to the sort method Roo´s code seems a bit complicated the first time, but later it becomes very interesting
Aug 18th, 2009, 08:41 PM
It's pretty non-trivial to automate a comparable method because you'd need to include all fields in order to ensure equals and hashCode symmetry. Including all fields would mean they too are comparable, which isn't such an issue for JDK classes but becomes an issue for your normal classes. Specifically, you'd need all your normal classes to be comparable, including those entities which are involved in circular relationships. Consider for example Order-LineItem, where the Order has a Set<LineItem> (or equivalent) and the LineItem has a field related to the Order. If you attempt to compare the Order, it will iterate its LineItems, which in turn will re-call the Order etc. Of course if we sat down to think about this fully we could use a ThreadLocal to store a Set of Objects already visited, but it's getting somewhat complicated. My present advice is to implement the method by hand, ensuring you carefully observe the symmetry expectations defined on the JDK Object class (and Comparable).
Aug 19th, 2009, 05:35 AM
Thanks for your answer, i´m not thinking in implement a fully fledged compareTo Method, the sort will be field based, the controller will use a FieldBasedComparator, for example to sort by name or by title.
Implement the comparator is easy using apache beanutils wich is already a dependency, i get the field value and use it´s compareTo method. That works for all basic properties, Strings, integers, Dates, etc, you can´t order by collection properties. I only need a way to make entities Comparable to resolve circular references, for example using your example if someone wants to sort LineItems using it´s Order property.
To achieve that I plan to implement a very basic compareTo method based only on entity´s id, later the user could manually customize the method to fit his needs.
Do you think this is more suitable??