/** * Returns the row's total width. * * @return the width of the row. */ public int getRowWidth() { int overallWidth = 0; for (int i = 0; i < data.length; i++) { overallWidth += getTable().getColumnWidth(i); } return overallWidth; }
/** * Adds a header. * * @param title * the text to display within the header */ public void addHeader(final String title) { addHeader(title, '=', Alignment.Left); }
/** * Returns the total width of the table. * * @return int the width of the table */ protected int getTotalWidth() { return this.getRowWidth() + 3 * numColumns() + 1; }
/** * {@inheritDoc} */ @Override public String draw() { final String[] row = new String[data.length]; for (int i = 0; i < data.length; i++) { row[i] = Util.pad(data[i], ' ', getTable().getColumnWidth(i), getTable().getOrientation(i)); } return Util.combine(new String(new char[] { AbstractTabularComponent.BORDER }), SPACE, Util.implode(Util.combine(SPACE, new String(new char[] { AbstractTabularComponent.BORDER }), SPACE), row), SPACE, new String(new char[] { AbstractTabularComponent.BORDER }), NEWLINE); }
/** * Alignement test if some rows are incomplete */ @Test public void testRowAlignment() { final NiceTable zero = new NiceTable(2); zero.addRow(new String[] { "a\nb\nc", "a\nb" }); zero.addRow(new String[] { "d", "d" }); final String expected = "| a | a |\n" + "| b | b |\n" + "| c | |\n" + "| d | d |\n"; assertEquals("Test for row alignment", expected, zero.toString()); }
/** * Adds a string row. checks that the strings added do not contain newlines, * because if so, it has to split them in order to make them fit the row. * * @param data * the array of data. */ public void addRow(final String[] data) { if (anyStringContainsNewLine(data)) { final String[][] theMatrix = Util.createMatrix(data); for (int i = 0; i < theMatrix.length; i++) { addRow(theMatrix[i]); } } else { final Row myRow = new Row(this, data); rows.add(myRow); } }
/** * Generating header for a given table. * * @param table * the table where the header should fit to * @return another {@link NiceTable} instance */ private NiceTable generateHeader(final NiceTable table) { table.addHeader("Benchmark"); table.addRow(new String[] { "-", "unit", "sum", "min", "max", "avg", "stddev", "conf95", "runs" }); return table; }
/** * {@inheritDoc} */ @Override public String draw() { return Util.combine(new String(new char[] { AbstractTabularComponent.BORDER }), Util.pad(title, enclosing, getTable().getTotalWidth() - 2, orientation), new String(new char[] { AbstractTabularComponent.BORDER }), NEWLINE); }
/** * {@inheritDoc} */ @Override public String draw() { return Util.combine(new String(new char[] { AbstractTabularComponent.BORDER }), Util.repeat(new String(new char[] { content }), getTable().getTotalWidth() - 2), new String(new char[] { AbstractTabularComponent.BORDER }), NEWLINE); }
/** * Constructor. * * @param myParent * table to be printed to * @param paramData * data to be plotted */ public Row(final NiceTable myParent, final String[] paramData) { super(myParent); data = new String[paramData.length]; System.arraycopy(paramData, 0, data, 0, paramData.length); for (int i = 0; i < data.length; i++) { getTable().updateColumnWidth(i, data[i].length()); data[i] = data[i].trim(); } }
/** * Simple setUp. */ @Before public void setUp() { table = new NiceTable(COLUMNNUMBER); }
/** * Test method for {@link org.perfidix.ouput.asciitable.NiceTable#NiceTable(int)}. */ @Test public void testCreate() { assertEquals("Test for create", "", table.toString()); }
/** * Tests whether any string contains a newline symbol. * * @param data * the array to check. * @return whether any of the strings contains a newline symbol. */ private boolean anyStringContainsNewLine(final String[] data) { boolean returnVal = false; for (int i = 0; i < data.length; i++) { if (Util.containsNewlines(data[i])) { returnVal = true; } } return returnVal; }
/** * allows the addition of lines. think of it as a horizontal rule in a * table. * * @param fill * any character with which to draw the line. */ public void addLine(final char fill) { rows.add(new DynamicLine(fill, this)); }
/** * Adds a header row to the table. can be used to give a table some title. * * <pre> * addHeader("hello",'.',NiceTable.LEFT) * would produce a row like * ... hello .......................... * {rows in here.} * </pre> * * @param title * the string to display as a header * @param mark * the mark to use for the rest of the column * @param orientation * the orientation of the header column. */ public void addHeader(final String title, final char mark, final Alignment orientation) { final Header header = new Header(title, mark, orientation, this); rows.add(header); }
/** * Returns the row width. * * @return int the width of the row */ private int getRowWidth() { int returnVal = 1; if (rows.size() < 1) { returnVal = 0; } for (int i = 0; i < rows.size(); i++) { if (rows.get(i) instanceof Row) { returnVal = ((Row)rows.get(i)).getRowWidth(); } } return returnVal; }
/** * the main method doing the work. draws everyting. * * @return the formatted table. */ @Override public String toString() { final StringBuilder builder = new StringBuilder(); for (int i = 0; i < rows.size(); i++) { final AbstractTabularComponent myObj = rows.get(i); builder.append(myObj.draw()); } return builder.toString(); }