1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys;
17
18 import java.io.Serializable;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23
24 import org.apache.commons.beanutils.PropertyUtils;
25 import org.apache.commons.lang.ObjectUtils;
26
27
28
29
30
31 public class DynamicCollectionComparator<T> implements Comparator<T>, Serializable {
32 private List<T> list;
33 private String[] fieldNames;
34 private SortOrder sortOrder;
35
36
37
38
39 public enum SortOrder {
40 ASC, DESC
41 }
42
43
44
45
46
47
48
49
50 private DynamicCollectionComparator(List<T> list, SortOrder sortOrder, String... fieldNames) {
51 super();
52
53 if (fieldNames == null || fieldNames.length <= 0) {
54 throw new IllegalArgumentException("The input field names cannot be null or empty");
55 }
56
57 this.list = list;
58 this.fieldNames = fieldNames;
59 this.sortOrder = sortOrder;
60 }
61
62
63
64
65
66
67
68 public static <C> void sort(List<C> list, String... fieldNames) {
69 sort(list, SortOrder.ASC, fieldNames);
70 }
71
72
73
74
75
76
77
78
79 public static <C> void sort(List<C> list, SortOrder sortOrder, String... fieldNames) {
80 Comparator<C> comparator = new DynamicCollectionComparator<C>(list, sortOrder, fieldNames);
81 Collections.sort(list, comparator);
82 }
83
84
85
86
87
88
89 public int compare(T object0, T object1) {
90 int comparisonResult = 0;
91
92 for (String fieldName : fieldNames) {
93 comparisonResult = this.compare(object0, object1, fieldName);
94
95 if (comparisonResult != 0) {
96 break;
97 }
98 }
99
100 return comparisonResult;
101 }
102
103
104
105
106
107
108 public int compare(T object0, T object1, String fieldName) {
109 int comparisonResult = 0;
110
111 try {
112 Object propery0 = PropertyUtils.getProperty(object0, fieldName);
113 Object propery1 = PropertyUtils.getProperty(object1, fieldName);
114
115 if(propery0 == null && propery1 == null) {
116 comparisonResult = 0;
117 }
118 else if(propery0 == null) {
119 comparisonResult = -1;
120 }
121 else if(propery1 == null) {
122 comparisonResult = 1;
123 }
124 else if (propery0 instanceof Comparable) {
125 Comparable comparable0 = (Comparable) propery0;
126 Comparable comparable1 = (Comparable) propery1;
127
128 comparisonResult = comparable0.compareTo(comparable1);
129 }
130 else {
131 String property0AsString = ObjectUtils.toString(propery0);
132 String property1AsString = ObjectUtils.toString(propery1);
133
134 comparisonResult = property0AsString.compareTo(property1AsString);
135 }
136 }
137 catch (IllegalAccessException e) {
138 throw new RuntimeException("unable to compare property: " + fieldName, e);
139 }
140 catch (InvocationTargetException e) {
141 throw new RuntimeException("unable to compare property: " + fieldName, e);
142 }
143 catch (NoSuchMethodException e) {
144 throw new RuntimeException("unable to compare property: " + fieldName, e);
145 }
146
147 return comparisonResult * this.getSortOrderAsNumber();
148 }
149
150
151
152
153
154
155 public int getSortOrderAsNumber() {
156 return sortOrder.equals(SortOrder.ASC) ? 1 : -1;
157 }
158 }