texsoft.java.object
Class XsObjectComparator

java.lang.Object
  extended bytexsoft.java.object.XsObjectComparator
All Implemented Interfaces:
java.util.Comparator

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

Implements a comparator that compares XsObjectWithReflection objects.

For the comparator to work the caller MUST set an array of properties that define the properties used to order the object and their sequence for comparison. For example if one need to order by properties name, age the array will be { "name", "age" }.

If case insensitive sorting is needed for String property, the the NOCASE constant has to be appended to the property name (ex. { "name" + NOCASE, "age" }).

A property can be ordered descending by appending the constant DESCENDING to the property name (ex. { "name", "age" + DESCENDING }).

There is also a DESCENDING_NOCASE constant to perform a descending insensitive case sorting.


Field Summary
static java.lang.String ASCENDING
          Append it to property name to have ascending sorting on the property.
static java.lang.String ASCENDING_NOCASE
          Append it to property name to have ascending ignorecase sorting on the property.
static java.lang.String DESCENDING
          Append it to property name to have descending sorting on the property.
static java.lang.String DESCENDING_NOCASE
          Append it to property name to have descending ignorecase sorting on the property.
static java.lang.String NOCASE
          Append it to property name to have ascending ignorecase sorting on the property.
 
Constructor Summary
XsObjectComparator()
          Creates a new XsObjectComparator.
XsObjectComparator(java.lang.String[] order)
          Create a new XsObjectComparator with the given properties' comparison order.
 
Method Summary
 int compare(java.lang.Object a, java.lang.Object b)
          Compare two objects.
 java.lang.String[] getOrder()
          Returns the properties used for the comparison.
 void setOrder(java.lang.String[] order)
          Sets the properties involved into the comparison.
 
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
 

Field Detail

ASCENDING

public static final java.lang.String ASCENDING
Append it to property name to have ascending sorting on the property.

See Also:
Constant Field Values

ASCENDING_NOCASE

public static final java.lang.String ASCENDING_NOCASE
Append it to property name to have ascending ignorecase sorting on the property.

See Also:
Constant Field Values

NOCASE

public static final java.lang.String NOCASE
Append it to property name to have ascending ignorecase sorting on the property.

See Also:
Constant Field Values

DESCENDING

public static final java.lang.String DESCENDING
Append it to property name to have descending sorting on the property.

See Also:
Constant Field Values

DESCENDING_NOCASE

public static final java.lang.String DESCENDING_NOCASE
Append it to property name to have descending ignorecase sorting on the property.

See Also:
Constant Field Values
Constructor Detail

XsObjectComparator

public XsObjectComparator()
Creates a new XsObjectComparator.


XsObjectComparator

public XsObjectComparator(java.lang.String[] order)
Create a new XsObjectComparator with the given properties' comparison order.

Parameters:
order - array of String with the names of the properties used to compare the object; the comparison type constants can be appended to property names to select comparison options
Method Detail

getOrder

public java.lang.String[] getOrder()
Returns the properties used for the comparison.

Returns:
array of String with the names of the properties used to compare the object; the comparison type constants can be appended to property names to select comparison options

setOrder

public void setOrder(java.lang.String[] order)
Sets the properties involved into the comparison.

Parameters:
order - array of String with the names of the properties used to compare the object; the comparison type constants can be appended to property names to select comparison options

compare

public int compare(java.lang.Object a,
                   java.lang.Object b)
Compare two objects.

This implementations require that both objects derive from XsObjectWithReflection and belong to the same class.

Specified by:
compare in interface java.util.Comparator
Parameters:
a - first object to compare
b - second object 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.
Throws:
java.lang.ClassCastException - thrown if the two object being compared do not belong to the same class or if they do not extend XsObjectWithReflection; this exception is thrown also if during objects comparison a reflection error occurs.