-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBeanComparator.java
More file actions
171 lines (144 loc) · 3.78 KB
/
BeanComparator.java
File metadata and controls
171 lines (144 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.lang.reflect.*;
import java.util.*;
/**
* A comparator to sort on the specified field of a given class.
*
* Reflection is used to retrieve the data to be sorted, therefore
* you must provide the Class and the method name within the class
* that will be used to retrieve the data.
*
* Several sort properties can be set:
*
* a) ascending (default true)
* b) ignore case (default true)
* c) nulls last (default true)
*/
public class BeanComparator implements Comparator
{
private static final Class[] EMPTY_CLASS_ARRAY = new Class[] {};
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[] {};
private Method method;
private boolean isAscending;
private boolean isIgnoreCase;
private boolean isNullsLast = true;
/*
* Sort using default sort properties
*/
BeanComparator(Class<?> beanClass, String methodName)
{
this(beanClass, methodName, true);
}
/*
* Sort in the specified order with the remaining default properties
*/
BeanComparator(Class<?> beanClass, String methodName, boolean isAscending)
{
this(beanClass, methodName, isAscending, true);
}
/*
* Sort in the specified order and case sensitivity with the
* remaining default properties
*/
BeanComparator(Class<?> beanClass, String methodName, boolean isAscending, boolean isIgnoreCase)
{
setAscending( isAscending );
setIgnoreCase( isIgnoreCase );
// Make sure the method exists in the given bean class
try
{
method = beanClass.getMethod(methodName, EMPTY_CLASS_ARRAY);
}
catch(NoSuchMethodException nsme)
{
String message = methodName + "() method does not exist";
throw new IllegalArgumentException( message );
}
// Make sure the method returns a value
Class returnClass = method.getReturnType();
if (returnClass.getName().equals("void"))
{
String message = methodName + " has a void return type";
throw new IllegalArgumentException( message );
}
}
/*
* Set the sort order
*/
public void setAscending(boolean isAscending)
{
this.isAscending = isAscending;
}
/*
* Set whether case should be ignored when sorting Strings
*/
public void setIgnoreCase(boolean isIgnoreCase)
{
this.isIgnoreCase = isIgnoreCase;
}
/*
* Set nulls position in the sort order
*/
public void setNullsLast(boolean isNullsLast)
{
this.isNullsLast = isNullsLast;
}
/*
* Implement the Comparable interface
*/
@SuppressWarnings("unchecked")
public int compare(Object object1, Object object2)
{
Object field1 = null;
Object field2 = null;
try
{
field1 = method.invoke(object1, EMPTY_OBJECT_ARRAY);
field2 = method.invoke(object2, EMPTY_OBJECT_ARRAY);
}
catch (Exception e)
{
throw new RuntimeException( e );
}
// Treat empty strings like nulls
if (field1 instanceof String && ((String)field1).length() == 0)
{
field1 = null;
}
if (field2 instanceof String && ((String)field2).length() == 0)
{
field2 = null;
}
// Handle sorting of null values
if (field1 == null && field2 == null) return 0;
if (field1 == null) return isNullsLast ? 1 : -1;
if (field2 == null) return isNullsLast ? -1 : 1;
// Compare objects
Object c1;
Object c2;
if (isAscending)
{
c1 = field1;
c2 = field2;
}
else
{
c1 = field2;
c2 = field1;
}
if (c1 instanceof Comparable)
{
if (c1 instanceof String
&& isIgnoreCase)
return ((String)c1).compareToIgnoreCase((String)c2);
else
return ((Comparable)c1).compareTo(c2);
}
else // Compare as a String
{
if (isIgnoreCase)
return c1.toString().compareToIgnoreCase(c2.toString());
else
return c1.toString().compareTo(c2.toString());
}
}
}