Coverage Report - org.kuali.rice.krad.comparator.NumericValueComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
NumericValueComparator
0%
0/35
0%
0/18
5
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  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  
         // null guard. non-null value is greater. equal if both are null
 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  
         // handle negatives
 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  
 }