forked from yeshodha02/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaseInsensitiveComparator.java
More file actions
99 lines (91 loc) · 4.64 KB
/
CaseInsensitiveComparator.java
File metadata and controls
99 lines (91 loc) · 4.64 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
package com.edurekademo.utilities;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CaseInsensitiveComparator extends GenericComparator {
private static final long serialVersionUID = -6836701171640412573L;
private static final Logger LOG =LoggerFactory.getLogger(CaseInsensitiveComparator.class);
/*
* This function call base GenericComparator(boolean sortAscending) class and set whether sorting is in ascending or descending
* sortAscending = true then ascending
* sortAscending = false then descending
*/
public CaseInsensitiveComparator(boolean sortAscending) {
super(sortAscending);
this.targetMethod = null;
this.sortAscending = sortAscending;
}
/*
* This function call base GenericComparator(boolean sortField) class and set which field we need to sort and sort as asc
*/
public CaseInsensitiveComparator(String sortField) {
super(sortField);
this.targetMethod = prepareTargetMethod(sortField);
this.sortAscending = true;
}
/*
* This function call base GenericComparator(boolean sortField,sortAscending) class and set which field we need to sort and sort based on the boolen value given
* sortAscending = true then ascending
* sortAscending = false then descending
*/
public CaseInsensitiveComparator(String sortField, boolean sortAscending) {
super(sortField, sortAscending);
this.targetMethod = prepareTargetMethod(sortField);
this.sortAscending = sortAscending;
}
/*
* (non-Javadoc)
* @see com.edurekademo.utilities.GenericComparator#compare(java.lang.Object, java.lang.Object)
*/
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);
}
catch (NoSuchMethodException e){LOG.error(new LoggerStackTraceUtil().getErrorMessage(e));}
catch (IllegalAccessException e){LOG.error(new LoggerStackTraceUtil().getErrorMessage(e));}
catch (InvocationTargetException e){LOG.error(new LoggerStackTraceUtil().getErrorMessage(e));}
return response;
}
/*
* This Method is the overridden compareActual of GenericComparator.
* If the data type is String then it convert string to upper case and compare it with other.
*/
protected 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).toUpperCase().compareTo(((String) v2).toUpperCase()) * 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();
}
return acutal;
}
}