-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathGenericComparator.java
More file actions
277 lines (262 loc) · 9.79 KB
/
GenericComparator.java
File metadata and controls
277 lines (262 loc) · 9.79 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.edurekademo.utilities;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.Date;
/**
* Sorting - Generic Comparator
*
* @author Seshagiri Sriram
* @version 1.0
* @since Janauary 12, 2015
*
* This is an adaptation of a genenic comparator found on github linked from myjeeva.com
* Many thanks for the code!
*/
@SuppressWarnings("rawtypes")
public class GenericComparator implements Comparator, Serializable {
protected static final long serialVersionUID = -2293914106471884607L;
protected static final int LESSER = -1;
protected static final int EQUAL = 0;
protected static final int GREATER = 1;
protected static final String METHOD_GET_PREFIX = "get";
protected static final String DATATYPE_STRING = "java.lang.String";
protected static final String DATATYPE_DATE = "java.util.Date";
protected static final String DATATYPE_INTEGER = "java.lang.Integer";
protected static final String DATATYPE_LONG = "java.lang.Long";
protected static final String DATATYPE_FLOAT = "java.lang.Float";
protected static final String DATATYPE_DOUBLE = "java.lang.Double";
protected static final String DATATYPE_BOOLEAN = "java.lang.Boolean";
protected enum CompareMode { EQUAL, LESS_THAN, GREATER_THAN, DEFAULT }
// generic comparator attributes
protected String targetMethod;
protected boolean sortAscending;
/**
* <p>default constructor - assumes comparator for Type List</p>
*
* <p>For Example-</p>
* <code>List<Integer> aa = new ArrayList<Integer>();</code><br />
* <code>List<String> bb = new ArrayList<String>();</code><br />
* <code>List<Date> cc = new ArrayList<Date>();</code><br />
* <p>and so on..</p>
*
* @param sortAscending - a {@link boolean} - <code>true</code> ascending order or <code>false</code> descending order
*/
public GenericComparator(boolean sortAscending) {
super();
this.targetMethod = null;
this.sortAscending = sortAscending;
}
/**
* <p>constructor with <code>sortField</code> parameter for Derived type of <code>Class</code> default sorting is ascending order</p>
*
* <p>For Example-</p>
* <p><code>PersonVO person = new PersonVO();<br />
* person.setId(10001);<br />
* person.setName("Jacob");<br />
* person.setHeight(5.2F);<br />
* person.setEmailId("jacob@example.example");<br />
* person.setSalary(10500L);<br />
* person.setDob(new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("Jan 1, 1970"));<br /></code><br />
* <p>and person2, person3, so on.. And Defining & adding all the created objects in to below list</p>
* <p><code>List<PersonVO> persons = new ArrayList<PersonVO>();<br />
* persons.add(person1);<br />
* persons.add(person2);<br />
* persons.add(person3); </code>and so on<br />
*
* @param sortField - a {@link java.lang.String} - which field requires sorting; as per above example "sorting required for <code>name</code> field"
*/
public GenericComparator(String sortField) {
super();
this.targetMethod = prepareTargetMethod(sortField);
this.sortAscending = true;
}
/**
* <p>constructor with <code>sortField, sortAscending</code> parameter for Derived type of <code>Class</code></p>
*
* <p>For Example-</p>
* <p><code>PersonVO person = new PersonVO();<br />
* person.setId(10001);<br />
* person.setName("Jacob");<br />
* person.setHeight(5.2F);<br />
* person.setEmailId("jacob@example.example");<br />
* person.setSalary(10500L);<br />
* person.setDob(new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse("Jan 1, 1970"));<br /></code><br />
* <p>and person2, person3, so on.. And Defining & adding all the created objects in to below list</p>
* <p><code>List<PersonVO> persons = new ArrayList<PersonVO>();<br />
* persons.add(person1);<br />
* persons.add(person2);<br />
* persons.add(person3); </code>and so on <br />
* @param sortField - a {@link java.lang.String} - which field requires sorting; as per above example "sorting required for <code>name</code> field"
* @param sortAscending - a {@link boolean} - <code>true</code> ascending order or <code>false</code> descending order
*/
public GenericComparator(String sortField, boolean sortAscending) {
super();
this.targetMethod = prepareTargetMethod(sortField);
this.sortAscending = sortAscending;
}
/**
* {@inheritDoc}
*/
public int compare(Object o1, Object o2) {
int response = LESSER;
Object v1,v2;
String returnType;
try {
if(this.targetMethod==null){
v1=o1;
v2=02;
returnType=o1.getClass().getName();
}else{
v1=getValue(o1);
v2=getValue(o2);
returnType=getMethod(o1).getReturnType().getName();
}
CompareMode cm = findCompareMode(v1, v2);
if (!cm.equals(CompareMode.DEFAULT)) {
return compareAlternate(cm);
}
response = compareActual(v1, v2, returnType);
}
// in JSE 1.7 the below is accepted
/*
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException nsme) {
} */
catch (NoSuchMethodException e){
LoggerStackTraceUtil.printErrorMessage(e);
}catch (IllegalAccessException e){
LoggerStackTraceUtil.printErrorMessage(e);
}catch (InvocationTargetException e){
LoggerStackTraceUtil.printErrorMessage(e);
}
return response;
}
//---------------------------------------------------------------------------------//
// Private methods used by {@link com.myjeeva.comparator.GenericComparator} //
//---------------------------------------------------------------------------------//
/**
* alternate to actual value comparison i.e., either (lsh & rhs) one the value could be null
*
* @param cm - a enum used to idetify the position for sorting
*/
protected int compareAlternate(CompareMode cm) {
int compareState = LESSER;
switch(cm) {
case LESS_THAN:
compareState = LESSER * determinePosition();
break;
case GREATER_THAN:
compareState = GREATER * determinePosition();
break;
case EQUAL:
compareState = EQUAL * determinePosition();
break;
}
return compareState;
}
/**
* actual value comparison for sorting; both lsh & rhs value available
*
* @param v1 - value of lhs
* @param v2 - value of rhs
* @param returnType - datatype of given values
* @return int - compare return value
*/
private int compareActual(Object v1, Object v2, String returnType) {
String obj = returnType;
if ("java.lang.Object".equals(obj) && v1!=null) {
obj = v1.getClass().getName();
}
int acutal = LESSER;
if (obj.equals(DATATYPE_INTEGER)) {
acutal = ((Integer) v1).compareTo((Integer) v2) * determinePosition();
} else if (obj.equals(DATATYPE_LONG)) {
acutal = ((Long) v1).compareTo((Long) v2) * determinePosition();
} else if (obj.equals(DATATYPE_STRING)) {
acutal = ((String) v1).compareTo((String) v2) * determinePosition();
} else if (obj.equals(DATATYPE_DATE)) {
acutal = ((Date) v1).compareTo((Date) v2) * determinePosition();
} else if (obj.equals(DATATYPE_FLOAT)) {
acutal = ((Float) v1).compareTo((Float) v2) * determinePosition();
} else if (obj.equals(DATATYPE_DOUBLE)) {
acutal = ((Double) v1).compareTo((Double) v2) * determinePosition();
} else if (obj.equals(DATATYPE_BOOLEAN)) {
acutal = ((Boolean) v1).compareTo((Boolean) v2) * determinePosition();
}
return acutal;
}
/**
* preparing target name of getter method for given sort field
*
* @param name a {@link java.lang.String}
* @return methodName a {@link java.lang.String}
*/
protected final static String prepareTargetMethod(String name) {
StringBuffer fieldName = new StringBuffer(METHOD_GET_PREFIX);
fieldName.append(name.substring(0, 1).toUpperCase()).append(name.substring(1));
return fieldName.toString();
}
/**
* fetching method from <code>Class</code> object through reflect
*
* @param obj - a {@link java.lang.Object} - input object
* @return method - a {@link java.lang.reflect.Method}
* @throws NoSuchMethodException
*/
protected final Method getMethod(Object obj) throws NoSuchMethodException {
return obj.getClass().getMethod(targetMethod, null);
}
/**
* dynamically invoking given method with given object through reflect
*
* @param method - a {@link java.lang.reflect.Method}
* @param obj - a {@link java.lang.Object}
* @return object - a {@link java.lang.Object} - return of given method
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
private final static Object invoke(Method method, Object obj) throws InvocationTargetException, IllegalAccessException {
return method.invoke(obj, null);
}
/**
* fetching a value from given object
*
* @param obj - a {@link java.lang.Object}
* @return object - a {@link java.lang.Object} - return of given method
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws NoSuchMethodException
*/
protected Object getValue(Object obj) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
return invoke(getMethod(obj), obj);
}
/**
* identifying the comparison mode for given value
*
* @param o1 - a {@link java.lang.Object}
* @param o2 - a {@link java.lang.Object}
* @return compareMode - a {@link com.edurekademo.utilities.GenericComparator.CompareMode}
*/
protected CompareMode findCompareMode(Object o1, Object o2) {
CompareMode cm = CompareMode.LESS_THAN;
if(null != o1 & null != o2) {
cm = CompareMode.DEFAULT;
} else if (null == o1 & null != o2) {
cm = CompareMode.LESS_THAN;
} else if (null != o1 & null == o2) {
cm = CompareMode.GREATER_THAN;
} else if (null == o1 & null == o2) {
cm = CompareMode.EQUAL;
}
return cm;
}
/**
* Determining positing for sorting
*
* @return -1 to change the sort order if appropriate.
*/
protected int determinePosition() {
return sortAscending ? GREATER : LESSER;
}
}