public class TableAppender extends Object implements Flushable
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 ║ ╚═════════╧═════════╧════════╝
TreeTableFormat
Defined in the sis-utility
module
Modifier and Type | Field and Description |
---|---|
static byte |
ALIGN_CENTER
A possible value for cell alignment.
|
static byte |
ALIGN_LEFT
A possible value for cell alignment.
|
static byte |
ALIGN_RIGHT
A possible value for cell alignment.
|
protected Appendable |
out
The underlying character output stream or buffer.
|
Constructor and Description |
---|
TableAppender()
Creates 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.
|
Modifier and Type | Method and Description |
---|---|
TableAppender |
append(char c)
Writes a single character.
|
TableAppender |
append(CharSequence sequence)
Appends the specified character sequence.
|
TableAppender |
append(CharSequence sequence,
int start,
int end)
Writes a portion of a character sequence.
|
void |
appendHorizontalSeparator()
Writes an horizontal separator using the
'─' character. |
void |
flush()
Flushes the table content to the underlying stream or buffer.
|
byte |
getCellAlignment()
Returns the alignment of the text inside the current cell.
|
int |
getColumnCount()
Returns the number of columns in this table.
|
int |
getRowCount()
Returns the number of rows in this table.
|
boolean |
isMultiLinesCells()
Returns
true if EOL characters are used for line feeds inside current cells. |
void |
nextColumn()
Moves one column to the right.
|
void |
nextColumn(char fill)
Moves one column to the right, filling remaining space with the given character.
|
void |
nextLine()
Moves to the first column on the next row.
|
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.
|
void |
setCellAlignment(byte alignment)
Sets the alignment of the text inside the current cell.
|
void |
setMultiLinesCells(boolean multiLines)
Sets the desired behavior for EOL and tabulations characters.
|
String |
toString()
Returns the content of this
TableAppender as a string if possible. |
public static final byte ALIGN_LEFT
public static final byte ALIGN_CENTER
public static final byte ALIGN_RIGHT
protected final Appendable out
public TableAppender()
public TableAppender(String separator)
separator
- string to write between columns.public TableAppender(Appendable out)
out
- the underlying stream or buffer to write to.public TableAppender(Appendable out, String separator)
out
- the underlying stream or buffer to write to.separator
- string to write between columns.public TableAppender(Appendable out, String leftBorder, String separator, String rightBorder)
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.public boolean isMultiLinesCells()
true
if EOL characters are used for line feeds inside current cells.true
if EOL characters are to be write inside the cell.public void setMultiLinesCells(boolean multiLines)
true
, then tabulations,
line and paragraph separator characters are copied into the current cell.
Subsequent writing operations will continue inside the same cell.false
, then tabulations move to next column and EOL move
to the first cell of next row (i.e. tabulation and EOL are equivalent to
nextColumn()
and nextLine()
calls respectively).false
.multiLines
- true
true if EOL are used for line feeds inside current cells,
or false
if EOL move to the next row.public byte getCellAlignment()
ALIGN_LEFT
.ALIGN_LEFT
,
ALIGN_RIGHT
or ALIGN_CENTER
constants.public void setCellAlignment(byte alignment)
setCellAlignment(…)
method is invoked again
with a different value.
If this method is never invoked, then the default alignment is ALIGN_LEFT
.
alignment
- the new cell alignment as one of the ALIGN_LEFT
,
ALIGN_RIGHT
or ALIGN_CENTER
constants.public int getRowCount()
flush()
.public int getColumnCount()
public TableAppender append(char c)
isMultiLinesCells()
is false
(which is the default), then:
'\t'
) are replaced by calls to nextColumn()
.nextLine()
.append
in interface Appendable
c
- character to write.this
.public TableAppender append(CharSequence sequence)
append
in interface Appendable
sequence
- the character sequence to append, or null
.Appendable
.public TableAppender append(CharSequence sequence, int start, int end)
append(char)
.append
in interface Appendable
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.this
.public void appendHorizontalSeparator()
'─'
character.nextLine(char)
public void nextColumn()
public void nextColumn(char fill)
Calling nextColumn('*')
from the first character
in a cell is a convenient way to put a pad value in this cell.
fill
- character filling the cell (default to whitespace).public void nextLine()
public void nextLine(char fill)
Calling nextLine('-')
or nextLine('═')
from the first column of a row
is a convenient way to fill this row with a line separator.
fill
- character filling the rest of the line (default to whitespace).
This character may be use as a row separator.appendHorizontalSeparator()
public void flush() throws IOException
flush
in interface Flushable
IOException
- if an output operation failed.public String toString()
TableAppender
as a string if possible.
TableAppender
has been created without explicit Appendable
,
then this method always returns the current table content formatted as a string.out
implements CharSequence
or is directly or
indirectly a wrapper around a CharSequence
, returns its toString()
representation. The string will contain this table content only if flush()
has been invoked prior this toString()
method.Appendable
, or a localized message for unavailable content.IO.content(Appendable)
Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.