org.shept.beans.support
Class PropertyComparator

java.lang.Object
  extended by org.shept.beans.support.PropertyComparator
All Implemented Interfaces:
Comparator

public class PropertyComparator
extends Object
implements Comparator

PropertyComparator performs a comparison of two beans, evaluating the specified bean property via a BeanWrapper. This is a modified version of the original PropertyComparator as deliverd by Spring. For some reason the original Comparator won't sort if the models objects in the list are Array objects. This is the case if Hibernate returns Tuples (i.e. Arrays where each index represents a model object). It would be nice if the BeanWrapperImpl would properly handle these cases but this seems to be a limit of the java bean spec. So I modified the getPropertyValue(Object) method to support Tuples also.

Since:
19.05.2003
Author:
Juergen Hoeller, Jean-Pierre Pawlak
See Also:
BeanWrapper

Nested Class Summary
private static class PropertyComparator.WrappedProperty
           
 
Field Summary
private  org.springframework.beans.BeanWrapperImpl beanWrapper
           
protected  org.apache.commons.logging.Log logger
           
private  org.springframework.beans.support.SortDefinition sortDefinition
           
 
Constructor Summary
PropertyComparator(org.springframework.beans.support.SortDefinition sortDefinition)
          Create a new PropertyComparator for the given SortDefinition.
PropertyComparator(String property, boolean ignoreCase, boolean ascending)
          Create a PropertyComparator for the given settings.
 
Method Summary
 int compare(Object o1, Object o2)
           
private  Object getPropertyValue(Object obj)
          Get the SortDefinition's property value for the given object.
 org.springframework.beans.support.SortDefinition getSortDefinition()
          Return the SortDefinition that this comparator uses.
static void sort(List source, org.springframework.beans.support.SortDefinition sortDefinition)
          Sort the given List according to the given sort definition.
static void sort(Object[] source, org.springframework.beans.support.SortDefinition sortDefinition)
          Sort the given source according to the given sort definition.
 
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

logger

protected final org.apache.commons.logging.Log logger

sortDefinition

private final org.springframework.beans.support.SortDefinition sortDefinition

beanWrapper

private final org.springframework.beans.BeanWrapperImpl beanWrapper
Constructor Detail

PropertyComparator

public PropertyComparator(org.springframework.beans.support.SortDefinition sortDefinition)
Create a new PropertyComparator for the given SortDefinition.

See Also:
MutableSortDefinition

PropertyComparator

public PropertyComparator(String property,
                          boolean ignoreCase,
                          boolean ascending)
Create a PropertyComparator for the given settings.

Parameters:
property - the property to compare
ignoreCase - whether upper and lower case in String values should be ignored
ascending - whether to sort ascending (true) or descending (false)
Method Detail

getSortDefinition

public final org.springframework.beans.support.SortDefinition getSortDefinition()
Return the SortDefinition that this comparator uses.


compare

public int compare(Object o1,
                   Object o2)
Specified by:
compare in interface Comparator

getPropertyValue

private Object getPropertyValue(Object obj)
Get the SortDefinition's property value for the given object.

Parameters:
obj - the object to get the property value for
Returns:
the property value

sort

public static void sort(List source,
                        org.springframework.beans.support.SortDefinition sortDefinition)
                 throws org.springframework.beans.BeansException
Sort the given List according to the given sort definition.

Note: Contained objects have to provide the given property in the form of a bean property, i.e. a getXXX method.

Parameters:
source - the input List
sortDefinition - the parameters to sort by
Throws:
IllegalArgumentException - in case of a missing propertyName
org.springframework.beans.BeansException

sort

public static void sort(Object[] source,
                        org.springframework.beans.support.SortDefinition sortDefinition)
                 throws org.springframework.beans.BeansException
Sort the given source according to the given sort definition.

Note: Contained objects have to provide the given property in the form of a bean property, i.e. a getXXX method.

Parameters:
source - input source
sortDefinition - the parameters to sort by
Throws:
IllegalArgumentException - in case of a missing propertyName
org.springframework.beans.BeansException


Copyright © 2011. All Rights Reserved.