Top Description Fields Constructors Methods
org.springframework.beans.support

public Class PropertyComparator<T>

extends Object
implements Comparator<T>
Class Inheritance
All Implemented Interfaces
java.util.Comparator
Type Parameters
<T>
the type of objects that may be compared by this comparator
Imports
java.util.Arrays, .Comparator, .List, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.BeanWrapperImpl, .BeansException, org.springframework.lang.Nullable, org.springframework.util.StringUtils

PropertyComparator performs a comparison of two beans, evaluating the specified bean property via a BeanWrapper.
Authors
Juergen Hoeller, Jean-Pierre Pawlak
Since
19.05.2003
See Also
org.springframework.beans.BeanWrapper

Field Summary

Modifier and TypeField and Description
protected final Log
private final SortDefinition

Constructor Summary

AccessConstructor and Description
public
PropertyComparator(SortDefinition sortDefinition)

Create a new PropertyComparator for the given SortDefinition.

public
PropertyComparator(String
the property to compare
property
,
boolean
whether upper and lower case in String values should be ignored
ignoreCase
,
boolean
whether to sort ascending (true) or descending (false)
ascending
)

Create a PropertyComparator for the given settings.

Method Summary

Modifier and TypeMethod and Description
public int
compare(T
the first object to be compared.
o1
,
T
the second object to be compared.
o2
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

private Object

Returns:

the property value
getPropertyValue
(Object
the object to get the property value for
obj
)

Get the SortDefinition's property value for the given object.

public final SortDefinition
getSortDefinition()

Return the SortDefinition that this comparator uses.

public static void
sort(List<?>
the input List
source
,
SortDefinition
the parameters to sort by
sortDefinition
)

Sort the given List according to the given sort definition.

public static void
sort(Object[]
input source
source
,
SortDefinition
the parameters to sort by
sortDefinition
)

Sort the given source according to the given sort definition.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
protected final Log logger
sortDefinitionback to summary
private final SortDefinition sortDefinition

Constructor Detail

PropertyComparatorback to summary
public PropertyComparator(SortDefinition sortDefinition)

Create a new PropertyComparator for the given SortDefinition.

See Also
MutableSortDefinition
PropertyComparatorback to summary
public PropertyComparator(String property, boolean ignoreCase, boolean ascending)

Create a PropertyComparator for the given settings.

Parameters
property:String

the property to compare

ignoreCase:boolean

whether upper and lower case in String values should be ignored

ascending:boolean

whether to sort ascending (true) or descending (false)

Method Detail

compareback to summary
public int compare(T o1, T o2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
o1:T

the first object to be compared.

o2:T

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
@SuppressWarnings:unchecked
getPropertyValueback to summary
private Object getPropertyValue(Object obj)

Get the SortDefinition's property value for the given object.

Parameters
obj:Object

the object to get the property value for

Returns:Object

the property value

Annotations
@Nullable
getSortDefinitionback to summary
public final SortDefinition getSortDefinition()

Return the SortDefinition that this comparator uses.

sortback to summary
public static void sort(List<?> source, SortDefinition sortDefinition) throws 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:List<?>

the input List

sortDefinition:SortDefinition

the parameters to sort by

Exceptions
IllegalArgumentException:
in case of a missing propertyName
sortback to summary
public static void sort(Object[] source, SortDefinition sortDefinition) throws 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:Object[]

input source

sortDefinition:SortDefinition

the parameters to sort by

Exceptions
IllegalArgumentException:
in case of a missing propertyName