Class TableBuilder
If framing is activated, the tables usually look like this (but with box drawing characters):
------------------------------------
|Table Name| Header[0] | Header[1] |
-----------+-----------+------------
| Row[0] |Value[0][0]|Value[0][1]|
-----------+-----------+------------
| Row[1] |Value[1][0]|Value[1][1]|
------------------------------------
Example to get a table like above:
String table = new TableBuilder()
.setAlignment(TableBuilder.CENTER) // setting center alignment (already set by default)
.addHeaders("Header[0]", "Header[1]") // setting headers
.setValues(new String[][] { // setting values as 2d array
{"Value[0][0]", "Value[0][1]"},
{"Value[1][0]", "Value[1][1]"}
}).addRowNames("Row[0]", "Row[1]") // setting row names
.setName("Table Name") // the name (displayed in the top left corner)
.frame(true) // activating framing
.build(); // building (note that this consists of unicode box drawing characters, so it might not display correctly on some devices)
// now do whatever you want with that
The characters used to display the table can be configured individually.
- Author:
- Johnny_JayJay (https://www.github.com/JohnnyJayJay)
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
An enum that represents the alignments possible to setstatic class
A data class whose instances store the characters needed to create a table with the enclosingTableBuilder
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddHeaders
(String... headers) Sets the headers for the columns specified invalues
, applied in the order given here.addRowNames
(String... rows) Sets names for the rows specified invalues
, applied in the order given here.autoAdjust
(boolean autoAdjust) Sets whether the table should be adjusted automatically according to the lengths of the values.build()
Builds a String table according to the settings made.codeblock
(boolean codeblock) Sets whether the table should be embedded in a markdown code block (no special semantics).frame
(boolean frame) Sets whether the table should be framed.setAlignment
(TableBuilder.Alignment alignment) Sets the alignment/orientation of all headers and values.setBorders
(TableBuilder.Borders borders) Sets the borders for this table using the nestedBorders
class.Sets the name of the table.setPadding
(int padding) Sets a paddling that is applied to each value if autoAdjust istrue
.Sets the values of this table.
-
Constructor Details
-
TableBuilder
public TableBuilder()
-
-
Method Details
-
build
Builds a String table according to the settings made.- Returns:
- The table as a String.
If framing is activated, there will be a frame around the whole table.
If codeblock is activated, there will be a
```\n
at the beginning and a```
at the end. - Throws:
IllegalArgumentException
- if:- No
Borders
were set - No values were set
- The values are empty
- One or more of the values are null
- The padding is < 0
- Row names were set and there are less rows in the values than row names given
- The amount of columns is not consistent throughout headers (if present) and values
- autoAdjust is false and no headers were set
- autoAdjust is false and any value is longer than the corresponding header
- No
-
addHeaders
Sets the headers for the columns specified invalues
, applied in the order given here.This setting is optional. By default, there will not be any headers.
- Parameters:
headers
- The headers as varargs, so either a String[] or single Strings.- Returns:
- this
-
addRowNames
Sets names for the rows specified invalues
, applied in the order given here.This setting is optional. By default, there will not be any row names.
- Parameters:
rows
- The row names as varargs, so either a String[] or single Strings.- Returns:
- This builder.
- See Also:
-
setValues
Sets the values of this table.- Parameters:
values
- The values as a 2D-Array. The arrays inside of that array each represent a row. Each value inside a row will be placed in the table according to its index, which describes the Y-position.- Returns:
- This builder.
-
setBorders
Sets the borders for this table using the nestedBorders
class.- Parameters:
borders
- An instance of the Borders class that specifies the characters to use.- Returns:
- This builder.
-
setName
Sets the name of the table. This will be displayed in the upper left corner cell if row names were set.- Parameters:
tableName
- The name of the table as a String. Default: empty String- Returns:
- This builder.
- See Also:
-
setAlignment
Sets the alignment/orientation of all headers and values. This will be applied if and only if autoAdjust istrue
.
By default, this is set toCENTER
.- Parameters:
alignment
- The alignment to set.- Returns:
- This builder.
- See Also:
-
setPadding
Sets a paddling that is applied to each value if autoAdjust istrue
.
By default, this is1
.- Parameters:
padding
- The minimum amount of blank spaces between a value and the corresponding borders.- Returns:
- This builder.
- See Also:
-
codeblock
Sets whether the table should be embedded in a markdown code block (no special semantics). By default, this isfalse
.- Parameters:
codeblock
-true
, if the table should be in a markdown code block.- Returns:
- This builder.
-
frame
Sets whether the table should be framed. By default, this isfalse
.- Parameters:
frame
-true
, if the table should have outlines and the cells should have borders.- Returns:
- This builder.
-
autoAdjust
Sets whether the table should be adjusted automatically according to the lengths of the values. By default, this istrue
.- Parameters:
autoAdjust
-true
, if the table should evaluate the space needed automatically.false
, if you want every value to have max. the same length as the corresponding header and it should be fixed. This also deactivates the possibility to use an alignment or a padding.- Returns:
- This builder.
-