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