import java.util.Comparator;
public class ByWeightComparator implements Comparator
{
/**
* Compare two Trucks by weight. Callback for sort or TreeMap.
* effectively returns a-b; orders by ascending weight
*
* @param pFirst first object a to be compared
*
* @param pSecond second object b to be compared
*
* @return +1 if a>b, 0 if a=b, -1 if a<b
*/
public final int compare ( Object pFirst, Object pSecond )
{
long aFirstWeight = ( (Truck)pFirst ).weight;
long aSecondWeight = ( (Truck)pSecond ).weight;
return signum( aFirstWeight - aSecondWeight );
}
/**
* Collapse number down to +1 0 or -1 depending on sign.
* Typically used in compare routines to collapse a difference
* of two longs to an int.
*
* @param diff usually represents the difference of two long.
*
* @return signum of diff, +1, 0 or -1.
*/
public static final int signum ( long diff )
{
if ( diff > 0 ) return 1;
if ( diff < 0 ) return -1;
else return 0;
}
}