##$Id: comparator.java.vm,v 1.5 2007/07/03 12:59:57 kameleono Exp $
#parse( "table.include.vm" )
#parse( "header.include.vm" )
$codewriter.setCurrentJavaFilename("$table.getPackage()", "${comparatorClass}.java")
package $table.getPackage();
import java.util.Comparator;
##import $pkg.*;
/**
* Comparator class is used to sort the $beanClass objects.
* @author sql2java
*/
public class $comparatorClass implements Comparator
{
/**
* Holds the field on which the comparison is performed.
*/
private int iType;
/**
* Value that will contain the information about the order of the sort: normal or reversal.
*/
private boolean bReverse;
/**
* Constructor class for $comparatorClass.
*
* Example:
*
* Arrays.sort(pArray, new $comparatorClass(${managerClass}.ID_$table.getFirstColumn().getConstName(), bReverse));
*
* @param iType the field from which you want to sort
*
* Possible values are:
*
#foreach( $column in $columns )
#if ( $column.hasCompareTo() )
* - ${managerClass}.ID_$column.getConstName()
#end
#end
*
*/
public $comparatorClass(int iType)
{
this(iType, false);
}
/**
* Constructor class for ${comparatorClass}.
*
* Example:
*
* Arrays.sort(pArray, new $comparatorClass(${managerClass}.ID_$table.getFirstColumn().getConstName(), bReverse));
*
* @param iType the field from which you want to sort.
*
* Possible values are:
*
#foreach( $column in $columns )
* - ${managerClass}.ID_$column.getConstName()
#end
*
*
* @param bReverse set this value to true, if you want to reverse the sorting results
*/
public $comparatorClass(int iType, boolean bReverse)
{
this.iType = iType;
this.bReverse = bReverse;
}
/**
* Implementation of the compare method.
*/
public int compare(Object pObj1, Object pObj2)
{
$beanClass b1 = ($beanClass)pObj1;
$beanClass b2 = ($beanClass)pObj2;
int iReturn = 0;
switch(iType)
{
## I need to add a try catch here??????
#set ( $nbColumns = 0 )
#foreach( $column in $columns )
#set ( $getMethod = $column.getGetMethod() )
#if ( $column.hasCompareTo() )
case ${managerClass}.ID_$column.getConstName():
if (b1.$getMethod() == null && b2.$getMethod() != null) {
iReturn = -1;
} else if (b1.$getMethod() == null && b2.$getMethod() == null) {
iReturn = 0;
} else if (b1.$getMethod() != null && b2.$getMethod() == null) {
iReturn = 1;
} else {
iReturn = b1.$getMethod().compareTo(b2.$getMethod());
}
break;
#set ( $nbColumns = $nbColumns + 1 )
#end
#end
default:
#if ( $nbColumns != 0 )
throw new IllegalArgumentException("Type passed for the field is not supported");
#end
}
return bReverse ? (-1 * iReturn) : iReturn;
}}