01: /*
02: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
03: Licensed under the Academic Free License version 3.0
04: */
05:
06: package joptsimple.util;
07:
08: import java.util.Comparator;
09: import java.util.LinkedList;
10: import java.util.List;
11:
12: /**
13: * @since 2.1
14: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
15: * @version $Id: Column.java,v 1.5 2007/04/10 20:06:27 pholser Exp $
16: */
17: public class Column {
18: static final Comparator BY_HEIGHT = new Comparator() {
19: public int compare(Object first, Object second) {
20: return ((Column) first).height()
21: - ((Column) second).height();
22: }
23: };
24:
25: private final String header;
26: private final List data;
27: private int width;
28: private int height;
29:
30: Column(String header) {
31: this .header = header;
32: data = new LinkedList();
33: width = header.length();
34: height = 0;
35: }
36:
37: void addRow(Object row) {
38: String asString = String.valueOf(row);
39:
40: data.add(asString);
41: width = Math.max(width, asString.length());
42: ++height;
43: }
44:
45: void writeHeaderOn(StringBuffer buffer) {
46: buffer.append(header).append(
47: StringUtilities.repeat(' ', width - header.length()))
48: .append(' ');
49: }
50:
51: void writeSeparatorOn(StringBuffer buffer) {
52: buffer.append(StringUtilities.repeat('-', width)).append(' ');
53: }
54:
55: void writeCellOn(int index, StringBuffer buffer) {
56: if (index < data.size()) {
57: String item = (String) data.get(index);
58:
59: buffer.append(item).append(
60: StringUtilities.repeat(' ', width - item.length()))
61: .append(' ');
62: }
63: }
64:
65: int height() {
66: return height;
67: }
68: }
|