1672 public int compare(String s1, String s2) { |
1673 public int compare(String s1, String s2) { |
1673 return getKey(s1).compareTo(getKey(s2)); |
1674 return getKey(s1).compareTo(getKey(s2)); |
1674 } |
1675 } |
1675 } |
1676 } |
1676 |
1677 |
|
1678 private Comparator<Element> moduleComparator = null; |
1677 /** |
1679 /** |
1678 * Comparator for ModuleElements, simply compares the fully qualified names |
1680 * Comparator for ModuleElements, simply compares the fully qualified names |
1679 * @return a Comparator |
1681 * @return a Comparator |
1680 */ |
1682 */ |
1681 public Comparator<Element> makeModuleComparator() { |
1683 public Comparator<Element> makeModuleComparator() { |
1682 return new Utils.ElementComparator<Element>() { |
1684 if (moduleComparator == null) { |
1683 @Override |
1685 moduleComparator = new Utils.ElementComparator() { |
1684 public int compare(Element mod1, Element mod2) { |
1686 @Override |
1685 return compareFullyQualifiedNames(mod1, mod2); |
1687 public int compare(Element mod1, Element mod2) { |
1686 } |
1688 return compareFullyQualifiedNames(mod1, mod2); |
1687 }; |
1689 } |
1688 } |
1690 }; |
1689 |
1691 } |
|
1692 return moduleComparator; |
|
1693 } |
|
1694 |
|
1695 private Comparator<Element> allClassesComparator = null; |
1690 /** |
1696 /** |
1691 * Returns a Comparator for all classes, compares the simple names of |
1697 * Returns a Comparator for all classes, compares the simple names of |
1692 * TypeElement, if equal then the fully qualified names. |
1698 * TypeElement, if equal then the fully qualified names. |
1693 * |
1699 * |
1694 * @return Comparator |
1700 * @return Comparator |
1695 */ |
1701 */ |
1696 public Comparator<Element> makeAllClassesComparator() { |
1702 public Comparator<Element> makeAllClassesComparator() { |
1697 return new Utils.ElementComparator<Element>() { |
1703 if (allClassesComparator == null) { |
1698 @Override |
1704 allClassesComparator = new Utils.ElementComparator() { |
1699 public int compare(Element e1, Element e2) { |
1705 @Override |
1700 int result = compareNames(e1, e2); |
1706 public int compare(Element e1, Element e2) { |
1701 if (result == 0) |
1707 int result = compareNames(e1, e2); |
1702 result = compareFullyQualifiedNames(e1, e2); |
1708 if (result == 0) |
1703 |
1709 result = compareFullyQualifiedNames(e1, e2); |
1704 return result; |
1710 |
1705 } |
1711 return result; |
1706 }; |
1712 } |
1707 } |
1713 }; |
1708 |
1714 } |
|
1715 return allClassesComparator; |
|
1716 } |
|
1717 |
|
1718 private Comparator<Element> packageComparator = null; |
1709 /** |
1719 /** |
1710 * Returns a Comparator for packages, by comparing the fully qualified names. |
1720 * Returns a Comparator for packages, by comparing the fully qualified names. |
1711 * |
1721 * |
1712 * @return a Comparator |
1722 * @return a Comparator |
1713 */ |
1723 */ |
1714 public Comparator<Element> makePackageComparator() { |
1724 public Comparator<Element> makePackageComparator() { |
1715 return new Utils.ElementComparator<Element>() { |
1725 if (packageComparator == null) { |
1716 @Override |
1726 packageComparator = new Utils.ElementComparator() { |
1717 public int compare(Element pkg1, Element pkg2) { |
1727 @Override |
1718 return compareFullyQualifiedNames(pkg1, pkg2); |
1728 public int compare(Element pkg1, Element pkg2) { |
1719 } |
1729 return compareFullyQualifiedNames(pkg1, pkg2); |
1720 }; |
1730 } |
1721 } |
1731 }; |
1722 |
1732 } |
|
1733 return packageComparator; |
|
1734 } |
|
1735 |
|
1736 private Comparator<SerialFieldTree> serialFieldTreeComparator = null; |
1723 /** |
1737 /** |
1724 * Returns a Comparator for SerialFieldTree. |
1738 * Returns a Comparator for SerialFieldTree. |
1725 * @return a Comparator |
1739 * @return a Comparator |
1726 */ |
1740 */ |
1727 public Comparator<SerialFieldTree> makeSerialFieldTreeComparator() { |
1741 public Comparator<SerialFieldTree> makeSerialFieldTreeComparator() { |
1728 return (SerialFieldTree o1, SerialFieldTree o2) -> { |
1742 if (serialFieldTreeComparator == null) { |
1729 String s1 = o1.getName().toString(); |
1743 serialFieldTreeComparator = (SerialFieldTree o1, SerialFieldTree o2) -> { |
1730 String s2 = o2.getName().toString(); |
1744 String s1 = o1.getName().toString(); |
1731 return s1.compareTo(s2); |
1745 String s2 = o2.getName().toString(); |
1732 }; |
1746 return s1.compareTo(s2); |
|
1747 }; |
|
1748 } |
|
1749 return serialFieldTreeComparator; |
1733 } |
1750 } |
1734 |
1751 |
1735 /** |
1752 /** |
1736 * Returns a general purpose comparator. |
1753 * Returns a general purpose comparator. |
1737 * @return a Comparator |
1754 * @return a Comparator |
1738 */ |
1755 */ |
1739 public Comparator<Element> makeGeneralPurposeComparator() { |
1756 public Comparator<Element> makeGeneralPurposeComparator() { |
1740 return makeClassUseComparator(); |
1757 return makeClassUseComparator(); |
1741 } |
1758 } |
1742 |
1759 |
|
1760 private Comparator<Element> overrideUseComparator = null; |
1743 /** |
1761 /** |
1744 * Returns a Comparator for overrides and implements, |
1762 * Returns a Comparator for overrides and implements, |
1745 * used primarily on methods, compares the name first, |
1763 * used primarily on methods, compares the name first, |
1746 * then compares the simple names of the enclosing |
1764 * then compares the simple names of the enclosing |
1747 * TypeElement and the fully qualified name of the enclosing TypeElement. |
1765 * TypeElement and the fully qualified name of the enclosing TypeElement. |
1748 * @return a Comparator |
1766 * @return a Comparator |
1749 */ |
1767 */ |
1750 public Comparator<Element> makeOverrideUseComparator() { |
1768 public Comparator<Element> makeOverrideUseComparator() { |
1751 return new Utils.ElementComparator<Element>() { |
1769 if (overrideUseComparator == null) { |
1752 @Override |
1770 overrideUseComparator = new Utils.ElementComparator() { |
1753 public int compare(Element o1, Element o2) { |
1771 @Override |
1754 int result = compareStrings(getSimpleName(o1), getSimpleName(o2)); |
1772 public int compare(Element o1, Element o2) { |
1755 if (result != 0) { |
1773 int result = compareStrings(getSimpleName(o1), getSimpleName(o2)); |
1756 return result; |
1774 if (result != 0) { |
1757 } |
1775 return result; |
1758 if (!isTypeElement(o1) && !isTypeElement(o2) && !isPackage(o1) && !isPackage(o2)) { |
1776 } |
1759 TypeElement t1 = getEnclosingTypeElement(o1); |
1777 if (!isTypeElement(o1) && !isTypeElement(o2) && !isPackage(o1) && !isPackage(o2)) { |
1760 TypeElement t2 = getEnclosingTypeElement(o2); |
1778 TypeElement t1 = getEnclosingTypeElement(o1); |
1761 result = compareStrings(getSimpleName(t1), getSimpleName(t2)); |
1779 TypeElement t2 = getEnclosingTypeElement(o2); |
|
1780 result = compareStrings(getSimpleName(t1), getSimpleName(t2)); |
|
1781 if (result != 0) |
|
1782 return result; |
|
1783 } |
|
1784 result = compareStrings(getFullyQualifiedName(o1), getFullyQualifiedName(o2)); |
1762 if (result != 0) |
1785 if (result != 0) |
1763 return result; |
1786 return result; |
1764 } |
1787 return compareElementTypeKinds(o1, o2); |
1765 result = compareStrings(getFullyQualifiedName(o1), getFullyQualifiedName(o2)); |
1788 } |
1766 if (result != 0) |
1789 }; |
1767 return result; |
1790 } |
1768 return compareElementTypeKinds(o1, o2); |
1791 return overrideUseComparator; |
1769 } |
1792 } |
1770 }; |
1793 |
1771 } |
1794 private Comparator<Element> indexUseComparator = null; |
1772 |
1795 /** |
1773 /** |
1796 * Returns a Comparator for index file presentations, and are sorted as follows. |
1774 * Returns a Comparator for index file presentations, and are sorted as follows. |
1797 * If comparing modules and/or packages then simply compare the qualified names, |
1775 * If comparing modules and packages then simply compare the qualified names, if comparing a module |
1798 * if comparing a module or a package with a type/member then compare the |
1776 * or a package with a type/member then compare the FullyQualifiedName of the module or a package |
1799 * FullyQualifiedName of the module or a package with the SimpleName of the entity, |
1777 * with the SimpleName of the entity, otherwise |
1800 * otherwise: |
1778 * 1. compare the ElementKind ex: Module, Package, Interface etc. |
1801 * 1. compare the ElementKind ex: Module, Package, Interface etc. |
1779 * 2a. if equal and if the type is of ExecutableElement(Constructor, Methods), |
1802 * 2a. if equal and if the type is of ExecutableElement(Constructor, Methods), |
1780 * a case insensitive comparison of parameter the type signatures |
1803 * a case insensitive comparison of parameter the type signatures |
1781 * 2b. if equal, case sensitive comparison of the type signatures |
1804 * 2b. if equal, case sensitive comparison of the type signatures |
1782 * 3. finally, if equal, compare the FQNs of the entities |
1805 * 3. finally, if equal, compare the FQNs of the entities |
1783 * @return a comparator for index file use |
1806 * @return a comparator for index file use |
1784 */ |
1807 */ |
1785 public Comparator<Element> makeIndexUseComparator() { |
1808 public Comparator<Element> makeIndexUseComparator() { |
1786 return new Utils.ElementComparator<Element>() { |
1809 if (indexUseComparator == null) { |
1787 /** |
1810 indexUseComparator = new Utils.ElementComparator() { |
1788 * Compare two given elements, if comparing two modules or two packages, return the |
1811 /** |
1789 * comparison of FullyQualifiedName, if comparing a module or a package with a |
1812 * Compares two elements. |
1790 * type/member then compare the FullyQualifiedName of the module or the package |
1813 * |
1791 * with the SimpleName of the entity, then sort on the kinds, then on |
1814 * @param e1 - an element. |
1792 * the parameters only if the type is an ExecutableElement, |
1815 * @param e2 - an element. |
1793 * the parameters are compared and finally the qualified names. |
1816 * @return a negative integer, zero, or a positive integer as the first |
1794 * |
1817 * argument is less than, equal to, or greater than the second. |
1795 * @param e1 - an element. |
1818 */ |
1796 * @param e2 - an element. |
1819 @Override |
1797 * @return a negative integer, zero, or a positive integer as the first |
1820 public int compare(Element e1, Element e2) { |
1798 * argument is less than, equal to, or greater than the second. |
1821 int result; |
1799 */ |
1822 // first, compare names as appropriate |
1800 @Override |
1823 if ((isModule(e1) || isPackage(e1)) && (isModule(e2) || isPackage(e2))) { |
1801 public int compare(Element e1, Element e2) { |
1824 result = compareFullyQualifiedNames(e1, e2); |
1802 int result = 0; |
1825 } else if (isModule(e1) || isPackage(e1)) { |
1803 if ((isModule(e1) || isPackage(e1)) && (isModule(e2) || isPackage(e2))) { |
1826 result = compareStrings(getFullyQualifiedName(e1), getSimpleName(e2)); |
1804 result = compareFullyQualifiedNames(e1, e2); |
1827 } else if (isModule(e2) || isPackage(e2)) { |
|
1828 result = compareStrings(getSimpleName(e1), getFullyQualifiedName(e2)); |
|
1829 } else { |
|
1830 result = compareNames(e1, e2); |
|
1831 } |
1805 if (result != 0) { |
1832 if (result != 0) { |
1806 return result; |
1833 return result; |
1807 } |
1834 } |
1808 return compareElementTypeKinds(e1, e2); |
1835 // if names are the same, compare element kinds |
1809 } |
1836 result = compareElementTypeKinds(e1, e2); |
1810 if (isModule(e1) || isPackage(e1)) { |
|
1811 result = compareStrings(getFullyQualifiedName(e1), getSimpleName(e2)); |
|
1812 } else if (isModule(e2) || isPackage(e2)) { |
|
1813 result = compareStrings(getSimpleName(e1), getFullyQualifiedName(e2)); |
|
1814 } else { |
|
1815 result = compareNames(e1, e2); |
|
1816 } |
|
1817 if (result != 0) { |
|
1818 return result; |
|
1819 } |
|
1820 result = compareElementTypeKinds(e1, e2); |
|
1821 if (result != 0) { |
|
1822 return result; |
|
1823 } |
|
1824 if (hasParameters(e1)) { |
|
1825 List<? extends VariableElement> parameters1 = ((ExecutableElement)e1).getParameters(); |
|
1826 List<? extends VariableElement> parameters2 = ((ExecutableElement)e2).getParameters(); |
|
1827 result = compareParameters(false, parameters1, parameters2); |
|
1828 if (result != 0) { |
1837 if (result != 0) { |
1829 return result; |
1838 return result; |
1830 } |
1839 } |
1831 result = compareParameters(true, parameters1, parameters2); |
1840 // if element kinds are the same, and are methods, |
1832 if (result != 0) { |
1841 // compare the method parameters |
1833 return result; |
1842 if (hasParameters(e1)) { |
|
1843 List<? extends VariableElement> parameters1 = ((ExecutableElement)e1).getParameters(); |
|
1844 List<? extends VariableElement> parameters2 = ((ExecutableElement)e2).getParameters(); |
|
1845 result = compareParameters(false, parameters1, parameters2); |
|
1846 if (result != 0) { |
|
1847 return result; |
|
1848 } |
|
1849 result = compareParameters(true, parameters1, parameters2); |
|
1850 if (result != 0) { |
|
1851 return result; |
|
1852 } |
1834 } |
1853 } |
1835 } |
1854 // else fall back on fully qualified names |
1836 return compareFullyQualifiedNames(e1, e2); |
1855 return compareFullyQualifiedNames(e1, e2); |
1837 } |
1856 } |
1838 }; |
1857 }; |
1839 } |
1858 } |
1840 |
1859 return indexUseComparator; |
|
1860 } |
|
1861 |
|
1862 private Comparator<TypeMirror> typeMirrorClassUseComparator = null; |
1841 /** |
1863 /** |
1842 * Compares the FullyQualifiedNames of two TypeMirrors |
1864 * Compares the FullyQualifiedNames of two TypeMirrors |
1843 * @return |
1865 * @return |
1844 */ |
1866 */ |
1845 public Comparator<TypeMirror> makeTypeMirrorClassUseComparator() { |
1867 public Comparator<TypeMirror> makeTypeMirrorClassUseComparator() { |
1846 return (TypeMirror type1, TypeMirror type2) -> { |
1868 if (typeMirrorClassUseComparator == null) { |
1847 String s1 = getQualifiedTypeName(type1); |
1869 typeMirrorClassUseComparator = (TypeMirror type1, TypeMirror type2) -> { |
1848 String s2 = getQualifiedTypeName(type2); |
1870 String s1 = getQualifiedTypeName(type1); |
1849 return compareStrings(s1, s2); |
1871 String s2 = getQualifiedTypeName(type2); |
1850 }; |
1872 return compareStrings(s1, s2); |
1851 } |
1873 }; |
1852 |
1874 } |
|
1875 return typeMirrorClassUseComparator; |
|
1876 } |
|
1877 |
|
1878 private Comparator<TypeMirror> typeMirrorIndexUseComparator = null; |
1853 /** |
1879 /** |
1854 * Compares the SimpleNames of TypeMirrors if equal then the |
1880 * Compares the SimpleNames of TypeMirrors if equal then the |
1855 * FullyQualifiedNames of TypeMirrors. |
1881 * FullyQualifiedNames of TypeMirrors. |
1856 * |
1882 * |
1857 * @return |
1883 * @return |
1858 */ |
1884 */ |
1859 public Comparator<TypeMirror> makeTypeMirrorIndexUseComparator() { |
1885 public Comparator<TypeMirror> makeTypeMirrorIndexUseComparator() { |
1860 return (TypeMirror t1, TypeMirror t2) -> { |
1886 if (typeMirrorIndexUseComparator == null) { |
1861 int result = compareStrings(getTypeName(t1, false), getTypeName(t2, false)); |
1887 typeMirrorIndexUseComparator = (TypeMirror t1, TypeMirror t2) -> { |
1862 if (result != 0) |
1888 int result = compareStrings(getTypeName(t1, false), getTypeName(t2, false)); |
1863 return result; |
1889 if (result != 0) |
1864 return compareStrings(getQualifiedTypeName(t1), getQualifiedTypeName(t2)); |
1890 return result; |
1865 }; |
1891 return compareStrings(getQualifiedTypeName(t1), getQualifiedTypeName(t2)); |
|
1892 }; |
|
1893 } |
|
1894 return typeMirrorIndexUseComparator; |
1866 } |
1895 } |
1867 |
1896 |
1868 /** |
1897 /** |
1869 * Get the qualified type name of a TypeMiror compatible with the Element's |
1898 * Get the qualified type name of a TypeMiror compatible with the Element's |
1870 * getQualified name, returns the qualified name of the Reference type |
1899 * getQualified name, returns the qualified name of the Reference type |
1934 return outer ? visit(e.getEnclosingElement()) : e.getSimpleName().toString(); |
1963 return outer ? visit(e.getEnclosingElement()) : e.getSimpleName().toString(); |
1935 } |
1964 } |
1936 }.visit(e); |
1965 }.visit(e); |
1937 } |
1966 } |
1938 |
1967 |
|
1968 private Comparator<Element> classUseComparator = null; |
1939 /** |
1969 /** |
1940 * Comparator for ClassUse presentations, and sorts as follows: |
1970 * Comparator for ClassUse presentations, and sorts as follows: |
1941 * 1. member names |
1971 * 1. member names |
1942 * 2. then fully qualified member names |
1972 * 2. then fully qualified member names |
1943 * 3. then parameter types if applicable |
1973 * 3. then parameter types if applicable |
1944 * 4. finally the element kinds ie. package, class, interface etc. |
1974 * 4. finally the element kinds ie. package, class, interface etc. |
1945 * @return a comparator to sort classes and members for class use |
1975 * @return a comparator to sort classes and members for class use |
1946 */ |
1976 */ |
1947 public Comparator<Element> makeClassUseComparator() { |
1977 public Comparator<Element> makeClassUseComparator() { |
1948 return new Utils.ElementComparator<Element>() { |
1978 if (classUseComparator == null) { |
1949 /** |
1979 classUseComparator = new Utils.ElementComparator() { |
1950 * Compare two Elements, first sort on simple name, and if |
1980 /** |
1951 * applicable on the fully qualified name, and finally if applicable |
1981 * Compares two Elements. |
1952 * on the parameter types. |
1982 * |
1953 * @param e1 - an element. |
1983 * @param e1 - an element. |
1954 * @param e2 - an element. |
1984 * @param e2 - an element. |
1955 * @return a negative integer, zero, or a positive integer as the first |
1985 * @return a negative integer, zero, or a positive integer as the first |
1956 * argument is less than, equal to, or greater than the second. |
1986 * argument is less than, equal to, or greater than the second. |
1957 */ |
1987 */ |
1958 @Override |
1988 @Override |
1959 public int compare(Element e1, Element e2) { |
1989 public int compare(Element e1, Element e2) { |
1960 int result = compareNames(e1, e2); |
1990 int result = compareNames(e1, e2); |
1961 if (result != 0) { |
|
1962 return result; |
|
1963 } |
|
1964 result = compareFullyQualifiedNames(e1, e2); |
|
1965 if (result != 0) { |
|
1966 return result; |
|
1967 } |
|
1968 if (hasParameters(e1) && hasParameters(e2)) { |
|
1969 @SuppressWarnings("unchecked") |
|
1970 List<VariableElement> parameters1 = (List<VariableElement>) ((ExecutableElement)e1).getParameters(); |
|
1971 @SuppressWarnings("unchecked") |
|
1972 List<VariableElement> parameters2 = (List<VariableElement>) ((ExecutableElement)e2).getParameters(); |
|
1973 result = compareParameters(false, parameters1, parameters2); |
|
1974 if (result != 0) { |
1991 if (result != 0) { |
1975 return result; |
1992 return result; |
1976 } |
1993 } |
1977 result = compareParameters(true, parameters1, parameters2); |
1994 result = compareFullyQualifiedNames(e1, e2); |
1978 } |
1995 if (result != 0) { |
1979 if (result != 0) { |
1996 return result; |
1980 return result; |
1997 } |
1981 } |
1998 if (hasParameters(e1) && hasParameters(e2)) { |
1982 return compareElementTypeKinds(e1, e2); |
1999 @SuppressWarnings("unchecked") |
1983 } |
2000 List<VariableElement> parameters1 = (List<VariableElement>)((ExecutableElement)e1).getParameters(); |
1984 }; |
2001 @SuppressWarnings("unchecked") |
|
2002 List<VariableElement> parameters2 = (List<VariableElement>)((ExecutableElement)e2).getParameters(); |
|
2003 result = compareParameters(false, parameters1, parameters2); |
|
2004 if (result != 0) { |
|
2005 return result; |
|
2006 } |
|
2007 result = compareParameters(true, parameters1, parameters2); |
|
2008 } |
|
2009 if (result != 0) { |
|
2010 return result; |
|
2011 } |
|
2012 return compareElementTypeKinds(e1, e2); |
|
2013 } |
|
2014 }; |
|
2015 } |
|
2016 return classUseComparator; |
1985 } |
2017 } |
1986 |
2018 |
1987 /** |
2019 /** |
1988 * A general purpose comparator to sort Element entities, basically provides the building blocks |
2020 * A general purpose comparator to sort Element entities, basically provides the building blocks |
1989 * for creating specific comparators for an use-case. |
2021 * for creating specific comparators for an use-case. |
1990 * @param <T> an Element |
2022 */ |
1991 */ |
2023 private abstract class ElementComparator implements Comparator<Element> { |
1992 private abstract class ElementComparator<T extends Element> implements Comparator<Element> { |
|
1993 /** |
2024 /** |
1994 * compares two parameter arrays by first comparing the length of the arrays, and |
2025 * compares two parameter arrays by first comparing the length of the arrays, and |
1995 * then each Type of the parameter in the array. |
2026 * then each Type of the parameter in the array. |
1996 * @param params1 the first parameter array. |
2027 * @param params1 the first parameter array. |
1997 * @param params2 the first parameter array. |
2028 * @param params2 the first parameter array. |