001    /**
002     * Copyright (c) 2000-2013 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.tools.sourceformatter;
016    
017    import com.liferay.portal.kernel.util.NumericalStringComparator;
018    import com.liferay.portal.kernel.util.StringUtil;
019    
020    import java.util.Comparator;
021    import java.util.List;
022    
023    /**
024     * @author Hugo Huijser
025     */
026    public class JavaTermComparator implements Comparator<JavaTerm> {
027    
028            @Override
029            public int compare(JavaTerm javaTerm1, JavaTerm javaTerm2) {
030                    int type1 = javaTerm1.getType();
031                    int type2 = javaTerm2.getType();
032    
033                    if (type1 != type2) {
034                            return type1 - type2;
035                    }
036    
037                    String name1 = javaTerm1.getName();
038                    String name2 = javaTerm2.getName();
039    
040                    if (type1 == JavaSourceProcessor.TYPE_VARIABLE_PRIVATE_STATIC) {
041                            if (name2.equals("_log")) {
042                                    return 1;
043                            }
044    
045                            if (name1.equals("_instance") || name1.equals("_log")) {
046                                    return -1;
047                            }
048    
049                            if (name2.equals("_instance")) {
050                                    return 1;
051                            }
052                    }
053    
054                    if (JavaSourceProcessor.isInJavaTermTypeGroup(
055                                    type1, JavaSourceProcessor.TYPE_VARIABLE)) {
056    
057                            if (StringUtil.isUpperCase(name1) &&
058                                    !StringUtil.isLowerCase(name1) &&
059                                    !StringUtil.isUpperCase(name2)) {
060    
061                                    return -1;
062                            }
063    
064                            if (!StringUtil.isUpperCase(name1) &&
065                                    StringUtil.isUpperCase(name2) &&
066                                    !StringUtil.isLowerCase(name2)) {
067    
068                                    return 1;
069                            }
070                    }
071    
072                    if (name1.compareToIgnoreCase(name2) != 0) {
073                            NumericalStringComparator numericalStringComparator =
074                                    new NumericalStringComparator(true, false);
075    
076                            return numericalStringComparator.compare(name1, name2);
077                    }
078    
079                    if (name1.compareTo(name2) != 0) {
080                            NumericalStringComparator numericalStringComparator =
081                                    new NumericalStringComparator(true, true);
082    
083                            return -numericalStringComparator.compare(name1, name2);
084                    }
085    
086                    return _compareParameterTypes(javaTerm1, javaTerm2);
087            }
088    
089            private static int _compareParameterTypes(
090                    JavaTerm javaTerm1, JavaTerm javaTerm2) {
091    
092                    List<String> parameterTypes2 = javaTerm2.getParameterTypes();
093    
094                    if (parameterTypes2.isEmpty()) {
095                            return 1;
096                    }
097    
098                    List<String> parameterTypes1 = javaTerm1.getParameterTypes();
099    
100                    if (parameterTypes1.isEmpty()) {
101                            return -1;
102                    }
103    
104                    for (int i = 0; i < parameterTypes1.size(); i++) {
105                            if (parameterTypes2.size() < (i + 1)) {
106                                    return 1;
107                            }
108    
109                            String parameterType1 = parameterTypes1.get(i);
110                            String parameterType2 = parameterTypes2.get(i);
111    
112                            if ((parameterTypes1.size() != parameterTypes2.size()) &&
113                                    (parameterType1.equals(parameterType2.concat("...")) ||
114                                     parameterType2.equals(parameterType1.concat("...")))) {
115    
116                                    continue;
117                            }
118    
119                            if (parameterType1.compareToIgnoreCase(parameterType2) != 0) {
120                                    return parameterType1.compareToIgnoreCase(parameterType2);
121                            }
122    
123                            if (parameterType1.compareTo(parameterType2) != 0) {
124                                    return -parameterType1.compareTo(parameterType2);
125                            }
126                    }
127    
128                    return -1;
129            }
130    
131    }