Coverage Report - joptsimple.internal.Rows
 
Classes in this File Line Coverage Branch Coverage Complexity
Rows
100%
33/33
100%
6/6
1.333
 
 1  
 /*
 2  
  The MIT License
 3  
 
 4  
  Copyright (c) 2004-2015 Paul R. Holser, Jr.
 5  
 
 6  
  Permission is hereby granted, free of charge, to any person obtaining
 7  
  a copy of this software and associated documentation files (the
 8  
  "Software"), to deal in the Software without restriction, including
 9  
  without limitation the rights to use, copy, modify, merge, publish,
 10  
  distribute, sublicense, and/or sell copies of the Software, and to
 11  
  permit persons to whom the Software is furnished to do so, subject to
 12  
  the following conditions:
 13  
 
 14  
  The above copyright notice and this permission notice shall be
 15  
  included in all copies or substantial portions of the Software.
 16  
 
 17  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 18  
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 19  
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 20  
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 21  
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 22  
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 23  
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 24  
 */
 25  
 
 26  
 package joptsimple.internal;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.List;
 30  
 
 31  
 import static java.lang.Math.*;
 32  
 
 33  
 import static joptsimple.internal.Strings.*;
 34  
 
 35  
 /**
 36  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 37  
  */
 38  
 public class Rows {
 39  
     private final int overallWidth;
 40  
     private final int columnSeparatorWidth;
 41  819
     private final List<Row> rows = new ArrayList<Row>();
 42  
     private int widthOfWidestOption;
 43  
     private int widthOfWidestDescription;
 44  
 
 45  819
     public Rows( int overallWidth, int columnSeparatorWidth ) {
 46  819
         this.overallWidth = overallWidth;
 47  819
         this.columnSeparatorWidth = columnSeparatorWidth;
 48  819
     }
 49  
 
 50  
     public void add( String option, String description ) {
 51  238
         add( new Row( option, description ) );
 52  238
     }
 53  
 
 54  
     private void add( Row row ) {
 55  522
         rows.add( row );
 56  522
         widthOfWidestOption = max( widthOfWidestOption, row.option.length() );
 57  522
         widthOfWidestDescription = max( widthOfWidestDescription, row.description.length() );
 58  522
     }
 59  
 
 60  
     private void reset() {
 61  131
         rows.clear();
 62  131
         widthOfWidestOption = 0;
 63  131
         widthOfWidestDescription = 0;
 64  131
     }
 65  
 
 66  
     public void fitToWidth() {
 67  131
         Columns columns = new Columns( optionWidth(), descriptionWidth() );
 68  
 
 69  131
         List<Row> fitted = new ArrayList<Row>();
 70  131
         for ( Row each : rows )
 71  238
             fitted.addAll( columns.fit( each ) );
 72  
 
 73  131
         reset();
 74  
 
 75  131
         for ( Row each : fitted )
 76  284
             add( each );
 77  131
     }
 78  
 
 79  
     public String render() {
 80  131
         StringBuilder buffer = new StringBuilder();
 81  
 
 82  131
         for ( Row each : rows ) {
 83  284
             pad( buffer, each.option, optionWidth() ).append( repeat( ' ', columnSeparatorWidth ) );
 84  284
             pad( buffer, each.description, descriptionWidth() ).append( LINE_SEPARATOR );
 85  284
         }
 86  
 
 87  131
         return buffer.toString();
 88  
     }
 89  
 
 90  
     private int optionWidth() {
 91  415
         return min( ( overallWidth - columnSeparatorWidth ) / 2, widthOfWidestOption );
 92  
     }
 93  
 
 94  
     private int descriptionWidth() {
 95  415
         return min( ( overallWidth - columnSeparatorWidth ) / 2, widthOfWidestDescription );
 96  
     }
 97  
 
 98  
     private StringBuilder pad( StringBuilder buffer, String s, int length ) {
 99  568
         buffer.append( s ).append( repeat( ' ', length - s.length() ) );
 100  568
         return buffer;
 101  
     }
 102  
 }