001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.comparator;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.util.type.KualiDecimal;
020    
021    import java.io.Serializable;
022    import java.util.Comparator;
023    
024    public class NumericValueComparator implements Serializable, Comparator {
025    
026        static final long serialVersionUID = 3449202365486147519L;
027    
028        private static final NumericValueComparator theInstance = new NumericValueComparator();
029        
030        public NumericValueComparator() {
031        }
032        
033        public static NumericValueComparator getInstance() {
034            return theInstance;
035        }
036        
037        public int compare(Object o1, Object o2) {
038    
039            // null guard. non-null value is greater. equal if both are null
040            if (null == o1 || null == o2) {
041                return (null == o1 && null == o2) ? 0 : ((null == o1) ? -1 : 1);
042            }
043    
044            String numericCompare1 = (String) o1;
045            String numericCompare2 = (String) o2;
046    
047            numericCompare1 = StringUtils.replace(numericCompare1, ",", "");
048            numericCompare1 = StringUtils.replace(numericCompare1, "$", "");
049            numericCompare1 = StringUtils.replace(numericCompare1, " ", "");
050            numericCompare2 = StringUtils.replace(numericCompare2, ",", "");
051            numericCompare2 = StringUtils.replace(numericCompare2, "$", "");
052            numericCompare2 = StringUtils.replace(numericCompare2, " ", "");
053    
054            // handle negatives
055            if (StringUtils.contains(numericCompare1, "(")) {
056                numericCompare1 = StringUtils.replace(numericCompare1, "(", "");
057                numericCompare1 = StringUtils.replace(numericCompare1, ")", "");
058                numericCompare1 = "-" + numericCompare1;
059            }
060    
061            if (StringUtils.contains(numericCompare2, "(")) {
062                numericCompare2 = StringUtils.replace(numericCompare2, "(", "");
063                numericCompare2 = StringUtils.replace(numericCompare2, ")", "");
064                numericCompare2 = "-" + numericCompare2;
065            }
066    
067            KualiDecimal k1 = null;
068            try {
069                k1 = new KualiDecimal(numericCompare1);
070            }
071            catch (Throwable t) {
072                k1 = KualiDecimal.ZERO;
073            }
074    
075            KualiDecimal k2 = null;
076            try {
077                k2 = new KualiDecimal(numericCompare2);
078            }
079            catch (Throwable t) {
080                k2 = KualiDecimal.ZERO;
081            }
082    
083            double d1 = k1.doubleValue();
084            double d2 = k2.doubleValue();
085    
086            return (d1 == d2) ? 0 : ((d1 < d2) ? -1 : 1);
087        }
088    }