Coverage Report - joptsimple.internal.Columns
 
Classes in this File Line Coverage Branch Coverage Complexity
Columns
100%
32/32
100%
12/12
2
 
 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.text.BreakIterator;
 29  
 import java.util.ArrayList;
 30  
 import java.util.List;
 31  
 import java.util.Locale;
 32  
 
 33  
 import static java.text.BreakIterator.*;
 34  
 
 35  
 import static joptsimple.internal.Strings.*;
 36  
 
 37  
 /**
 38  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 39  
  */
 40  
 class Columns {
 41  
     private static final int INDENT_WIDTH = 2;
 42  
 
 43  
     private final int optionWidth;
 44  
     private final int descriptionWidth;
 45  
 
 46  131
     Columns( int optionWidth, int descriptionWidth ) {
 47  131
         this.optionWidth = optionWidth;
 48  131
         this.descriptionWidth = descriptionWidth;
 49  131
     }
 50  
 
 51  
     List<Row> fit( Row row ) {
 52  238
         List<String> options = piecesOf( row.option, optionWidth );
 53  238
         List<String> descriptions = piecesOf( row.description, descriptionWidth );
 54  
 
 55  238
         List<Row> rows = new ArrayList<Row>();
 56  522
         for ( int i = 0; i < Math.max( options.size(), descriptions.size() ); ++i )
 57  284
             rows.add( new Row( itemOrEmpty( options, i ), itemOrEmpty( descriptions, i ) ) );
 58  
 
 59  238
         return rows;
 60  
     }
 61  
 
 62  
     private static String itemOrEmpty( List<String> items, int index ) {
 63  568
         return index >= items.size() ? "" : items.get( index );
 64  
     }
 65  
 
 66  
     private List<String> piecesOf( String raw, int width ) {
 67  476
         List<String> pieces = new ArrayList<String>();
 68  
 
 69  956
         for ( String each : raw.trim().split( LINE_SEPARATOR ) )
 70  480
             pieces.addAll( piecesOfEmbeddedLine( each, width ) );
 71  
 
 72  476
         return pieces;
 73  
     }
 74  
 
 75  
     private List<String> piecesOfEmbeddedLine( String line, int width ) {
 76  480
         List<String> pieces = new ArrayList<String>();
 77  
 
 78  480
         BreakIterator words = BreakIterator.getLineInstance( Locale.US );
 79  480
         words.setText( line );
 80  
 
 81  480
         StringBuilder nextPiece = new StringBuilder();
 82  
 
 83  480
         int start = words.first();
 84  1822
         for ( int end = words.next(); end != DONE; start = end, end = words.next() )
 85  1342
             nextPiece = processNextWord( line, nextPiece, start, end, width, pieces );
 86  
 
 87  480
         if ( nextPiece.length() > 0 )
 88  444
             pieces.add( nextPiece.toString() );
 89  
 
 90  480
         return pieces;
 91  
     }
 92  
 
 93  
     private StringBuilder processNextWord( String source, StringBuilder nextPiece, int start, int end, int width,
 94  
                                            List<String> pieces ) {
 95  1342
         StringBuilder augmented = nextPiece;
 96  
 
 97  1342
         String word = source.substring( start, end );
 98  1342
         if ( augmented.length() + word.length() > width ) {
 99  45
             pieces.add( augmented.toString().replaceAll( "\\s+$", "" ) );
 100  45
             augmented = new StringBuilder( repeat( ' ', INDENT_WIDTH ) ).append( word );
 101  
         }
 102  
         else
 103  1297
             augmented.append( word );
 104  
 
 105  1342
         return augmented;
 106  
     }
 107  
 }