-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Can one use @Sort with comparator without Comparable?
PostPosted: Wed Jun 22, 2011 7:43 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
I have a @ManyToMany entity where I want to apply a numerical order to a String column. The most basic idea would be:

Code:
   
@OrderBy(value="code")
private List<Centre> centres = new ArrayList<Centre>();


But this doesn't work because codes 1,2,10 are ordered like 1,10,2.

I then thought I'd use the @Sort annotation. First attempt was:
Code:
   
@Sort(type = SortType.COMPARATOR,comparator=StringNumberComparator.class)
private List<Centre> centres = new ArrayList<Centre>();


But this doesn't seem to trigger the comparator and the items in the list are in insertion order 1,10,2.

I read in the documentation one needed to use a SortedSet, so I changed the code to:

Code:
   
@Sort(type = SortType.COMPARATOR,comparator=StringNumberComparator.class)
private SortedSet<Centre> centres = new TreeSet<Centre>();


Unfortunately, changing to this will give me new error elsewhere. In the code:
Code:
   public void addTrial(ClinicalTrial trial) {
      if(trial == null){
         return;
      }
      getClinicalTrials().add(trial);
      trial.getCentres().add(this);      
   }

I'll get the Exception:
Quote:
java.lang.ClassCastException: com.itclinical.iwrs.persist.model.CentreImpl cannot be cast to java.lang.Comparable


This is only solved if CentreImpl implements Comparable, but that makes the use of SortType.COMPARATOR useless.

Does anyone know how can we use the Comparator without having to implement Comparable on the underlying class?


Top
 Profile  
 
 Post subject: Re: Can one use @Sort with comparator without Comparable?
PostPosted: Wed Jun 22, 2011 8:08 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You should specify the comparator to use as a parameter in when creating the TreeSet:

Code:
new TreeSet<Centre>(new StringNumberComparator())


This is according to the javadoc for the TreeSet class which mentions that if you use the no-argument constructor, the elements must implement Comparable. If you supply your own comparator implementation it should work for any objects that the comparator supports.


Top
 Profile  
 
 Post subject: Re: Can one use @Sort with comparator without Comparable?
PostPosted: Wed Jun 22, 2011 8:19 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
nordborg wrote:
You should specify the comparator to use as a parameter in when creating the TreeSet:

Code:
new TreeSet<Centre>(new StringNumberComparator())


This is according to the javadoc for the TreeSet class which mentions that if you use the no-argument constructor, the elements must implement Comparable. If you supply your own comparator implementation it should work for any objects that the comparator supports.


You are right, nordborg.

This leads me to another question, however. Implementing the SortedSet with the Comparator means all operations (like add, remove) rely on the comparator instead of equals. This leads to some problems: I only want the Comparator to sort the collection (by code), not to assess equality.

For example, if CentreImpl's equals is based on name and code, the following test will break (saying size is 1):

Code:
@Test
   public void removeCentreFromTrialTest() {
      Centre centre = new CentreImpl("name", "someCode");
      Centre otherCentre = new CentreImpl("OTHERname", "someCode");
      generalDAO.save(centre);
      generalDAO.save(otherCentre);
      ClinicalTrial trial = new ClinicalTrialImpl();
      generalDAO.save(trial);
      
      
      clinicalTrialService.addCentreToTrial(trial.getId(),centre);
      clinicalTrialService.addCentreToTrial(trial.getId(),otherCentre);
      flush();
      assertEquals(2, trial.getCentres().size());
}


So, how can one sort using a comparator but keep the default equals as the base for other operations?


Top
 Profile  
 
 Post subject: Re: Can one use @Sort with comparator without Comparable?
PostPosted: Wed Jun 22, 2011 8:40 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
This can be done if it is possible to implement the comparator in a way that always distinguishes non-equal elements. In the example you give, if the codes are equal the comparator should also compare the names.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.