Class TableBuilderImpl

  • All Implemented Interfaces:
    org.refcodes.mixin.PrintStreamAccessor, org.refcodes.mixin.PrintStreamAccessor.PrintStreamBuilder<TableBuilder>, org.refcodes.mixin.PrintStreamAccessor.PrintStreamMutator, org.refcodes.mixin.PrintStreamAccessor.PrintStreamProperty, org.refcodes.mixin.RowWidthAccessor, org.refcodes.mixin.RowWidthAccessor.RowWidthBuilder<TableBuilder>, TableBuilder, TablePrinter


    public class TableBuilderImpl
    extends java.lang.Object
    implements TableBuilder
    An implementation of the TableBuilder interface.
    • Constructor Detail

      • TableBuilderImpl

        public TableBuilderImpl​()
        Instantiates a new table builder impl.
      • TableBuilderImpl

        public TableBuilderImpl​(int aRowWidth)
        Initializes the TableBuilder with the given row width.
        Parameters:
        aRowWidth - The row width with which to initialize the TableBuilder.
    • Method Detail

      • getRowWidth

        public int getRowWidth​()
        Specified by:
        getRowWidth in interface org.refcodes.mixin.RowWidthAccessor
      • setTableStatus

        public TableBuilder setTableStatus​(TableStatus aTableStatus)
        Set the TablePrinter (TableBuilder) status. The TableStatus is required by a TablePrinter ( TableBuilder) to determine whether to do additional table decoration for a print operation or to fail that operation as of an illegal state exception. As far as possible an illegal state exception is to be tried to be prevented by decorating the table automatically by an operation as of the current TableStatus. Setting the status manually, you can use differently configured TablePrinter interfaces to print their lines according to the status of a previous TablePrinter.
        Specified by:
        setTableStatus in interface TablePrinter
        Parameters:
        aTableStatus - The table's current TableStatus to be set.
        Returns:
        the table printer
      • withLeftBorder

        public TableBuilder withLeftBorder​(boolean hasLeftBorder)
        Sets whether to print the left border and returns this TableBuilder instance as of the Builder-Pattern.
        Specified by:
        withLeftBorder in interface TableBuilder
        Parameters:
        hasLeftBorder - True in case a left border is to be printed, elser false
        Returns:
        This TableBuilder instance to continue configuration.
      • withRightBorder

        public TableBuilder withRightBorder​(boolean hasRightBorder)
        Sets whether to print the right border and returns this TableBuilder instance as of the Builder-Pattern.
        Specified by:
        withRightBorder in interface TableBuilder
        Parameters:
        hasRightBorder - True in case a right border is to be printed, elser false
        Returns:
        This TableBuilder instance to continue configuration.
      • withRowWidth

        public TableBuilder withRowWidth​(int aRowWidth)
        In case a value of -1 is provided, then the width is calculated dynamically from the individual columns' widths added together including any borders being specified.
        Specified by:
        withRowWidth in interface org.refcodes.mixin.RowWidthAccessor.RowWidthBuilder<TableBuilder>
      • withLineBreak

        public TableBuilder withLineBreak​(java.lang.String aLineBreak)
        Sets the required line break and returns this TableBuilder instance as of the Builder-Pattern.
        Specified by:
        withLineBreak in interface TableBuilder
        Parameters:
        aLineBreak - The line break character to use, by default the platform specific one is used.
        Returns:
        This TableBuilder instance to continue configuration.
      • getPrintStream

        public java.io.PrintStream getPrintStream​()
        Specified by:
        getPrintStream in interface org.refcodes.mixin.PrintStreamAccessor
      • setPrintStream

        public void setPrintStream​(java.io.PrintStream aPrintStream)
        Specified by:
        setPrintStream in interface org.refcodes.mixin.PrintStreamAccessor.PrintStreamMutator
      • withEscapeCodes

        public TableBuilder withEscapeCodes​(boolean isEscCodesEnabled)
        Sets an overall ANSI Escape-Code support for the TableBuilder, for the rows as well as the borders; and returns this TableBuilder instance as of the Builder-Pattern. In case of being set to Boolean.FALSE, then an ANSI support is disabled altogether, else it is enabled (again, if already configured). The latest setting wins.
        Specified by:
        withEscapeCodes in interface TableBuilder
        Parameters:
        isEscCodesEnabled - True to enable, false to disable Escape-Code support altogether.
        Returns:
        This TableBuilder instance to continue configuration.
      • withEscapeCode

        public TableBuilder withEscapeCode​(java.lang.String aAnsiEscCode)
        Sets an overall ANSI Escape-Code for the header, the rows as well as the borders; and returns this TableBuilder instance as of the builder pattern. In case an ANSI Escape-Codes is set, then an ANSI Reset-Code is prepended to the according text being printed. The latest ANSI escape code set wins.
        Specified by:
        withEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withBorderEscapeCode

        public TableBuilder withBorderEscapeCode​(java.lang.String aAnsiEscCode)
        Sets an overall ANSI Escape-Code for the borders and returns this TableBuilder instance as of the Builder-Pattern. In case an ANSI Escape-Codes is set, then the ANSI Escape-Code is prepended and an ANSI Reset-Code is appended to the according text being printed. The latest ANSI Escape-Code set wins.
        Specified by:
        withBorderEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withTextEscapeCode

        public TableBuilder withTextEscapeCode​(java.lang.String aAnsiEscCode)
        Sets an overall ANSI Escape-Code for the header and the rows and returns this TableBuilder instance as of the Builder-Pattern. In case an ANSI Escape-Codes is set, then the ANSI Escape-Code is prepended and an ANSI Reset-Code is appended to the according text being printed. The latest ANSI Escape-Code set wins.
        Specified by:
        withTextEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withHeaderEscapeCode

        public TableBuilder withHeaderEscapeCode​(java.lang.String aAnsiEscCode)
        Sets an overall ANSI Escape-Code for the header and returns this TableBuilder instance as of the Builder-Pattern. The latest ANSI Escape-Code being set wins.
        Specified by:
        withHeaderEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withRowEscapeCode

        public TableBuilder withRowEscapeCode​(java.lang.String aAnsiEscCode)
        Sets an overall ANSI Escape-Code for the rows and returns this TableBuilder instance as of the Builder-Pattern. The latest ANSI Escape-Code being set wins.
        Specified by:
        withRowEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withTextColumnEscapeCode

        public TableBuilder withTextColumnEscapeCode​(java.lang.String aAnsiEscCode)
        Sets the column's ANSI Escape-Code for the current (header and row) column and returns this TableBuilder instance as of the builder pattern. The latest ANSI Escape-Code being set wins.
        Specified by:
        withTextColumnEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withHeaderColumnEscapeCode

        public TableBuilder withHeaderColumnEscapeCode​(java.lang.String aAnsiEscCode)
        Sets the column's header ANSI Escape-Code for the current header column and returns this TableBuilder instance as of the Builder-Pattern. The latest ANSI Escape-Code being set wins.
        Specified by:
        withHeaderColumnEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withRowColumnEscapeCode

        public TableBuilder withRowColumnEscapeCode​(java.lang.String aAnsiEscCode)
        Sets the column's header ANSI Escape-Code for the current row column and returns this TableBuilder instance as of the Builder-Pattern. The latest ANSI Escape-Code being set wins.
        Specified by:
        withRowColumnEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withResetEscapeCode

        public TableBuilder withResetEscapeCode​(java.lang.String aAnsiEscCode)
        Sets the ANSI reset Escape-Code for this TableBuilder instance as of the Builder-Pattern. In case an ANSI Escape-Codes is set, then this ANSI Reset-Code is prepended to the according text being printed.
        Specified by:
        withResetEscapeCode in interface TableBuilder
        Parameters:
        aAnsiEscCode - The String to be used for resetting ANSI escaping.
        Returns:
        This TableBuilder instance to continue configuration.
      • withSplitTextMode

        public TableBuilder withSplitTextMode​(SplitTextMode aLineSplitMode)
        Sets an overall line split mode for the header and the rows and returns this TableBuilder instance as of the Builder-Pattern. The latest line split mode being set wins.
        Specified by:
        withSplitTextMode in interface TableBuilder
        Parameters:
        aLineSplitMode - The SplitTextMode to be used for aligning the text.
        Returns:
        This TableBuilder instance to continue configuration.
      • withColumnSplitTextMode

        public TableBuilder withColumnSplitTextMode​(SplitTextMode aLineSplitMode)
        Sets the column's line split mode for the header and the rows and returns this TableBuilder instance as of the Builder-Pattern. The latest line split mode being set wins.
        Specified by:
        withColumnSplitTextMode in interface TableBuilder
        Parameters:
        aLineSplitMode - The SplitTextMode to be used for aligning the text.
        Returns:
        This TableBuilder instance to continue configuration.
      • withRowColumnSplitTextMode

        public TableBuilder withRowColumnSplitTextMode​(SplitTextMode aLineSplitMode)
        Sets the column's header line split mode for the header and the rows and returns this TableBuilder instance as of the Builder-Pattern. The latest line split mode being set wins.
        Specified by:
        withRowColumnSplitTextMode in interface TableBuilder
        Parameters:
        aLineSplitMode - The SplitTextMode to be used for aligning the text.
        Returns:
        This TableBuilder instance to continue configuration.
      • withMoreTextMode

        public TableBuilder withMoreTextMode​(MoreTextMode aMoreTextMode)
        Sets an overall "more" mode for the header and the rows and returns this TableBuilder instance as of the Builder-Pattern. The latest "more" mode being set wins.
        Specified by:
        withMoreTextMode in interface TableBuilder
        Parameters:
        aMoreTextMode - The MoreTextMode to be used for aligning the text.
        Returns:
        This TableBuilder instance to continue configuration.
      • withColumnMoreTextMode

        public TableBuilder withColumnMoreTextMode​(MoreTextMode aMoreTextMode)
        Sets the column's "more" mode for the header and the rows and returns this TableBuilder instance as of the Builder-Pattern. The latest "more" mode being set wins.
        Specified by:
        withColumnMoreTextMode in interface TableBuilder
        Parameters:
        aMoreTextMode - The MoreTextMode to be used for aligning the text.
        Returns:
        This TableBuilder instance to continue configuration.
      • withRowColumnMoreTextMode

        public TableBuilder withRowColumnMoreTextMode​(MoreTextMode aMoreTextMode)
        Sets the column's header "more" mode for the header and the rows and returns this TableBuilder instance as of the Builder-Pattern. The latest "more" mode being set wins.
        Specified by:
        withRowColumnMoreTextMode in interface TableBuilder
        Parameters:
        aMoreTextMode - The MoreTextMode to be used for aligning the text.
        Returns:
        This TableBuilder instance to continue configuration.
      • withColumnWidth

        public TableBuilder withColumnWidth​(int aColumnWidth)
                                     throws java.lang.IllegalStateException
        Sets the current column's (the last added one with the TableBuilder.addColumn() method) absolute width () @link ColumnWidthType#ABSOLUTE) and returns this TableBuilder instance as of the Builder-Pattern. Same as calling "withColumnWidth(int, ColumnWidthType.ABSOLUTE)}"
        Specified by:
        withColumnWidth in interface TableBuilder
        Parameters:
        aColumnWidth - The width for the column.
        Returns:
        This TableBuilder instance to continue configuration.
        Throws:
        java.lang.IllegalStateException - in case there is none column already added.
      • toHeaderContinue

        public java.lang.String toHeaderContinue​(java.lang.String... aColumns)
        Continues a begun header, for convenience reasons, this is being encapsulated by the TablePrinter.toHeader(String...) method. Use this method in case you need more control on the header construction than TablePrinter.toHeader(String...) can provide you.
        Specified by:
        toHeaderContinue in interface TablePrinter
        Parameters:
        aColumns - the columns
        Returns:
        The begun header continued String including the line breaks.
      • toHeaderEnd

        public java.lang.String toHeaderEnd​(TableBuilder aTablePrinter)
        Ends the headers of the provided TableBuilder for this table printer to continue; different header widths and column widths are taken care of, so it is a pleasure to mix different TableBuilders, especially when empty columns may result in better using another column layout making better use of the available width.
        Specified by:
        toHeaderEnd in interface TablePrinter
        Parameters:
        aTablePrinter - The TableBuilder to which to append this TableBuilder's headers.
        Returns:
        The divider header joining the provided (top) TableBuilder's layout with this (bottom) TableBuilder's layout.
      • toRowContinue

        public java.lang.String toRowContinue​(java.lang.String... aColumns)
        Continues a begun row, for convenience reasons, this is being encapsulated by the TablePrinter.toRow(String...) method. Use this method in case you need more control on the header construction than TablePrinter.toRow(String...) can provide you.
        Specified by:
        toRowContinue in interface TablePrinter
        Parameters:
        aColumns - the columns
        Returns:
        The begun row continued String including the line breaks.
      • toRowEnd

        public java.lang.String toRowEnd​(TableBuilder aTablePrinter)
        Ends the rows of the provided TableBuilder for this table printer to continue; different row widths and column widths are taken care of, so it is a pleasure to mix different TableBuilders, especially when empty columns may result in better using another column layout making better use of the available width.
        Specified by:
        toRowEnd in interface TablePrinter
        Parameters:
        aTablePrinter - The TableBuilder to which to append this TableBuilder's rows.
        Returns:
        The divider row joining the provided (top) TableBuilder's layout with this (bottom) TableBuilder's layout.
      • toHeader

        public java.lang.String toHeader​(java.lang.String... aColumns)
        Prints the table's header with the content of the provided columns.
        Specified by:
        toHeader in interface TablePrinter
        Parameters:
        aColumns - The columns to be used in the table's header.
        Returns:
        The header String including line breaks as it most probably will consist of more than one line.
      • toRow

        public java.lang.String toRow​(java.lang.String... aColumns)
        Prints the table's (next) row with the content of the provided columns. Call this method for each row to print out.
        Specified by:
        toRow in interface TablePrinter
        Parameters:
        aColumns - The columns to be used in the (next) table's row.
        Returns:
        The row String including line breaks as it most probably will consist of more than one line.
      • toTail

        public java.lang.String toTail​()
        Finishes off the table by closing it.
        Specified by:
        toTail in interface TablePrinter
        Returns:
        The tail of the table for finishing the table off.
      • toColumnWidths

        public int[] toColumnWidths​()
        Returns the currently calculated or configured column widths. Changing the row width or adding a column will affect the result being returned.
        Specified by:
        toColumnWidths in interface TableBuilder
        Returns:
        The widths of the rows as currently calculated.
      • toColumns

        public static java.lang.String[][] toColumns​(java.lang.String[][] aColumns,
                                                     VertAlignTextMode aTextBlockMode)
        Creates an array of String arrays where all the arrays are of the same length and where the arrays are all aligned as specified by the passed VertAlignTextMode.
        Parameters:
        aColumns - An array of String arrays to be equalized.
        aTextBlockMode - The VertAlignTextMode to be used when equalizing the columns.
        Returns:
        The equalized columns.
      • toColumnWidths

        protected static int[] toColumnWidths​(int aTotalWidth,
                                              ColumnWidthMetrics... aColumnWidths)
        Calculates the columns widths in "number of chars" according to the provided ColumnWidthMetricsImpl instances in relation to the provided total (available) width; the provided column widths can be a mix of "number of char" widths (ColumnWidthType.ABSOLUTE) and percent (%) widths (ColumnWidthType.RELATIVE). When the width is being relative, then the total sum of all of relative widths represents 100%. The resulting column widths add up to the total width. The column widths for the ColumnWidthMetricsImpl instances being specified in percent (ColumnWidthType.RELATIVE) are calculated from the remainder width after all "number of char" ColumnWidthMetricsImpl instances ( ColumnWidthType.ABSOLUTE) have been subtracted from the provided total width. *
        Parameters:
        aTotalWidth - The total available width in number of chars.
        aColumnWidths - The ColumnWidthMetricsImpl instances providing width specifications either in percent (%) or in "number of chars".
        Returns:
        An array where each element represents one of the provided ColumnWidthMetricsImpl instances in the order them ColumnWidthMetricsImpl instances have been passed; the array species the actual column widths in "number of chars", adding up to the total width.