Copyright 2002 by aragost

com.aragost.util
Class BeanComparator

java.lang.Object
  |
  +--com.aragost.util.BeanComparator
All Implemented Interfaces:
java.util.Comparator

public class BeanComparator
extends java.lang.Object
implements java.util.Comparator

A class to compare beans on a list of specified properties. The compare is done one property at a time until a difference is found, or all properties are equal. It is useful for sorting a collection of beans, in particular if you not always want to sort on the same properties. There is support for implementing sorting similiar to e.g. Windows Explorer where the user can click on a column header to set the sorting.


Constructor Summary
BeanComparator()
          Create a new BeanComparator
BeanComparator(java.lang.String configurationString)
          Create a new BeanComparator.
 
Method Summary
 void addFirst(java.lang.String propertyName, boolean descending)
          Add a new property to be used first in the comparison.
 void addLast(java.lang.String propertyName, boolean descending)
          Add a new property to be used last in the comparison.
 void addOrToggle(java.lang.String propertyName)
          Used to implement sorting similar to Windows Explorer.
 int compare(java.lang.Object a, java.lang.Object b)
          Compares its two arguments for order.
static int compareObjects(java.lang.Object x, java.lang.Object y)
           
static int compareObjects(java.lang.Object x, java.lang.Object y, boolean ignoreCase)
          A utility method to compare two objects.
 boolean isIgnoreCase()
          Getter for property ignoreCase.
 void remove(java.lang.String name)
          Remove a property to use in the comparison
 void setIgnoreCase(boolean ignoreCase)
          Setter for property ignoreCase.
 
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()
Create a new BeanComparator


BeanComparator

public BeanComparator(java.lang.String configurationString)
Create a new BeanComparator. Configure it from the specified configuration String. The configuration string contains a comma seperated list of property names. The compare order can be set by a < or > before the name. A < means that the beans compare the same way as the property, > means that the order is reversed. E.g. "name,number" will compare the first on property name, and if the two values of name is equal the property number will be compared.

Parameters:
configurationString - The configuration String
Method Detail

compare

public int compare(java.lang.Object a,
                   java.lang.Object b)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. The comparison is done by comparing one property at a time, until a difference is found.The value of two properties is compared with the Comparable.compareTo() method. If the value of the property is not a Comparable it is converted to a String (which is a Comrable) with toString(). In comparisons null is considered less than anything else (and equal to null). The value of a property is read with Beans.getProperty(). If all properties compare equal then 0 is returned. A special case is when the BeanComparator has no fields added, then the comparison is forwarded to the static method compareObjects.

Specified by:
compare in interface java.util.Comparator
Parameters:
a - The first bean to compare
b - The second bean to compare
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second

addOrToggle

public void addOrToggle(java.lang.String propertyName)
Used to implement sorting similar to Windows Explorer. The specified property is added as the first to compare on. If the property name, match the property name of the first field, the compare order is toggled.

Parameters:
propertyName - The property name

addFirst

public void addFirst(java.lang.String propertyName,
                     boolean descending)
Add a new property to be used first in the comparison.

Parameters:
propertyName - The name of the property to sort on
descending - Indicate if the compare order on the specified property should be reversed. When used in a sorting the sorting will be descending instead of ascending.

addLast

public void addLast(java.lang.String propertyName,
                    boolean descending)
Add a new property to be used last in the comparison.

Parameters:
propertyName - The name of the property
descending - Indicate if the compare order on the specified property should be reversed. When used in a sorting the sorting will be descending instead of ascending.

compareObjects

public static int compareObjects(java.lang.Object x,
                                 java.lang.Object y)

remove

public void remove(java.lang.String name)
Remove a property to use in the comparison

Parameters:
name - The name of the property no longer to use when comparing

compareObjects

public static int compareObjects(java.lang.Object x,
                                 java.lang.Object y,
                                 boolean ignoreCase)
A utility method to compare two objects. If the arguments isn't implementing the Comparable interface then they are converted to it with the toString() method. The comparison is now done with the compareTo() method. If this method throws an exception then the class name of the two arguments are compare. This shouldn't thrown an exception since compareTo should always work for objects of the same class.

Parameters:
x -
y -
ignoreCase -
Returns:
int

isIgnoreCase

public boolean isIgnoreCase()
Getter for property ignoreCase.

Returns:
Value of property ignoreCase.

setIgnoreCase

public void setIgnoreCase(boolean ignoreCase)
Setter for property ignoreCase.

Parameters:
ignoreCase - New value of property ignoreCase.

Copyright 2002 by aragost