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    
019    import java.util.Comparator;
020    import java.util.List;
021    
022    /**
023     * @author Hugo Huijser
024     */
025    public class JavaTermComparator implements Comparator<JavaTerm> {
026    
027            @Override
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 == JavaSourceProcessor.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                            NumericalStringComparator numericalStringComparator =
055                                    new NumericalStringComparator(true, false);
056    
057                            return numericalStringComparator.compare(name1, name2);
058                    }
059    
060                    if (name1.compareTo(name2) != 0) {
061                            NumericalStringComparator numericalStringComparator =
062                                    new NumericalStringComparator(true, true);
063    
064                            return -numericalStringComparator.compare(name1, name2);
065                    }
066    
067                    return _compareParameterTypes(javaTerm1, javaTerm2);
068            }
069    
070            private static int _compareParameterTypes(
071                    JavaTerm javaTerm1, JavaTerm javaTerm2) {
072    
073                    List<String> parameterTypes2 = javaTerm2.getParameterTypes();
074    
075                    if (parameterTypes2.isEmpty()) {
076                            return 1;
077                    }
078    
079                    List<String> parameterTypes1 = javaTerm1.getParameterTypes();
080    
081                    if (parameterTypes1.isEmpty()) {
082                            return -1;
083                    }
084    
085                    for (int i = 0; i < parameterTypes1.size(); i++) {
086                            if (parameterTypes2.size() < (i + 1)) {
087                                    return 1;
088                            }
089    
090                            String parameterType1 = parameterTypes1.get(i);
091                            String parameterType2 = parameterTypes2.get(i);
092    
093                            if ((parameterTypes1.size() != parameterTypes2.size()) &&
094                                    (parameterType1.equals(parameterType2.concat("...")) ||
095                                     parameterType2.equals(parameterType1.concat("...")))) {
096    
097                                    continue;
098                            }
099    
100                            if (parameterType1.compareToIgnoreCase(parameterType2) != 0) {
101                                    return parameterType1.compareToIgnoreCase(parameterType2);
102                            }
103    
104                            if (parameterType1.compareTo(parameterType2) != 0) {
105                                    return -parameterType1.compareTo(parameterType2);
106                            }
107                    }
108    
109                    return -1;
110            }
111    
112    }