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 private static final NumericValueComparator theInstance = new NumericValueComparator();
29
30 public NumericValueComparator() {
31 }
32
33 public static NumericValueComparator getInstance() {
34 return theInstance;
35 }
36
37 public int compare(Object o1, Object o2) {
38
39
40 if (null == o1 || null == o2) {
41 return (null == o1 && null == o2) ? 0 : ((null == o1) ? -1 : 1);
42 }
43
44 String numericCompare1 = (String) o1;
45 String numericCompare2 = (String) o2;
46
47 numericCompare1 = StringUtils.replace(numericCompare1, ",", "");
48 numericCompare1 = StringUtils.replace(numericCompare1, "$", "");
49 numericCompare1 = StringUtils.replace(numericCompare1, " ", "");
50 numericCompare2 = StringUtils.replace(numericCompare2, ",", "");
51 numericCompare2 = StringUtils.replace(numericCompare2, "$", "");
52 numericCompare2 = StringUtils.replace(numericCompare2, " ", "");
53
54
55 if (StringUtils.contains(numericCompare1, "(")) {
56 numericCompare1 = StringUtils.replace(numericCompare1, "(", "");
57 numericCompare1 = StringUtils.replace(numericCompare1, ")", "");
58 numericCompare1 = "-" + numericCompare1;
59 }
60
61 if (StringUtils.contains(numericCompare2, "(")) {
62 numericCompare2 = StringUtils.replace(numericCompare2, "(", "");
63 numericCompare2 = StringUtils.replace(numericCompare2, ")", "");
64 numericCompare2 = "-" + numericCompare2;
65 }
66
67 KualiDecimal k1 = null;
68 try {
69 k1 = new KualiDecimal(numericCompare1);
70 }
71 catch (Throwable t) {
72 k1 = KualiDecimal.ZERO;
73 }
74
75 KualiDecimal k2 = null;
76 try {
77 k2 = new KualiDecimal(numericCompare2);
78 }
79 catch (Throwable t) {
80 k2 = KualiDecimal.ZERO;
81 }
82
83 double d1 = k1.doubleValue();
84 double d2 = k2.doubleValue();
85
86 return (d1 == d2) ? 0 : ((d1 < d2) ? -1 : 1);
87 }
88 }