FileDocCategorySizeDatePackage
ReflexiveComparator.javaAPI DocExample6565Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.reflection

ReflexiveComparator

public class ReflexiveComparator extends Object implements Comparator
Creates a comparator that sorts the target objects based upon the value of a specific property. Note: this comparator imposes orderings that are inconsistent with equals. This is because the comparator is set up reflexively and may not match the comparisons in the equals method.
author
$author$
version
$Revision: 1.9 $

Fields Summary
private static final String
ERR_COMPARABLE
Holds error message for non comparable sortProperty.
private Class
dataType
Holds value of property dataType.
private PropertyDescriptor
sortProperty
Holds value of property sortProperty.
private boolean
ascending
Holds value of property ascending.
Constructors Summary
public ReflexiveComparator(Class dataType, PropertyDescriptor sortProperty, boolean ascending)
Creates a new instance of ReflexiveComparator.

param
dataType Class which this comparator will be sorting.
param
sortProperty The property descriptor of the class to use to sort.
param
ascending Whether to sort in ascending order (default) or not. Pass false to sort in descending order.
throws
IllegalArgumentException DOCUMENT ME!


	                                                         	 
	   
	                             
	                              
		this.dataType = dataType;
		this.sortProperty = sortProperty;
		this.ascending = ascending;

		if (!(sortProperty.getPropertyType().isPrimitive())) {
			if (!(Comparable.class.isAssignableFrom(this.sortProperty.getPropertyType()))) {
				throw new IllegalArgumentException(ERR_COMPARABLE);
			}
		}
	
Methods Summary
public intcompare(java.lang.Object x, java.lang.Object y)
Compares its two arguments for order. Sorts according to the following design. Note that if the ascending property is false, the values for +1 or -1 will be reversed.
  • Giving null for the first argument will always give a -1 reply.
  • Giving null for the second argument will always give a +1 reply.
  • Giving null for the both arguments will always give a 0 reply.
  • Objects whose sortProperty contains null will give a -1 reply relative to objects with a non-null sortProperty.
  • If both objects' sort property are not null, The values of the sort properties will be compared.

param
x Object to compare.
param
y Second object to compare.
return
A value of +1, -1 or 0 depending on the compare.

		if (ascending) {
			return compareHelper(x, y);
		} else {
			return (0 - compareHelper(x, y));
		}
	
private intcompareHelper(java.lang.Object x, java.lang.Object y)
Perform the raw compare.

param
x Object to compare.
param
y Second object to compare.
return
The result of the comparison
throws
RuntimeException If there is an exception during ocmparison.
see
Method {@link mirror.utilties.ReflexiveComparator.compare(Object, Object)}

		try {
			if ((x != null) && (y == null)) {
				return -1;
			} else if ((x == null) && (y != null)) {
				return -1;
			} else if ((x == null) && (y == null)) {
				return 0;
			}

			Object xVal = this.sortProperty.getReadMethod()
				                           .invoke(x, null);
			Object yVal = this.sortProperty.getReadMethod()
				                           .invoke(y, null);

			if ((xVal != null) && (yVal == null)) {
				return -1;
			} else if ((xVal == null) && (yVal != null)) {
				return -1;
			} else if ((xVal == null) && (yVal == null)) {
				return 0;
			}

			return ((Comparable)xVal).compareTo(yVal);
		} catch (final InvocationTargetException ex) {
			throw new RuntimeException(ex);
		} catch (final IllegalAccessException ex) {
			throw new RuntimeException(ex);
		}
	
public java.lang.ClassgetDataType()
Getter for property tgtClass.

return
Value of property tgtClass.

		return this.dataType;
	
public java.beans.PropertyDescriptorgetSortProperty()
Getter for property sortProperty.

return
Value of property sortProperty.

		return this.sortProperty;
	
public booleanisAscending()
Getter for property ascending.

return
Value of property ascending.

		return this.ascending;
	
public voidsetAscending(boolean ascending)
Setter for property ascending.

param
ascending New value of property ascending.

		this.ascending = ascending;
	
public voidsetDataType(java.lang.Class dataType)
Setter for property tgtClass.

param
dataType New value of property tgtClass.

		this.dataType = dataType;
	
public voidsetSortProperty(java.beans.PropertyDescriptor sortProperty)
Setter for property sortProperty.

param
sortProperty New value of property sortProperty.

		this.sortProperty = sortProperty;