View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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          // null guard. non-null value is greater. equal if both are null
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          // handle negatives
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  }