1 /*
2 * Copyright 2008 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * The comparator can dynamically implement java.util.Comparator and facilitate to sort a given colletion. This implementation is
29 * based on an article by York Davis, which was published in Java Developer's Journal (http://java.sys-con.com/read/45837.htm).
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 * enumerate the valid values of sort order
38 */
39 public enum SortOrder {
40 ASC, DESC
41 }
42
43 /**
44 * private constructs a DynamicCollectionComparator.java.
45 *
46 * @param list the given collection that needs to be sorted
47 * @param fieldName the field name ordered by
48 * @param sortOrder the given sort order, either ascending or descending
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 * sort the given collection ordered by the given field name. Ascending order is used.
64 *
65 * @param list the given collection that needs to be sorted
66 * @param fieldName the field name ordered by
67 */
68 public static <C> void sort(List<C> list, String... fieldNames) {
69 sort(list, SortOrder.ASC, fieldNames);
70 }
71
72 /**
73 * sort the given collection ordered by the given field name
74 *
75 * @param list the given collection that needs to be sorted
76 * @param fieldName the field name ordered by
77 * @param sortOrder the given sort order, either ascending or descending
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 * compare the two given objects for order. Returns a negative integer, zero, or a positive integer as this object is less than,
86 * equal to, or greater than the specified object. If the objects implement Comparable interface, the objects compare with each
87 * other based on the implementation; otherwise, the objects will be converted into Strings and compared as String.
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 * compare the two given objects for order. Returns a negative integer, zero, or a positive integer as this object is less than,
105 * equal to, or greater than the specified object. If the objects implement Comparable interface, the objects compare with each
106 * other based on the implementation; otherwise, the objects will be converted into Strings and compared as String.
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 * convert the sort order as an interger. If the sort order is "DESC" (descending order), reutrn -1; otherwise, return 1.
152 *
153 * @return -1 if the sort order is "DESC" (descending order); otherwise, return 1.
154 */
155 public int getSortOrderAsNumber() {
156 return sortOrder.equals(SortOrder.ASC) ? 1 : -1;
157 }
158 }