net.sourceforge.stripes.util.bean
Class BeanComparator

java.lang.Object
  extended by net.sourceforge.stripes.util.bean.BeanComparator
All Implemented Interfaces:
Comparator<Object>

public class BeanComparator
extends Object
implements Comparator<Object>

A comparator which compares objects based on one or more bean properties. Nested properties are fully supported. If a property is non-String and implements Comparable then the compareTo() method is delegated to. Otherwise the property is converted to a String and a Locale aware Collator is used to to compare property values.

Since:
Stripes 1.5
Author:
Tim Fennell

Constructor Summary
BeanComparator(Locale locale, String... properties)
          Constructs a BeanComparator for comparing beans based on the supplied set of properties, using the supplied Locale to collate Strings.
BeanComparator(String... properties)
          Constructs a BeanComparator for comparing beans based on the supplied set of properties, using the default system Locale to collate Strings.
 
Method Summary
 int compare(Object o1, Object o2)
          Compares two JavaBeans for order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

BeanComparator

public BeanComparator(String... properties)
Constructs a BeanComparator for comparing beans based on the supplied set of properties, using the default system Locale to collate Strings.

Parameters:
properties - one or more property names to be used, in order, to sort beans

BeanComparator

public BeanComparator(Locale locale,
                      String... properties)
Constructs a BeanComparator for comparing beans based on the supplied set of properties, using the supplied Locale to collate Strings.

Parameters:
locale - the Locale to be used for collating Strings
properties - one or more property names to be used, in order, to sort beans
Method Detail

compare

public int compare(Object o1,
                   Object o2)

Compares two JavaBeans for order. Returns a negative integer, zero, or a positive integer as the first argument sorts earlier, equal to, or later than the second.

Iterates through the properties supplied in the constructor comparing the values of each property for the two beans. As soon as a property is found that supplied a non-equal ordering, the ordering is returned. If all properties are equal, will return 0.

Specified by:
compare in interface Comparator<Object>
Parameters:
o1 - the first object to be compared, must not be null.
o2 - the second object to be compared, must not be null.
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
ClassCastException - if the arguments' types, or the types of the properties, prevent them from being compared by this Comparator.


? Copyright 2005-2006, Stripes Development Team.