| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.util.comparator; |
| 17 | |
|
| 18 | |
import java.beans.PropertyDescriptor; |
| 19 | |
import java.io.Serializable; |
| 20 | |
import java.lang.reflect.InvocationTargetException; |
| 21 | |
import java.util.Collections; |
| 22 | |
import java.util.Comparator; |
| 23 | |
import java.util.Iterator; |
| 24 | |
import java.util.List; |
| 25 | |
|
| 26 | |
import org.apache.commons.beanutils.PropertyUtils; |
| 27 | |
import org.apache.commons.collections.comparators.ComparableComparator; |
| 28 | |
import org.kuali.rice.kns.exception.BeanComparisonException; |
| 29 | |
import org.kuali.rice.kns.util.TypeUtils; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public class BeanPropertyComparator implements Comparator, Serializable { |
| 37 | |
private static final long serialVersionUID = -2675700473766186018L; |
| 38 | |
boolean ignoreCase; |
| 39 | |
private List propertyNames; |
| 40 | |
private Comparator stringComparator; |
| 41 | |
private Comparator booleanComparator; |
| 42 | |
private Comparator genericComparator; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public BeanPropertyComparator(List propertyNames) { |
| 52 | 0 | this(propertyNames, true); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 0 | public BeanPropertyComparator(List propertyNames, boolean ignoreCase) { |
| 63 | 0 | if (propertyNames == null) { |
| 64 | 0 | throw new IllegalArgumentException("invalid (null) propertyNames list"); |
| 65 | |
} |
| 66 | 0 | if (propertyNames.size() == 0) { |
| 67 | 0 | throw new IllegalArgumentException("invalid (empty) propertyNames list"); |
| 68 | |
} |
| 69 | 0 | this.propertyNames = Collections.unmodifiableList(propertyNames); |
| 70 | 0 | this.ignoreCase = ignoreCase; |
| 71 | |
|
| 72 | 0 | if (ignoreCase) { |
| 73 | 0 | this.stringComparator = String.CASE_INSENSITIVE_ORDER; |
| 74 | |
} |
| 75 | |
else { |
| 76 | 0 | this.stringComparator = ComparableComparator.getInstance(); |
| 77 | |
} |
| 78 | 0 | this.booleanComparator = new Comparator() { |
| 79 | |
public int compare(Object o1, Object o2) { |
| 80 | 0 | int compared = 0; |
| 81 | |
|
| 82 | 0 | Boolean b1 = (Boolean) o1; |
| 83 | 0 | Boolean b2 = (Boolean) o2; |
| 84 | |
|
| 85 | 0 | if (!b1.equals(b2)) { |
| 86 | 0 | if (b1.equals(Boolean.FALSE)) { |
| 87 | 0 | compared = -1; |
| 88 | |
} |
| 89 | |
else { |
| 90 | 0 | compared = 1; |
| 91 | |
} |
| 92 | |
} |
| 93 | |
|
| 94 | 0 | return compared; |
| 95 | |
} |
| 96 | |
|
| 97 | |
}; |
| 98 | 0 | this.genericComparator = ComparableComparator.getInstance(); |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public int compare(Object o1, Object o2) { |
| 110 | 0 | int compared = 0; |
| 111 | |
|
| 112 | |
try { |
| 113 | 0 | for (Iterator i = propertyNames.iterator(); (compared == 0) && i.hasNext();) { |
| 114 | 0 | String currentProperty = i.next().toString(); |
| 115 | |
|
| 116 | |
|
| 117 | 0 | Comparator currentComparator = null; |
| 118 | |
try { |
| 119 | 0 | PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(o1, currentProperty); |
| 120 | 0 | Class propertyClass = propertyDescriptor.getPropertyType(); |
| 121 | 0 | if (propertyClass.equals(String.class)) { |
| 122 | 0 | currentComparator = this.stringComparator; |
| 123 | |
} |
| 124 | 0 | else if (TypeUtils.isBooleanClass(propertyClass)) { |
| 125 | 0 | currentComparator = this.booleanComparator; |
| 126 | |
} |
| 127 | |
else { |
| 128 | 0 | currentComparator = this.genericComparator; |
| 129 | |
} |
| 130 | |
} |
| 131 | 0 | catch (NullPointerException e) { |
| 132 | 0 | throw new BeanComparisonException("unable to find property '" + o1.getClass().getName() + "." + currentProperty + "'", e); |
| 133 | 0 | } |
| 134 | |
|
| 135 | |
|
| 136 | 0 | Object value1 = PropertyUtils.getProperty(o1, currentProperty); |
| 137 | 0 | Object value2 = PropertyUtils.getProperty(o2, currentProperty); |
| 138 | |
|
| 139 | 0 | compared = currentComparator.compare(value1, value2); |
| 140 | 0 | } |
| 141 | |
} |
| 142 | 0 | catch (IllegalAccessException e) { |
| 143 | 0 | throw new BeanComparisonException("unable to compare property values", e); |
| 144 | |
} |
| 145 | 0 | catch (NoSuchMethodException e) { |
| 146 | 0 | throw new BeanComparisonException("unable to compare property values", e); |
| 147 | |
} |
| 148 | 0 | catch (InvocationTargetException e) { |
| 149 | 0 | throw new BeanComparisonException("unable to compare property values", e); |
| 150 | 0 | } |
| 151 | |
|
| 152 | 0 | return compared; |
| 153 | |
} |
| 154 | |
} |