src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/internal/Rows.java
changeset 50428 8c88df2e8a78
parent 47216 71c04702a3d5
equal deleted inserted replaced
50427:b06f330492cd 50428:8c88df2e8a78
    29  * However, the following notice accompanied the original version of this
    29  * However, the following notice accompanied the original version of this
    30  * file:
    30  * file:
    31  *
    31  *
    32  * The MIT License
    32  * The MIT License
    33  *
    33  *
    34  * Copyright (c) 2004-2014 Paul R. Holser, Jr.
    34  * Copyright (c) 2004-2015 Paul R. Holser, Jr.
    35  *
    35  *
    36  * Permission is hereby granted, free of charge, to any person obtaining
    36  * Permission is hereby granted, free of charge, to any person obtaining
    37  * a copy of this software and associated documentation files (the
    37  * a copy of this software and associated documentation files (the
    38  * "Software"), to deal in the Software without restriction, including
    38  * "Software"), to deal in the Software without restriction, including
    39  * without limitation the rights to use, copy, modify, merge, publish,
    39  * without limitation the rights to use, copy, modify, merge, publish,
    53  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    53  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    54  */
    54  */
    55 
    55 
    56 package jdk.internal.joptsimple.internal;
    56 package jdk.internal.joptsimple.internal;
    57 
    57 
    58 import java.util.LinkedHashSet;
    58 import java.util.ArrayList;
    59 import java.util.Set;
    59 import java.util.List;
    60 
    60 
    61 import static java.lang.Math.*;
    61 import static java.lang.Math.*;
    62 
    62 
    63 import static jdk.internal.joptsimple.internal.Strings.*;
    63 import static jdk.internal.joptsimple.internal.Strings.*;
    64 
    64 
    66  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
    66  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
    67  */
    67  */
    68 public class Rows {
    68 public class Rows {
    69     private final int overallWidth;
    69     private final int overallWidth;
    70     private final int columnSeparatorWidth;
    70     private final int columnSeparatorWidth;
    71     private final Set<Row> rows = new LinkedHashSet<Row>();
    71     private final List<Row> rows = new ArrayList<>();
       
    72 
    72     private int widthOfWidestOption;
    73     private int widthOfWidestOption;
    73     private int widthOfWidestDescription;
    74     private int widthOfWidestDescription;
    74 
    75 
    75     public Rows( int overallWidth, int columnSeparatorWidth ) {
    76     public Rows( int overallWidth, int columnSeparatorWidth ) {
    76         this.overallWidth = overallWidth;
    77         this.overallWidth = overallWidth;
    85         rows.add( row );
    86         rows.add( row );
    86         widthOfWidestOption = max( widthOfWidestOption, row.option.length() );
    87         widthOfWidestOption = max( widthOfWidestOption, row.option.length() );
    87         widthOfWidestDescription = max( widthOfWidestDescription, row.description.length() );
    88         widthOfWidestDescription = max( widthOfWidestDescription, row.description.length() );
    88     }
    89     }
    89 
    90 
    90     private void reset() {
    91     public void reset() {
    91         rows.clear();
    92         rows.clear();
    92         widthOfWidestOption = 0;
    93         widthOfWidestOption = 0;
    93         widthOfWidestDescription = 0;
    94         widthOfWidestDescription = 0;
    94     }
    95     }
    95 
    96 
    96     public void fitToWidth() {
    97     public void fitToWidth() {
    97         Columns columns = new Columns( optionWidth(), descriptionWidth() );
    98         Columns columns = new Columns( optionWidth(), descriptionWidth() );
    98 
    99 
    99         Set<Row> fitted = new LinkedHashSet<Row>();
   100         List<Row> fitted = new ArrayList<>();
   100         for ( Row each : rows )
   101         for ( Row each : rows )
   101             fitted.addAll( columns.fit( each ) );
   102             fitted.addAll( columns.fit( each ) );
   102 
   103 
   103         reset();
   104         reset();
   104 
   105 
   120     private int optionWidth() {
   121     private int optionWidth() {
   121         return min( ( overallWidth - columnSeparatorWidth ) / 2, widthOfWidestOption );
   122         return min( ( overallWidth - columnSeparatorWidth ) / 2, widthOfWidestOption );
   122     }
   123     }
   123 
   124 
   124     private int descriptionWidth() {
   125     private int descriptionWidth() {
   125         return min( ( overallWidth - columnSeparatorWidth ) / 2, widthOfWidestDescription );
   126         return min( overallWidth - optionWidth() - columnSeparatorWidth, widthOfWidestDescription );
   126     }
   127     }
   127 
   128 
   128     private StringBuilder pad( StringBuilder buffer, String s, int length ) {
   129     private StringBuilder pad( StringBuilder buffer, String s, int length ) {
   129         buffer.append( s ).append( repeat( ' ', length - s.length() ) );
   130         buffer.append( s ).append( repeat( ' ', length - s.length() ) );
   130         return buffer;
   131         return buffer;