gcomp

 By: Wickus Martin on November 11, 2003

GComp is a comparator for lazy Java programmers. Typically a comparator implementation would be created for every unique comparison (ordering, sorting) situation. Even a small application would quickly accumulate a sizable number of these comparator classes. GComp helps to prevent this situation by providing a single comparator implementation that can be applied to most of the usual comparison scenarios.  It does this by reading an expression that declares how the comparison should be carried out. The expression is supplied as the only parameter to the GComp constructor.


Class Diagram

Perhaps the best way to show  the simplicity of GComp is by way of example. Consider we need a comparator to sort a collection of orders (as specified in the above diagram) according to customer number.

All we need is one of the following:

new GComp("getOrderNo");    // method syntax
new GComp("orderNo");       // property syntax


Suppose we need a comparator that will sort the orders according to a customer's date of birth:

new GComp("customer.dateOfBirth");                           // ascending order
new GComp("customer.dateOfBirth@order=desc");                // descending order
new GComp("customer.dateOfBirth@order=desc,nulls=first");    // descending order, placing first those orders for which
                                                             // the customer's date of birth is unknown


A comparator that will sort the orders according to a customer's last name and then first name for those customers with the same last name:

new GComp("customer.lastName|customer.firstName");                          // sort according to last name then first name
new GComp("customer.lastName@order=asc|customer.firstName@order=desc");     // last name ascending, first name descending
new GComp("customer.lastName.toLowerCase|customer.firstName.toLowerCase");  // ignoring case

Summary:

GComp provides a simple to use comparator that should suffice for most of the usual ordering situations that Java programmers can expect to encounter.  Find it at www.sourgeforge.net/projects/gcomp.


SourceForge Logo