1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.comparator; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.util.type.KualiDecimal; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
import java.util.Comparator; |
23 | |
|
24 | |
public class NumericValueComparator implements Serializable, Comparator { |
25 | |
|
26 | |
static final long serialVersionUID = 3449202365486147519L; |
27 | |
|
28 | 0 | private static final NumericValueComparator theInstance = new NumericValueComparator(); |
29 | |
|
30 | 0 | public NumericValueComparator() { |
31 | 0 | } |
32 | |
|
33 | |
public static NumericValueComparator getInstance() { |
34 | 0 | return theInstance; |
35 | |
} |
36 | |
|
37 | |
public int compare(Object o1, Object o2) { |
38 | |
|
39 | |
|
40 | 0 | if (null == o1 || null == o2) { |
41 | 0 | return (null == o1 && null == o2) ? 0 : ((null == o1) ? -1 : 1); |
42 | |
} |
43 | |
|
44 | 0 | String numericCompare1 = (String) o1; |
45 | 0 | String numericCompare2 = (String) o2; |
46 | |
|
47 | 0 | numericCompare1 = StringUtils.replace(numericCompare1, ",", ""); |
48 | 0 | numericCompare1 = StringUtils.replace(numericCompare1, "$", ""); |
49 | 0 | numericCompare1 = StringUtils.replace(numericCompare1, " ", ""); |
50 | 0 | numericCompare2 = StringUtils.replace(numericCompare2, ",", ""); |
51 | 0 | numericCompare2 = StringUtils.replace(numericCompare2, "$", ""); |
52 | 0 | numericCompare2 = StringUtils.replace(numericCompare2, " ", ""); |
53 | |
|
54 | |
|
55 | 0 | if (StringUtils.contains(numericCompare1, "(")) { |
56 | 0 | numericCompare1 = StringUtils.replace(numericCompare1, "(", ""); |
57 | 0 | numericCompare1 = StringUtils.replace(numericCompare1, ")", ""); |
58 | 0 | numericCompare1 = "-" + numericCompare1; |
59 | |
} |
60 | |
|
61 | 0 | if (StringUtils.contains(numericCompare2, "(")) { |
62 | 0 | numericCompare2 = StringUtils.replace(numericCompare2, "(", ""); |
63 | 0 | numericCompare2 = StringUtils.replace(numericCompare2, ")", ""); |
64 | 0 | numericCompare2 = "-" + numericCompare2; |
65 | |
} |
66 | |
|
67 | 0 | KualiDecimal k1 = null; |
68 | |
try { |
69 | 0 | k1 = new KualiDecimal(numericCompare1); |
70 | |
} |
71 | 0 | catch (Throwable t) { |
72 | 0 | k1 = KualiDecimal.ZERO; |
73 | 0 | } |
74 | |
|
75 | 0 | KualiDecimal k2 = null; |
76 | |
try { |
77 | 0 | k2 = new KualiDecimal(numericCompare2); |
78 | |
} |
79 | 0 | catch (Throwable t) { |
80 | 0 | k2 = KualiDecimal.ZERO; |
81 | 0 | } |
82 | |
|
83 | 0 | double d1 = k1.doubleValue(); |
84 | 0 | double d2 = k2.doubleValue(); |
85 | |
|
86 | 0 | return (d1 == d2) ? 0 : ((d1 < d2) ? -1 : 1); |
87 | |
} |
88 | |
} |