roxspring    2004/04/19 15:09:51

  Modified:    cli/src/test/org/apache/commons/cli Tag:
                        RESEARCH_CLI_2_ROXSPRING TestHelpFormatter.java
               cli/src/java/org/apache/commons/cli Tag:
                        RESEARCH_CLI_2_ROXSPRING HelpFormatter.java
  Log:
  HelpFormatter now uses predictable sorting, previously:

  "it simply compares the object's toString() values rather than using the getKey() 
method which would perform the sort that the comments seem to indicate is desired"

  Also brought TestHelpFormatter expectations in line.

  PR:24184

  Submitted by: Robert Fletcher <[EMAIL PROTECTED]>

  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.6.4.3   +1 -1      
jakarta-commons/cli/src/test/org/apache/commons/cli/TestHelpFormatter.java
  
  Index: TestHelpFormatter.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cli/src/test/org/apache/commons/cli/TestHelpFormatter.java,v
  retrieving revision 1.6.4.2
  retrieving revision 1.6.4.3
  diff -u -r1.6.4.2 -r1.6.4.3
  --- TestHelpFormatter.java    4 Feb 2004 02:01:53 -0000       1.6.4.2
  +++ TestHelpFormatter.java    19 Apr 2004 22:09:50 -0000      1.6.4.3
  @@ -165,7 +165,7 @@
          assertEquals("simple auto usage", expected, out.toString().trim());
          out.reset();
   
  -       expected = "usage: app [-b] [-a]";
  +       expected = "usage: app [-a] [-b]";
          options = new Options().addOption("a", false, "aaaa aaaa aaaa aaaa aaaa")
          .addOption("b", false, "bbb" );
          hf.printUsage( pw, 60, "app", options );
  
  
  
  No                   revision
  No                   revision
  1.14.2.3  +8 -29     
jakarta-commons/cli/src/java/org/apache/commons/cli/HelpFormatter.java
  
  Index: HelpFormatter.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/HelpFormatter.java,v
  retrieving revision 1.14.2.2
  retrieving revision 1.14.2.3
  diff -u -r1.14.2.2 -r1.14.2.3
  --- HelpFormatter.java        4 Feb 2004 02:01:50 -0000       1.14.2.2
  +++ HelpFormatter.java        19 Apr 2004 22:09:51 -0000      1.14.2.3
  @@ -657,7 +657,7 @@
           Option option;
           List optList = options.helpOptions();
   
  -        Collections.sort(optList, new StringBufferComparator());
  +        Collections.sort(optList, new OptionComparator());
   
           for (Iterator i = optList.iterator(); i.hasNext();)
           {
  @@ -885,9 +885,9 @@
       // ---------------------------------------------------------- Inner classes
       /**
        * <p>This class implements the <code>Comparator</code> interface
  -     * for comparing StringBuffers.</p>
  +     * for comparing Options.</p>
        */
  -    private static class StringBufferComparator
  +    private static class OptionComparator
           implements Comparator {
   
           /**
  @@ -895,8 +895,8 @@
            * integer, zero, or a positive integer as the first argument 
            * is less than, equal to, or greater than the second.</p>
            *
  -         * @param o1 The first StringBuffer to be compared.
  -         * @param o2 The second StringBuffer to be compared.
  +         * @param o1 The first Option to be compared.
  +         * @param o2 The second Option to be compared.
            *
            * @return a negative integer, zero, or a positive integer as 
            * the first argument is less than, equal to, or greater than the 
  @@ -904,31 +904,10 @@
            */
           public int compare(Object o1, Object o2)
           {
  -            String str1 = stripPrefix(o1.toString());
  -            String str2 = stripPrefix(o2.toString());
  +            Option opt1 = (Option)o1;
  +            Option opt2 = (Option)o2;
   
  -            return (str1.compareTo(str2));
  -        }
  -
  -        /**
  -         * <p>Removes any leading hyphens from <code>strOption</code>.</p>
  -         *         
  -         * @param strOption The String from which to strip the leading
  -         * hyphens.
  -         *
  -         * @return The String without any leading hyphens.
  -         */
  -        private String stripPrefix(String strOption)
  -        {
  -            // Strip any leading '-' characters
  -            int iStartIndex = strOption.lastIndexOf('-');
  -
  -            if (iStartIndex == -1)
  -            {
  -                iStartIndex = 0;
  -            }
  -
  -            return strOption.substring(iStartIndex);
  +            return opt1.getKey().compareToIgnoreCase(opt2.getKey());
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to