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