| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package joptsimple.internal; |
| 7 | |
|
| 8 | |
import java.text.BreakIterator; |
| 9 | |
import java.util.Comparator; |
| 10 | |
import java.util.LinkedList; |
| 11 | |
import java.util.List; |
| 12 | |
import java.util.Locale; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
public class Column { |
| 20 | 2 | static final Comparator BY_HEIGHT = new Comparator() { |
| 21 | 2 | public int compare( Object first, Object second ) { |
| 22 | 54 | int firstHeight = ( (Column) first ).height(); |
| 23 | 54 | int secondHeight = ( (Column) second ).height(); |
| 24 | |
|
| 25 | 54 | if ( firstHeight < secondHeight ) |
| 26 | 6 | return -1; |
| 27 | 48 | return firstHeight == secondHeight ? 0 : 1; |
| 28 | |
} |
| 29 | |
}; |
| 30 | |
|
| 31 | |
private final String header; |
| 32 | |
private final List data; |
| 33 | |
private int width; |
| 34 | |
private int height; |
| 35 | |
|
| 36 | 174 | Column( String header, int width ) { |
| 37 | 174 | this.header = header; |
| 38 | 174 | this.data = new LinkedList(); |
| 39 | 174 | this.width = Math.max( width, header.length() ); |
| 40 | 174 | this.height = 0; |
| 41 | 174 | } |
| 42 | |
|
| 43 | |
int addCells( Object cellCandidate ) { |
| 44 | 182 | int originalHeight = height; |
| 45 | |
|
| 46 | 182 | String source = String.valueOf( cellCandidate ).trim(); |
| 47 | 182 | BreakIterator words = BreakIterator.getLineInstance( Locale.US ); |
| 48 | 182 | words.setText( source ); |
| 49 | |
|
| 50 | 182 | StringBuffer nextCell = new StringBuffer(); |
| 51 | |
|
| 52 | 182 | for ( int start = words.first(), end = words.next(); |
| 53 | 1120 | end != BreakIterator.DONE; |
| 54 | 938 | start = end, end = words.next() ) { |
| 55 | |
|
| 56 | 938 | nextCell = processNextWord( source, nextCell, start, end ); |
| 57 | |
} |
| 58 | |
|
| 59 | 182 | if ( nextCell.length() > 0 ) |
| 60 | 170 | addCell( nextCell.toString() ); |
| 61 | |
|
| 62 | 182 | return height - originalHeight; |
| 63 | |
} |
| 64 | |
|
| 65 | |
private StringBuffer processNextWord( String source, StringBuffer nextCell, |
| 66 | |
int start, int end ) { |
| 67 | |
|
| 68 | 938 | StringBuffer augmented = nextCell; |
| 69 | |
|
| 70 | 938 | String word = source.substring( start, end ); |
| 71 | 938 | if ( augmented.length() + word.length() > width ) { |
| 72 | 112 | addCell( augmented.toString() ); |
| 73 | 112 | augmented = new StringBuffer( " " ).append( word ); |
| 74 | |
} |
| 75 | |
else |
| 76 | 826 | augmented.append( word ); |
| 77 | |
|
| 78 | 938 | return augmented; |
| 79 | |
} |
| 80 | |
|
| 81 | |
void addCell( String newCell ) { |
| 82 | 332 | data.add( newCell ); |
| 83 | 332 | ++height; |
| 84 | 332 | } |
| 85 | |
|
| 86 | |
void writeHeaderOn( StringBuffer buffer, boolean appendSpace ) { |
| 87 | 72 | buffer.append( header ) |
| 88 | |
.append( Strings.repeat( ' ', width - header.length() ) ); |
| 89 | |
|
| 90 | 72 | if ( appendSpace ) |
| 91 | 36 | buffer.append( ' ' ); |
| 92 | 72 | } |
| 93 | |
|
| 94 | |
void writeSeparatorOn( StringBuffer buffer, boolean appendSpace ) { |
| 95 | 72 | buffer.append( Strings.repeat( '-', header.length() ) ) |
| 96 | |
.append( Strings.repeat( ' ', width - header.length() ) ); |
| 97 | 72 | if ( appendSpace ) |
| 98 | 36 | buffer.append( ' ' ); |
| 99 | 72 | } |
| 100 | |
|
| 101 | |
void writeCellOn( int index, StringBuffer buffer, boolean appendSpace ) { |
| 102 | 248 | if ( index < data.size() ) { |
| 103 | 248 | String item = (String) data.get( index ); |
| 104 | |
|
| 105 | 248 | buffer.append( item ) |
| 106 | |
.append( Strings.repeat( ' ', width - item.length() ) ); |
| 107 | 248 | if ( appendSpace ) |
| 108 | 124 | buffer.append( ' ' ); |
| 109 | |
} |
| 110 | 248 | } |
| 111 | |
|
| 112 | |
int height() { |
| 113 | 144 | return height; |
| 114 | |
} |
| 115 | |
} |