779 elt.name().contentEquals(ElementType.METHOD.name()) || |
779 elt.name().contentEquals(ElementType.METHOD.name()) || |
780 elt.name().contentEquals(ElementType.PACKAGE.name()) || |
780 elt.name().contentEquals(ElementType.PACKAGE.name()) || |
781 elt.name().contentEquals(ElementType.PARAMETER.name()) || |
781 elt.name().contentEquals(ElementType.PARAMETER.name()) || |
782 elt.name().contentEquals(ElementType.TYPE.name()); |
782 elt.name().contentEquals(ElementType.TYPE.name()); |
783 } |
783 } |
|
784 |
|
785 /** |
|
786 * A general purpose String comparator, which compares two Strings using a Collator |
|
787 * strength of "SECONDARY", thus providing optimum case insensitive comparisons in |
|
788 * most Locales. |
|
789 * |
|
790 * @param s1 first String to compare. |
|
791 * @param s2 second String to compare. |
|
792 * @return a negative integer, zero, or a positive integer as the first |
|
793 * argument is less than, equal to, or greater than the second. |
|
794 */ |
|
795 public static int compareStrings(String s1, String s2) { |
|
796 Collator collator = Collator.getInstance(); |
|
797 collator.setStrength(Collator.SECONDARY); |
|
798 return collator.compare(s1, s2); |
|
799 } |
|
800 |
|
801 /** |
|
802 * A comparator for index file uses, this sorts first on names, then on |
|
803 * parameter types and finally on the fully qualified name. |
|
804 * @return a comparator for index file use |
|
805 */ |
|
806 public static Comparator<Doc> makeComparatorForIndexUse() { |
|
807 return new Util.DocComparator<Doc>() { |
|
808 /** |
|
809 * compare two given Doc entities, first sort on name, if |
|
810 * applicable on the method's parameter types, and finally on the |
|
811 * fully qualified name of the entity. |
|
812 * |
|
813 * @param d1 - a Doc element. |
|
814 * @param d2 - a Doc element. |
|
815 * @return a negative integer, zero, or a positive integer as the first |
|
816 * argument is less than, equal to, or greater than the second. |
|
817 */ |
|
818 public int compare(Doc d1, Doc d2) { |
|
819 int result = compareStrings(d1.name(), d2.name()); |
|
820 if (result != 0) { |
|
821 return result; |
|
822 } |
|
823 if (d1 instanceof ExecutableMemberDoc && d2 instanceof ExecutableMemberDoc) { |
|
824 result = compareExecutableMembers( |
|
825 (ExecutableMemberDoc) d1, |
|
826 (ExecutableMemberDoc) d2); |
|
827 if (result != 0) { |
|
828 return result; |
|
829 } |
|
830 } |
|
831 if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) { |
|
832 return compareProgramElementDoc((ProgramElementDoc)d1, (ProgramElementDoc)d2); |
|
833 } |
|
834 return 0; |
|
835 } |
|
836 }; |
|
837 } |
|
838 |
|
839 /** |
|
840 * Comparator for ClassUse representations, this sorts on member names, |
|
841 * fully qualified member names and then the parameter types if applicable. |
|
842 * @return a comparator to sort classes and members for class use |
|
843 */ |
|
844 public static Comparator<Doc> makeComparatorForClassUse() { |
|
845 return new Util.DocComparator<Doc>() { |
|
846 /** |
|
847 * compare two given Doc entities, first sort on name, and if |
|
848 * applicable on the fully qualified name, and finally if applicable |
|
849 * on the parameter types. |
|
850 * @param d1 - a Doc element. |
|
851 * @param d2 - a Doc element. |
|
852 * @return a negative integer, zero, or a positive integer as the first |
|
853 * argument is less than, equal to, or greater than the second. |
|
854 */ |
|
855 public int compare(Doc d1, Doc d2) { |
|
856 int result = compareStrings(d1.name(), d2.name()); |
|
857 if (result != 0) { |
|
858 return result; |
|
859 } |
|
860 if (d1 instanceof ProgramElementDoc && d2 instanceof ProgramElementDoc) { |
|
861 result = compareProgramElementDoc((ProgramElementDoc) d1, (ProgramElementDoc) d2); |
|
862 if (result != 0) { |
|
863 return result; |
|
864 } |
|
865 } |
|
866 if (d1 instanceof ExecutableMemberDoc && d2 instanceof ExecutableMemberDoc) { |
|
867 return compareExecutableMembers((ExecutableMemberDoc)d1, (ExecutableMemberDoc)d2); |
|
868 } |
|
869 return 0; |
|
870 } |
|
871 }; |
|
872 } |
|
873 |
|
874 /** |
|
875 * A general purpose comparator to sort Doc entities, basically provides the building blocks |
|
876 * for creating specific comparators for an use-case. |
|
877 * @param <T> a Doc entity |
|
878 */ |
|
879 static abstract class DocComparator<T extends Doc> implements Comparator<Doc> { |
|
880 /** |
|
881 * compares two parameter arrays by first comparing the length of the arrays, and |
|
882 * then each Type of the parameter in the array. |
|
883 * @param params1 the first parameter array. |
|
884 * @param params2 the first parameter array. |
|
885 * @return a negative integer, zero, or a positive integer as the first |
|
886 * argument is less than, equal to, or greater than the second. |
|
887 */ |
|
888 protected int compareParameters(Parameter[] params1, Parameter[] params2) { |
|
889 if (params1.length == 0 && params2.length == 0) { |
|
890 return 0; |
|
891 } |
|
892 int result = Integer.compare(params1.length, params2.length); |
|
893 if (result != 0) { |
|
894 return result; |
|
895 } |
|
896 for (int i = 0; i < params1.length; i++) { |
|
897 result = compareStrings(params1[i].typeName(), params2[i].typeName()); |
|
898 if (result != 0) { |
|
899 return result; |
|
900 } |
|
901 } |
|
902 return 0; |
|
903 } |
|
904 |
|
905 /** |
|
906 * Compares two MemberDocs, typically the name of a method, |
|
907 * field or constructor. |
|
908 * @param e1 the first MemberDoc. |
|
909 * @param e2 the second MemberDoc. |
|
910 * @return a negative integer, zero, or a positive integer as the first |
|
911 * argument is less than, equal to, or greater than the second. |
|
912 */ |
|
913 protected int compareMembers(MemberDoc e1, MemberDoc e2) { |
|
914 return compareStrings(e1.name(), e2.name()); |
|
915 } |
|
916 |
|
917 /** |
|
918 * Compares two ExecutableMemberDocs such as methods and constructors, |
|
919 * as well as the parameters the entity might take. |
|
920 * @param m1 the first ExecutableMemberDoc. |
|
921 * @param m2 the second ExecutableMemberDoc. |
|
922 * @return a negative integer, zero, or a positive integer as the first |
|
923 * argument is less than, equal to, or greater than the second. |
|
924 */ |
|
925 protected int compareExecutableMembers(ExecutableMemberDoc m1, ExecutableMemberDoc m2) { |
|
926 int result = compareMembers(m1, m2); |
|
927 if (result == 0) |
|
928 result = compareParameters(m1.parameters(), m2.parameters()); |
|
929 return result; |
|
930 } |
|
931 |
|
932 /** |
|
933 * Compares the fully qualified names of the entities |
|
934 * @param p1 the first ProgramElementDoc. |
|
935 * @param p2 the first ProgramElementDoc. |
|
936 * @return a negative integer, zero, or a positive integer as the first |
|
937 * argument is less than, equal to, or greater than the second. |
|
938 */ |
|
939 protected int compareProgramElementDoc(ProgramElementDoc p1, ProgramElementDoc p2) { |
|
940 return compareStrings(p1.qualifiedName(), p2.qualifiedName()); |
|
941 } |
|
942 } |
784 } |
943 } |