com.taursys.model
Class VOComparator

java.lang.Object
  extended bycom.taursys.model.VOComparator
All Implemented Interfaces:
java.util.Comparator

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

VOComparator is a Comparator for sorting value objects in a VOListValueHolder. This can sort by one or more properties of the value object. Ascending or descending can be specified for each property (default is ascending).

To sort by a single property, use the setPropertyName method. To sort by more than one property use the setPropertyNames method and pass it a String[] array of property names.

To change the order for a single property, use the setAscendingOrder method (true=Ascending false=descending). To change the order for multiple properties, use the setAscendingOrders method and pass it a boolean[] array. The order of the ascending order indicators should match the array of property names.

This VOComparator is then attached to the VOListValueHolder using the setComparator method. IMPORTANT: This VOComparator can only be used with one VOListValueHolder at a time - it cannot be shared.

Example - sort ascending by a lastName

    VOListValueHolder holder = new VOListValueHolder();
    VOComparator comparator = new VOComparator();
    ...
    comparator.setPropertyName("lastName");
    holder.setComparator(comparator);
 

Example - sort ascending by a salary(descending), yearsWorked(ascending)

    VOListValueHolder holder = new VOListValueHolder();
    VOComparator comparator = new VOComparator();
    ...
    comparator.setPropertyNames(new String[] {"salary", "yearsWorked"});
    comparator.setAscendingOrders(new boolean[] {false, true});
    holder.setComparator(comparator);
 

Version:
1.0
Author:
Marty Phelan

Constructor Summary
VOComparator()
          Constructs a new VOComparator
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
          Compare the two given value objects based on the propertyName(s) and ascendingOrder(s).
 boolean[] getAscendingOrders()
          Get the ascending order flags for each of the properties.
 java.lang.String getPropertyName()
          Get the first or only propertyName used for the comparison.
 java.lang.String[] getPropertyNames()
          Get the propertyNames (in order) used for the comparison.
 VOValueHolder getVOValueHolder()
          Get the VOValueHolder that this comparator is linked to.
 boolean isAscendingOrder()
          Get the ascending order flag for the first or only propertyName.
 void setAscendingOrder(boolean ascendingOrder)
          Set the ascending order flag for the first or only propertyName.
 void setAscendingOrders(boolean[] ascendingOrders)
          Set the ascending order flags for each of the properties.
 void setPropertyName(java.lang.String propertyName)
          Set the first or only propertyName used for the comparison.
 void setPropertyNames(java.lang.String[] propertyNames)
          Set the propertyNames (in order) used for the comparison.
 void setVOValueHolder(VOValueHolder holder)
          Set the VOValueHolder that this comparator is linked to.
 
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

VOComparator

public VOComparator()
Constructs a new VOComparator

Method Detail

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Compare the two given value objects based on the propertyName(s) and ascendingOrder(s). Return 0 if they are equal, negative value if o1 should appear before o2 in the list, or positive if o1 should appear after o2 in the list. If both o1 and o2 are null, they are considered equal. If one is null and the other not null, the null value will appear before the non-null value in the list. The same null rule holds true for the property values.

Specified by:
compare in interface java.util.Comparator
Parameters:
o1 - the object to base the comparison on
o2 - the object to compare to
Returns:
0 if o1 and o2 could appear in same position, negative if o1 should appear before o2, and positive if o1 should appear after o2.

setVOValueHolder

public void setVOValueHolder(VOValueHolder holder)
Set the VOValueHolder that this comparator is linked to. This holder provides accessors to the properties of the value object.

Parameters:
holder - the VOValueHolder that this comparator is linked to.

getVOValueHolder

public VOValueHolder getVOValueHolder()
Get the VOValueHolder that this comparator is linked to. This holder provides accessors to the properties of the value object.

Returns:
the VOValueHolder that this comparator is linked to.

setPropertyName

public void setPropertyName(java.lang.String propertyName)
Set the first or only propertyName used for the comparison.

Parameters:
propertyName - the first or only propertyName used for the comparison.

getPropertyName

public java.lang.String getPropertyName()
Get the first or only propertyName used for the comparison.

Returns:
the first or only propertyName used for the comparison.

setAscendingOrder

public void setAscendingOrder(boolean ascendingOrder)
Set the ascending order flag for the first or only propertyName. The default is true.

Parameters:
ascendingOrder - the ascending order flag for the first or only propertyName.

isAscendingOrder

public boolean isAscendingOrder()
Get the ascending order flag for the first or only propertyName. The default is true.

Returns:
the ascending order flag for the first or only propertyName.

setPropertyNames

public void setPropertyNames(java.lang.String[] propertyNames)
Set the propertyNames (in order) used for the comparison.

Parameters:
propertyNames - the propertyNames (in order) used for the comparison.

getPropertyNames

public java.lang.String[] getPropertyNames()
Get the propertyNames (in order) used for the comparison.

Returns:
the propertyNames (in order) used for the comparison.

setAscendingOrders

public void setAscendingOrders(boolean[] ascendingOrders)
Set the ascending order flags for each of the properties. The default is true(ascending) if ommitted.

Parameters:
ascendingOrders - the ascending order flags for each of the properties.

getAscendingOrders

public boolean[] getAscendingOrders()
Get the ascending order flags for each of the properties. The default is true(ascending) if ommitted.

Returns:
the ascending order flags for each of the properties.


Copyright © 2007 Martin T Phelan. All Rights Reserved.