001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.Serializable;
018    
019    import java.util.Comparator;
020    
021    /**
022     * @author Hugo Huijser
023     */
024    public class NumericalStringComparator
025            implements Comparator<String>, Serializable {
026    
027            public NumericalStringComparator() {
028                    this(true, false);
029            }
030    
031            public NumericalStringComparator(boolean ascending, boolean caseSensitive) {
032                    _ascending = ascending;
033                    _caseSensitive = caseSensitive;
034            }
035    
036            public int compare(String s1, String s2) {
037                    if (s1 == null) {
038                            s1 = StringPool.BLANK;
039                    }
040    
041                    if (s2 == null) {
042                            s2 = StringPool.BLANK;
043                    }
044    
045                    int startsWithWeight = StringUtil.startsWithWeight(s1, s2);
046    
047                    boolean lastMatchingCharIsDigit = false;
048    
049                    if ((startsWithWeight > 0) &&
050                            Validator.isDigit(s1.charAt(startsWithWeight - 1))) {
051    
052                            lastMatchingCharIsDigit = true;
053                    }
054    
055                    s1 = s1.substring(startsWithWeight);
056                    s2 = s2.substring(startsWithWeight);
057    
058                    String leadingDigits1 = StringUtil.extractLeadingDigits(s1);
059                    String leadingDigits2 = StringUtil.extractLeadingDigits(s2);
060    
061                    int value = 0;
062    
063                    if ((lastMatchingCharIsDigit &&
064                             (Validator.isNotNull(leadingDigits1) ||
065                              Validator.isNotNull(leadingDigits2))) ||
066                            (Validator.isNotNull(leadingDigits1) &&
067                             Validator.isNotNull(leadingDigits2))) {
068    
069                            if (leadingDigits1.length() != leadingDigits2.length()) {
070                                    value = leadingDigits1.length() - leadingDigits2.length();
071                            }
072                            else {
073                                    int i1 = GetterUtil.getInteger(leadingDigits1);
074                                    int i2 = GetterUtil.getInteger(leadingDigits2);
075    
076                                    value = i1 - i2;
077                            }
078                    }
079                    else {
080                            if (_caseSensitive) {
081                                    value = s1.compareTo(s2);
082                            }
083                            else {
084                                    value = s1.compareToIgnoreCase(s2);
085                            }
086                    }
087    
088                    if (_ascending) {
089                            return value;
090                    }
091                    else {
092                            return -value;
093                    }
094            }
095    
096            private boolean _ascending;
097            private boolean _caseSensitive;
098    
099    }