langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java
changeset 25449 3c14a2e16bd6
parent 25300 3b8a5067fe29
equal deleted inserted replaced
25448:40c5263b551f 25449:3c14a2e16bd6
   812         collator.setStrength(caseSensitive ? Collator.TERTIARY : Collator.SECONDARY);
   812         collator.setStrength(caseSensitive ? Collator.TERTIARY : Collator.SECONDARY);
   813         return collator.compare(s1, s2);
   813         return collator.compare(s1, s2);
   814     }
   814     }
   815 
   815 
   816     /**
   816     /**
   817      * A comparator for index file presentations,
   817      * A comparator for index file presentations, and are sorted as follows:
   818      *  1. this sorts first on simple names
   818      *  1. sort on simple names of entities
   819      *  2. if equal, then compare the DocKind ex: Package, Interface etc.
   819      *  2. if equal, then compare the DocKind ex: Package, Interface etc.
   820      *  3a. if equal and if the type is of ExecutableMemberDoc(Constructor, Fields),
   820      *  3a. if equal and if the type is of ExecutableMemberDoc(Constructor, Methods),
   821      *      a case insensitive comparison of parameter types
   821      *      a case insensitive comparison of parameter the type signatures
   822      *  3b. if equal, a case sensitive comparison of parameter types
   822      *  3b. if equal, case sensitive comparison of the type signatures
   823      *  4. finally, if equal, compare the FQNs of the entities
   823      *  4. finally, if equal, compare the FQNs of the entities
   824      * @return a comparator for index file use
   824      * @return a comparator for index file use
   825      */
   825      */
   826     public static Comparator<Doc> makeComparatorForIndexUse() {
   826     public static Comparator<Doc> makeComparatorForIndexUse() {
   827         return new Util.DocComparator<Doc>() {
   827         return new Util.DocComparator<Doc>() {
   828             /**
   828             /**
   829              * Compare two given Doc entities, first sort on name, then on the kinds,
   829              * Compare two given Doc entities, first sort on names, then on the kinds,
   830              * then on the parameters only if the type is an instance of ExecutableMemberDocs,
   830              * then on the parameters only if the type is an instance of ExecutableMemberDocs,
   831              * the parameters are compared ignoring the case first, then a case sensitive comparison,
   831              * the parameters are compared and finally the fully qualified names.
   832              * and finally the fully qualified names.
       
   833              *
   832              *
   834              * @param d1 - a Doc element.
   833              * @param d1 - a Doc element.
   835              * @param d2 - a Doc element.
   834              * @param d2 - a Doc element.
   836              * @return a negative integer, zero, or a positive integer as the first
   835              * @return a negative integer, zero, or a positive integer as the first
   837              *         argument is less than, equal to, or greater than the second.
   836              *         argument is less than, equal to, or greater than the second.
   860                 return compareFullyQualifiedNames(d1, d2);
   859                 return compareFullyQualifiedNames(d1, d2);
   861             }
   860             }
   862         };
   861         };
   863     }
   862     }
   864     /**
   863     /**
   865      * Comparator for ClassUse presentations, and sorts as follows:
   864      * Comparator for ClassUse presentations, and sorted as follows,
   866      * 1. member names
   865      * 1. compares simple names of entities
   867      * 2. then fully qualified member names
   866      * 2. if equal, the fully qualified names of the entities
   868      * 3. then parameter types if applicable
   867      * 3. if equal and if applicable, the string representation of parameter types
       
   868      * 3a. first by using case insensitive comparison
       
   869      * 3b. second by using a case sensitive comparison
   869      * 4. finally the Doc kinds ie. package, class, interface etc.
   870      * 4. finally the Doc kinds ie. package, class, interface etc.
   870      * @return a comparator to sort classes and members for class use
   871      * @return a comparator to sort classes and members for class use
   871      */
   872      */
   872     public static Comparator<Doc> makeComparatorForClassUse() {
   873     public static Comparator<Doc> makeComparatorForClassUse() {
   873         return new Util.DocComparator<Doc>() {
   874         return new Util.DocComparator<Doc>() {
   874             /**
   875             /**
   875              * Compare two given Doc entities, first sort on name, and if
   876              * Compares two given Doc entities, first sort on name, and if
   876              * applicable on the fully qualified name, and if applicable
   877              * applicable on the fully qualified name, and if applicable
   877              * on the parameter types, and finally the DocKind.
   878              * on the parameter types, and finally the DocKind.
   878              * @param d1 - a Doc element.
   879              * @param d1 - a Doc element.
   879              * @param d2 - a Doc element.
   880              * @param d2 - a Doc element.
   880              * @return a negative integer, zero, or a positive integer as the first
   881              * @return a negative integer, zero, or a positive integer as the first
   950          */
   951          */
   951         protected int compareDocKinds(Doc d1, Doc d2) {
   952         protected int compareDocKinds(Doc d1, Doc d2) {
   952             return getDocKind(d1).compareTo(getDocKind(d2));
   953             return getDocKind(d1).compareTo(getDocKind(d2));
   953         }
   954         }
   954         /**
   955         /**
   955          * Compares two parameter arrays by comparing each Type of the parameter in the array,
   956          * Compares arrays of parameters as a string representation of their types.
   956          * and as many as possible, otherwise compare their lengths.
   957          *
   957          * @param ignoreCase specifies case sensitive or insensitive comparison.
   958          * @param ignoreCase specifies case sensitive or insensitive comparison.
   958          * @param params1 the first parameter array.
   959          * @param params1 the first parameter array.
   959          * @param params2 the first parameter array.
   960          * @param params2 the first parameter array.
   960          * @return a negative integer, zero, or a positive integer as the first
   961          * @return a negative integer, zero, or a positive integer as the first argument is less
   961          *         argument is less than, equal to, or greater than the second.
   962          * than, equal to, or greater than the second.
   962          */
   963          */
   963         protected int compareParameters(boolean ignoreCase, Parameter[] params1, Parameter[] params2) {
   964         protected int compareParameters(boolean caseSensitive,
   964             // try to compare as many as possible
   965                                         Parameter[] params1,
   965             for (int i = 0; i < params1.length && i < params2.length; i++) {
   966                                         Parameter[] params2) {
   966                 int result = compareStrings(ignoreCase, params1[i].typeName(), params2[i].typeName());
   967             String s1 = getParametersAsString(params1);
   967                 if (result != 0) {
   968             String s2 = getParametersAsString(params2);
   968                     return result;
   969             return compareStrings(caseSensitive, s1, s2);
   969                 }
   970         }
   970             }
   971         /*
   971             return Integer.compare(params1.length, params2.length);
   972          * This method returns a string representation solely for comparison purposes.
       
   973          */
       
   974         protected String getParametersAsString(Parameter[] params) {
       
   975             StringBuilder sb = new StringBuilder();
       
   976             for (Parameter param : params) {
       
   977                 Type t = param.type();
       
   978                 // add parameter type to arrays, as TypeMirror does.
       
   979                 String tname = (t.asParameterizedType() != null && t.getElementType() != null)
       
   980                         ? t.getElementType() + t.dimension()
       
   981                         : t.toString();
       
   982                 // prefix P for primitive and R for reference types, thus items will
       
   983                 // be ordered naturally.
       
   984                 sb.append(t.isPrimitive() ? "P" : "R").append("-").append(tname).append("-");
       
   985             }
       
   986             return sb.toString();
   972         }
   987         }
   973 
   988 
   974         /**
   989         /**
   975          * Compares two Doc entities typically the simple name of a method,
   990          * Compares two Doc entities typically the simple name of a method,
   976          * field, constructor etc.
   991          * field, constructor etc.