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.comparator;
016    
017    import com.liferay.portal.kernel.sourceformatter.JavaTerm;
018    import com.liferay.portal.tools.SourceFormatter;
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            public int compare(JavaTerm javaTerm1, JavaTerm javaTerm2) {
029                    int type1 = javaTerm1.getType();
030                    int type2 = javaTerm2.getType();
031    
032                    if (type1 != type2) {
033                            return type1 - type2;
034                    }
035    
036                    String name1 = javaTerm1.getName();
037                    String name2 = javaTerm2.getName();
038    
039                    if (type1 == SourceFormatter._TYPE_VARIABLE_PRIVATE_STATIC) {
040                            if (name2.equals("_log")) {
041                                    return 1;
042                            }
043    
044                            if (name1.equals("_instance") || name1.equals("_log")) {
045                                    return -1;
046                            }
047    
048                            if (name2.equals("_instance")) {
049                                    return 1;
050                            }
051                    }
052    
053                    if (name1.compareToIgnoreCase(name2) != 0) {
054                            return name1.compareToIgnoreCase(name2);
055                    }
056    
057                    if (name1.compareTo(name2) != 0) {
058                            return -name1.compareTo(name2);
059                    }
060    
061                    return _compareParameterTypes(javaTerm1, javaTerm2);
062            }
063    
064            private static int _compareParameterTypes(
065                    JavaTerm javaTerm1, JavaTerm javaTerm2) {
066    
067                    List<String> parameterTypes2 = javaTerm2.getParameterTypes();
068    
069                    if (parameterTypes2.isEmpty()) {
070                            return 1;
071                    }
072    
073                    List<String> parameterTypes1 = javaTerm1.getParameterTypes();
074    
075                    if (parameterTypes1.isEmpty()) {
076                            return 1;
077                    }
078    
079                    for (int i = 0; i < parameterTypes1.size(); i++) {
080                            if (parameterTypes2.size() < (i + 1)) {
081                                    return 1;
082                            }
083    
084                            String parameterType1 = parameterTypes1.get(i);
085                            String parameterType2 = parameterTypes2.get(i);
086    
087                            if ((parameterTypes1.size() != parameterTypes2.size()) &&
088                                    (parameterType1.equals(parameterType2.concat("...")) ||
089                                     parameterType2.equals(parameterType1.concat("...")))) {
090    
091                                    continue;
092                            }
093    
094                            if (parameterType1.compareToIgnoreCase(parameterType2) != 0) {
095                                    return parameterType1.compareToIgnoreCase(parameterType2);
096                            }
097    
098                            if (parameterType1.compareTo(parameterType2) != 0) {
099                                    return -parameterType1.compareTo(parameterType2);
100                            }
101                    }
102    
103                    return -1;
104            }
105    
106    }