1+ package net .darkhax .bookshelf .api .lib ;
2+
3+ import org .apache .commons .lang3 .StringUtils ;
4+
5+ import java .util .LinkedList ;
6+ import java .util .List ;
7+ import java .util .function .Function ;
8+
9+ public class TableBuilder <T > {
10+
11+ private static final String NEW_LINE = System .lineSeparator ();
12+
13+ private static final String DIVIDER_COLUMN = "|" ;
14+
15+ private static final String DIVIDER_ROW = "-" ;
16+
17+ private final List <TableColumn <T >> columns = new LinkedList <>();
18+ private int entryCount = 0 ;
19+
20+ public TableBuilder <T > addColumn (String name , Function <T , Object > function ) {
21+
22+ this .columns .add (new TableColumn <>(name , function ));
23+ return this ;
24+ }
25+
26+ public void addAll (Iterable <T > entries ) {
27+
28+ entries .forEach (this ::addEntry );
29+ }
30+
31+ public void addEntry (T entry ) {
32+
33+ columns .forEach (column -> column .processValue (entry ));
34+ entryCount ++;
35+ }
36+
37+ @ Override
38+ public String toString () {
39+
40+ final StringBuilder builder = new StringBuilder ();
41+
42+ // Column Names
43+ for (TableColumn <T > column : this .columns ) {
44+
45+ builder .append (DIVIDER_COLUMN ).append (" " ).append (StringUtils .rightPad (column .getTitle (), column .getMaxLength ())).append (" " );
46+ }
47+
48+ builder .append (DIVIDER_COLUMN ).append (NEW_LINE );
49+
50+ // Entry Separator
51+ for (TableColumn <T > column : this .columns ) {
52+
53+ builder .append (DIVIDER_COLUMN ).append (DIVIDER_ROW .repeat (column .getMaxLength () + 2 ));
54+ }
55+
56+ builder .append (DIVIDER_COLUMN ).append (NEW_LINE );
57+
58+ for (int entryIndex = 0 ; entryIndex < entryCount ; entryIndex ++) {
59+
60+ for (TableColumn <T > column : this .columns ) {
61+
62+ builder .append (DIVIDER_COLUMN ).append (" " ).append (StringUtils .rightPad (column .getValue (entryIndex ), column .getMaxLength ())).append (" " );
63+ }
64+
65+ builder .append (DIVIDER_COLUMN );
66+
67+ if (entryIndex != entryCount - 1 ) {
68+
69+ builder .append (NEW_LINE );
70+ }
71+ }
72+
73+ return builder .toString ();
74+ }
75+
76+ public static class TableColumn <T > {
77+
78+ private final String title ;
79+ private final Function <T , Object > valueResolver ;
80+ private final List <String > heldValues ;
81+ private int maxLength ;
82+
83+ public TableColumn (String title , Function <T , Object > valueResolver ) {
84+
85+ this .title = title ;
86+ this .valueResolver = valueResolver ;
87+ this .heldValues = new LinkedList <>();
88+ this .maxLength = this .title .length ();
89+ }
90+
91+ public String getTitle () {
92+
93+ return this .title ;
94+ }
95+
96+ public String getValue (int index ) {
97+
98+ return this .heldValues .get (index );
99+ }
100+
101+ public void processValue (T value ) {
102+
103+ final String valueText = String .valueOf (this .valueResolver .apply (value ));
104+ heldValues .add (valueText );
105+
106+ if (valueText .length () > maxLength ) {
107+
108+ this .maxLength = valueText .length ();
109+ }
110+ }
111+
112+ public int getMaxLength () {
113+
114+ return this .maxLength ;
115+ }
116+ }
117+ }
0 commit comments