Package org.apache.sis.io
Class TableAppender
Object
TableAppender
- All Implemented Interfaces:
Flushable,Appendable
An
Appendable which formats the text as a table suitable for displaying in devices using
a monospaced font. Columns are separated by tabulations ('\t') and rows are separated by
line or paragraph separators.
The content of every table cells are stored in memory until the flush() method is invoked.
When invoked, flush() copies the cell contents to the underlying stream
or buffer while replacing tabulations by some amount of spaces and drawing borders.
The exact number of spaces is computed from the cell widths.
For example, the following code:
TableAppender table = new TableAppender(System.out);
table.nextLine('═');
table.append("English\tFrench\tr.e.d.\n");
table.nextLine('-');
table.append("Mercury\tMercure\t0.382\n")
.append("Venus\tVénus\t0.949\n")
.append("Earth\tTerre\t1.00\n")
.append("Mars\tMars\t0.532\n");
table.nextLine('═');
table.flush();
produces the following output:
╔═════════╤═════════╤════════╗ ║ English │ French │ r.e.d. ║ ╟─────────┼─────────┼────────╢ ║ Mercury │ Mercure │ 0.382 ║ ║ Venus │ Vénus │ 0.949 ║ ║ Earth │ Terre │ 1.00 ║ ║ Mars │ Mars │ 0.532 ║ ╚═════════╧═════════╧════════╝
- Since:
- 0.3
- See Also:
Defined in the sis-utility module
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteA possible value for cell alignment.static final byteA possible value for cell alignment.static final byteA possible value for cell alignment.protected final AppendableThe underlying character output stream or buffer. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new table formatter writing in an internal buffer with a default column separator.TableAppender(Appendable out) Creates a new table formatter writing in the given output with a default column separator.TableAppender(Appendable out, String separator) Creates a new table formatter writing in the given output with the specified column separator.TableAppender(Appendable out, String leftBorder, String separator, String rightBorder) Creates a new table formatter writing in the given output with the specified column separator and border.TableAppender(String separator) Creates a new table formatter writing in an internal buffer with the specified column separator. -
Method Summary
Modifier and TypeMethodDescriptionappend(char c) Writes a single character.append(CharSequence sequence) Appends the specified character sequence.append(CharSequence sequence, int start, int end) Writes a portion of a character sequence.voidWrites an horizontal separator using the'─'character.voidflush()Flushes the table content to the underlying stream or buffer.byteReturns the alignment of the text inside the current cell.intReturns the number of columns in this table.Returns the line separator between table rows.intReturns the number of rows in this table.booleanReturnstrueif EOL characters are used for line feeds inside current cells.voidMoves one column to the right.voidnextColumn(char fill) Moves one column to the right, filling remaining space with the given character.voidMoves to the first column on the next row.voidnextLine(char fill) Moves to the first column on the next row, filling every remaining cell in the current row with the specified character.voidsetCellAlignment(byte alignment) Sets the alignment of the text inside the current cell.voidsetMultiLinesCells(boolean multiLines) Sets the desired behavior for EOL and tabulations characters.Returns the content of thisTableAppenderas a string if possible.
-
Field Details
-
ALIGN_LEFT
public static final byte ALIGN_LEFTA possible value for cell alignment. This specifies that the text is aligned to the left indent and extra whitespace should be placed on the right.- See Also:
-
ALIGN_CENTER
public static final byte ALIGN_CENTERA possible value for cell alignment. This specifies that the text is aligned to the center and extra whitespace should be placed equally on the left and right.- See Also:
-
ALIGN_RIGHT
public static final byte ALIGN_RIGHTA possible value for cell alignment. This specifies that the text is aligned to the right indent and extra whitespace should be placed on the left.- See Also:
-
out
The underlying character output stream or buffer.
-
-
Constructor Details
-
TableAppender
public TableAppender()Creates a new table formatter writing in an internal buffer with a default column separator. The default is a vertical double line for the left and right table borders, and a single line between the columns. -
TableAppender
Creates a new table formatter writing in an internal buffer with the specified column separator.- Parameters:
separator- string to write between columns.
-
TableAppender
Creates a new table formatter writing in the given output with a default column separator. The default is a vertical double line for the left and right table borders, and a single line between the columns.- Parameters:
out- the underlying stream or buffer to write to.
-
TableAppender
Creates a new table formatter writing in the given output with the specified column separator.- Parameters:
out- the underlying stream or buffer to write to.separator- string to write between columns.
-
TableAppender
Creates a new table formatter writing in the given output with the specified column separator and border.- Parameters:
out- the underlying stream or buffer to write to.leftBorder- string to write on the left side of the table.separator- string to write between columns.rightBorder- string to write on the right side of the table.- Since:
- 0.8
-
-
Method Details
-
isMultiLinesCells
public boolean isMultiLinesCells()Returnstrueif EOL characters are used for line feeds inside current cells.- Returns:
trueif EOL characters are to be write inside the cell.
-
setMultiLinesCells
public void setMultiLinesCells(boolean multiLines) Sets the desired behavior for EOL and tabulations characters.- If
true, then tabulations, line and paragraph separator characters are copied into the current cell. Subsequent writing operations will continue inside the same cell. - If
false, then tabulations move to next column and EOL move to the first cell of next row (i.e. tabulation and EOL are equivalent tonextColumn()andnextLine()calls respectively).
false.- Parameters:
multiLines-truetrue if EOL are used for line feeds inside current cells, orfalseif EOL move to the next row.
- If
-
getCellAlignment
public byte getCellAlignment()Returns the alignment of the text inside the current cell. The default value isALIGN_LEFT.- Returns:
- current cell alignment as one of the
ALIGN_LEFT,ALIGN_RIGHTorALIGN_CENTERconstants.
-
setCellAlignment
public void setCellAlignment(byte alignment) Sets the alignment of the text inside the current cell. The alignments of any cell written prior this method call are left unchanged. The new alignment will apply to the next cells too until thissetCellAlignment(…)method is invoked again with a different value.If this method is never invoked, then the default alignment is
ALIGN_LEFT.- Parameters:
alignment- the new cell alignment as one of theALIGN_LEFT,ALIGN_RIGHTorALIGN_CENTERconstants.
-
getLineSeparator
Returns the line separator between table rows. This is the first line separator found in the text formatted as a table, or the system default if no line separator was found in the text to format.- Returns:
- the line separator between table rows.
- Since:
- 1.0
-
getRowCount
public int getRowCount()Returns the number of rows in this table. This count is reset to 0 byflush().- Returns:
- the number of rows in this table.
-
getColumnCount
public int getColumnCount()Returns the number of columns in this table.- Returns:
- the number of columns in this table.
-
append
Writes a single character. IfisMultiLinesCells()isfalse(which is the default), then:- Tabulations (
'\t') are replaced by calls tonextColumn(). - line or paragraph separators are replaced by calls to
nextLine().
- Specified by:
appendin interfaceAppendable- Parameters:
c- character to write.- Returns:
this.
- Tabulations (
-
append
Appends the specified character sequence.- Specified by:
appendin interfaceAppendable- Parameters:
sequence- the character sequence to append, ornull.- Returns:
- a reference to this
Appendable.
-
append
Writes a portion of a character sequence. Tabulations and line separators are interpreted as byappend(char).- Specified by:
appendin interfaceAppendable- Parameters:
sequence- the character sequence to be written.start- index from which to start reading characters.end- index of the character following the last character to read.- Returns:
this.
-
appendHorizontalSeparator
public void appendHorizontalSeparator()Writes an horizontal separator using the'─'character.- See Also:
-
nextColumn
public void nextColumn()Moves one column to the right. The subsequent writing operations will occur in a new cell on the same row. -
nextColumn
public void nextColumn(char fill) Moves one column to the right, filling remaining space with the given character. The subsequent writing operations will occur in a new cell on the same row.Calling
nextColumn('*')from the first character in a cell is a convenient way to put a pad value in this cell.- Parameters:
fill- character filling the cell (default to whitespace).
-
nextLine
public void nextLine()Moves to the first column on the next row. The subsequent writing operations will occur on a new row. -
nextLine
public void nextLine(char fill) Moves to the first column on the next row, filling every remaining cell in the current row with the specified character. The subsequent writing operations will occur on a new row.Calling
nextLine('-')ornextLine('═')from the first column of a row is a convenient way to fill this row with a line separator.- Parameters:
fill- character filling the rest of the line (default to whitespace). This character may be use as a row separator.- See Also:
-
flush
Flushes the table content to the underlying stream or buffer. This method should not be called before the table is completed (otherwise, columns may have the wrong width).- Specified by:
flushin interfaceFlushable- Throws:
IOException- if an output operation failed.
-
toString
Returns the content of thisTableAppenderas a string if possible.- If this
TableAppenderhas been created without explicitAppendable, then this method always returns the current table content formatted as a string. - Otherwise, if
outimplementsCharSequenceor is directly or indirectly a wrapper around aCharSequence, returns itstoString()representation. The string will contain this table content only ifflush()has been invoked prior thistoString()method. - Otherwise returns the localized "Unavailable content" string.
- Returns:
- the content of this
Appendable, or a localized message for unavailable content. - See Also:
- If this
-