Skip to content

Commit b8127b4

Browse files
Merge pull request MitchTalmadge#12 from chrisgleissner/null-data-support
Custom null value handling, closes MitchTalmadge#11
2 parents d1292a2 + b346d4a commit b8127b4

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

src/main/java/com/mitchtalmadge/asciidata/table/ASCIITable.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class ASCIITable {
1919
private final int[] columnWidths;
2020
private final int emptyWidth;
2121
private final String emptyMessage = "(empty)";
22+
private String nullValue = "";
2223

2324
/**
2425
* How the table will be displayed. Defines which characters to be used.
@@ -76,7 +77,7 @@ private ASCIITable(String[] headers, String[][] data) {
7677
// Iterate over each column in the row to get its width, and compare it to the maximum.
7778
for (int column = 0; column < columnsCount; column++) {
7879
// Check the length of each line in the cell.
79-
for (String rowDataLine : rowData[column].split("\\n"))
80+
for (String rowDataLine : nullSafeData(rowData[column]).split("\\n"))
8081
// Compare to the current max width.
8182
columnWidths[column] = Math.max(columnWidths[column], rowDataLine.length());
8283
}
@@ -110,6 +111,17 @@ public ASCIITable withTableFormat(TableFormatAbstract tableFormat) {
110111
return this;
111112
}
112113

114+
/**
115+
* Changes the value used for rendering <code>null</code> data.
116+
*
117+
* @param nullValue The nullValue to use. By default, the table will use an empty string (<code>""</code>).
118+
* @return This ASCIITable instance.
119+
*/
120+
public ASCIITable withNullValue(String nullValue) {
121+
this.nullValue = nullValue;
122+
return this;
123+
}
124+
113125
@Override
114126
public String toString() {
115127
StringBuilder output = new StringBuilder();
@@ -190,7 +202,7 @@ private void appendRow(StringBuilder output, String[] data) {
190202
int rowHeight = 0;
191203
for (int column = 0; column < columnsCount; column++) {
192204
// The height of this cell.
193-
int cellHeight = data[column].split("\\n").length;
205+
int cellHeight = nullSafeData(data[column]).split("\\n").length;
194206
// Choose the greatest.
195207
rowHeight = Math.max(rowHeight, cellHeight);
196208
}
@@ -206,7 +218,7 @@ private void appendRow(StringBuilder output, String[] data) {
206218
output.append(tableFormat.getVerticalBorderFill(column == 0));
207219

208220
// Split the data on its newlines to determine the contents of each line in the column.
209-
String[] cellLines = data[column].split("\\n");
221+
String[] cellLines = nullSafeData(data[column]).split("\\n");
210222

211223
// Decide what to put into this column. Use empty data if there is no specific data for this column.
212224
String cellLine = line < cellLines.length ? cellLines[line] : "";
@@ -220,6 +232,10 @@ private void appendRow(StringBuilder output, String[] data) {
220232
}
221233
}
222234

235+
private String nullSafeData(String data) {
236+
return data == null ? nullValue : data;
237+
}
238+
223239
/**
224240
* Appends a horizontal divider to the output using the given characters.
225241
* <p>

src/test/java/com/mitchtalmadge/asciidata/table/ASCIITableTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,56 @@ public void testNestedTables() throws IOException {
154154

155155
}
156156

157+
158+
/**
159+
* Tests a table with custom null data.
160+
*/
161+
@Test
162+
public void testNullDataTablesWithCustomNullValue() throws IOException {
163+
164+
String[] headers = new String[]{"ID", "Name", "Email"};
165+
String[][] data = new String[][]{
166+
{"123", "Alfred Alan", "aalan@gmail.com"},
167+
{"223", null, "asmart@gmail.com"},
168+
{"256", "Ben Bessel", "benb@outlook.com"},
169+
{"374", "John Roberts", "johnrob@company.com"},
170+
};
171+
172+
assertEquals(
173+
TestUtils.commonizeLineEndings(TestUtils.readFileToString("tables/utf8/nullDataTable.txt")),
174+
TestUtils.commonizeLineEndings(ASCIITable.fromData(headers, data).toString())
175+
);
176+
// ASCII Table Format
177+
assertEquals(
178+
TestUtils.commonizeLineEndings(TestUtils.readFileToString("tables/ascii/customNullDataTable.txt")),
179+
TestUtils.commonizeLineEndings(ASCIITable.fromData(headers, data).withNullValue("n/a").withTableFormat(new ASCIITableFormat()).toString())
180+
);
181+
182+
}
183+
184+
/**
185+
* Tests a table with default null data.
186+
*/
187+
@Test
188+
public void testNullDataTablesWithDefaultNullValue() throws IOException {
189+
190+
String[] headers = new String[]{"ID", "Name", "Email"};
191+
String[][] data = new String[][]{
192+
{"123", "Alfred Alan", "aalan@gmail.com"},
193+
{"223", null, "asmart@gmail.com"},
194+
{"256", "Ben Bessel", "benb@outlook.com"},
195+
{"374", "John Roberts", "johnrob@company.com"},
196+
};
197+
198+
assertEquals(
199+
TestUtils.commonizeLineEndings(TestUtils.readFileToString("tables/utf8/nullDataTable.txt")),
200+
TestUtils.commonizeLineEndings(ASCIITable.fromData(headers, data).toString())
201+
);
202+
// ASCII Table Format
203+
assertEquals(
204+
TestUtils.commonizeLineEndings(TestUtils.readFileToString("tables/ascii/customNullDataTable.txt")),
205+
TestUtils.commonizeLineEndings(ASCIITable.fromData(headers, data).withNullValue("n/a").withTableFormat(new ASCIITableFormat()).toString())
206+
);
207+
208+
}
157209
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
+=====+==============+=====================+
2+
| ID | Name | Email |
3+
|=====|==============|=====================|
4+
| 123 | Alfred Alan | aalan@gmail.com |
5+
|-----|--------------|---------------------|
6+
| 223 | n/a | asmart@gmail.com |
7+
|-----|--------------|---------------------|
8+
| 256 | Ben Bessel | benb@outlook.com |
9+
|-----|--------------|---------------------|
10+
| 374 | John Roberts | johnrob@company.com |
11+
+=====+==============+=====================+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
+=====+==============+=====================+
2+
| ID | Name | Email |
3+
|=====|==============|=====================|
4+
| 123 | Alfred Alan | aalan@gmail.com |
5+
|-----|--------------|---------------------|
6+
| 223 | | asmart@gmail.com |
7+
|-----|--------------|---------------------|
8+
| 256 | Ben Bessel | benb@outlook.com |
9+
|-----|--------------|---------------------|
10+
| 374 | John Roberts | johnrob@company.com |
11+
+=====+==============+=====================+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
╔═════╤══════════════╤═════════════════════╗
2+
║ ID │ Name │ Email ║
3+
╠═════╪══════════════╪═════════════════════╣
4+
║ 123 │ Alfred Alan │ aalan@gmail.com ║
5+
╟─────┼──────────────┼─────────────────────╢
6+
║ 223 │ n/a │ asmart@gmail.com ║
7+
╟─────┼──────────────┼─────────────────────╢
8+
║ 256 │ Ben Bessel │ benb@outlook.com ║
9+
╟─────┼──────────────┼─────────────────────╢
10+
║ 374 │ John Roberts │ johnrob@company.com ║
11+
╚═════╧══════════════╧═════════════════════╝
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
╔═════╤══════════════╤═════════════════════╗
2+
║ ID │ Name │ Email ║
3+
╠═════╪══════════════╪═════════════════════╣
4+
║ 123 │ Alfred Alan │ aalan@gmail.com ║
5+
╟─────┼──────────────┼─────────────────────╢
6+
║ 223 │ │ asmart@gmail.com ║
7+
╟─────┼──────────────┼─────────────────────╢
8+
║ 256 │ Ben Bessel │ benb@outlook.com ║
9+
╟─────┼──────────────┼─────────────────────╢
10+
║ 374 │ John Roberts │ johnrob@company.com ║
11+
╚═════╧══════════════╧═════════════════════╝

0 commit comments

Comments
 (0)