Class TableBuilder

java.lang.Object
com.jagrosh.jdautilities.commons.utils.TableBuilder

public class TableBuilder extends Object
A utility class that can be used to easily create String tables in Java without any extra frameworks. This can be useful to display table-like structures in Discord codeblocks, for example.

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)
  • Constructor Details

    • TableBuilder

      public TableBuilder()
  • Method Details

    • build

      public String 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
    • addHeaders

      public TableBuilder addHeaders(String... headers)
      Sets the headers for the columns specified in values, 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

      public TableBuilder addRowNames(String... rows)
      Sets names for the rows specified in values, 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

      public TableBuilder setValues(String[][] values)
      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

      public TableBuilder setBorders(TableBuilder.Borders borders)
      Sets the borders for this table using the nested Borders class.
      Parameters:
      borders - An instance of the Borders class that specifies the characters to use.
      Returns:
      This builder.
    • setName

      public TableBuilder setName(String tableName)
      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

      public TableBuilder setAlignment(TableBuilder.Alignment alignment)
      Sets the alignment/orientation of all headers and values. This will be applied if and only if autoAdjust is true.
      By default, this is set to CENTER.
      Parameters:
      alignment - The alignment to set.
      Returns:
      This builder.
      See Also:
    • setPadding

      public TableBuilder setPadding(int padding)
      Sets a paddling that is applied to each value if autoAdjust is true.
      By default, this is 1.
      Parameters:
      padding - The minimum amount of blank spaces between a value and the corresponding borders.
      Returns:
      This builder.
      See Also:
    • codeblock

      public TableBuilder codeblock(boolean codeblock)
      Sets whether the table should be embedded in a markdown code block (no special semantics). By default, this is false.
      Parameters:
      codeblock - true, if the table should be in a markdown code block.
      Returns:
      This builder.
    • frame

      public TableBuilder frame(boolean frame)
      Sets whether the table should be framed. By default, this is false.
      Parameters:
      frame - true, if the table should have outlines and the cells should have borders.
      Returns:
      This builder.
    • autoAdjust

      public TableBuilder autoAdjust(boolean autoAdjust)
      Sets whether the table should be adjusted automatically according to the lengths of the values. By default, this is true.
      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.