Google

Sep 17, 2013

Google Guava library handy methods to write your Comparators for sorting

Sorting objects in a collection key in presenting those objects in GUI or report like PDF, etc. In Java you use a Comparator interface or the Comparable interface to define the custom sorting logic. I hava covered this in a number of following blog posts.
 Recently, I had the opportunity to work with a number of Google Guava libraries and its comparator  came handy to write a more concise code to get the same thing done. Working with Java Collection using Gauva was covered with examples.

Step 1: The Guava library dependency in pom.xml file.

<!--  Google GUAVA -->
<dependency>
 <groupId>com.google.guava</groupId>
 <artifactId>guava</artifactId>
  <version>13.0.1</version>
</dependency>


Step 2: Here is a sample ReportKey value object class that uses Apache common librarie's utility methods for equals( ), hashCode( ), toString( ), and compareTo( ) methods. Implementing the Comparable interface and providing the compareTo method provides natural ordering for the ReportKey class. It is also a best pracrice to define your key class as an immutable object. Defensive copying is used to make it immutable.

package com.myapp.reporting;

import java.util.Date;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.util.Assert;

public class ReportKey implements Comparable<ReportKey>
{    
    private String clientId;
    private Date valuationDate;
    
    public ReportKey()
    {
    }
    
    private ReportKey(String clientId, Date valuationDate)
    {
        this.clientId = clientId;
        this.valuationDate = new Date(valuationDate.getTime()); // defensive
                                                                // copy
    }
    
    public static ReportKey newInstance(String clientId, Date valuationDate)
    {
        Assert.notNull(clientId);
        Assert.notNull(valuationDate);
        return new ReportKey(clientId, valuationDate);
    }
    
    public String getClientId()
    {
        return clientId;
    }
    
    public Date getValuationDate()
    {
        return new Date(valuationDate.getTime()); // defensive copy
    }
    
    @Override
    public int hashCode()
    {
        return new HashCodeBuilder().append(clientId).append(valuationDate).toHashCode();
    }
    
    @Override
    public boolean equals(final Object obj)
    {
        if (obj instanceof ReportKey)
        {
            final ReportKey other = (ReportKey) obj;
            return new EqualsBuilder().append(clientId, other.clientId).append(valuationDate, other.valuationDate)
                    .isEquals();
        }
        return false;
    }
    
    @Override
    public String toString()
    {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
    
    @Override
    public int compareTo(ReportKey reportKey)
    {
        return new CompareToBuilder().append(this.clientId, reportKey.clientId)
                .append(this.valuationDate, reportKey.valuationDate).toComparison();
    }
}



Step 3: The above natual ordering sorts by clientId and valuationDate in ascending order. What if you ant to sort it by clientId in ascending order and by valuationDate in descending order? Here is sample anonymous class using Google's Gauva library.

List<ReportKey> reportKeys = (List<ReportKey>) reportService.find();
Collections.sort(reportKeys, new Comparator<ReportKey>()
{
    @Override
    public int compare(ReportKey o1, ReportKey o2)
    {
        return ComparisonChain.start()
                .compare(o1.getClientId(), o2.getClientId())
                .compare(o1.getValuationDate(), o2.getValuationDate(), Ordering.natural().reverse())
                .result();
    }
});


Labels:

2 Comments:

Anonymous Anonymous said...

where are you using Guava here ?

8:46 AM, November 06, 2013  
Blogger Arulkumaran Kumaraswamipillai said...

ComparisonChain is a a Gauva library class.

10:29 AM, November 06, 2013  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home