@Test public void testDynamicTableWriter() { table.addRow( "1", "Bucket 1", "12345-12345-12345-12345-12345-12345", "" ); table.addRow( "2", "Bucket 2 - This is a really really really long name", "12345-12345-12345-12345-12345-12345", "This is a really really really really really really really really really really long description" ); table.addRow( "3", "Bucket 3", "12345-12345-12345-12345-12345-12345", null ); tableWriter.write(table, printStream); final String result = new String(outputStream.toByteArray(), StandardCharsets.UTF_8); final String expected = "\n" + "# Name Id Description \n" + "- ------------------------------------ ----------------------------------- ---------------------------------------- \n" + "1 Bucket 1 12345-12345-12345-12345-12345-12345 \n" + "2 Bucket 2 - This is a really reall... 12345-12345-12345-12345-12345-12345 This is a really really really really... \n" + "3 Bucket 3 12345-12345-12345-12345-12345-12345 (empty) \n" + "\n"; Assert.assertEquals(expected, result); }
@Override public void write(final Table table, final PrintStream output) { if (table == null) { throw new IllegalArgumentException("Table cannot be null"); } if (output == null) { throw new IllegalArgumentException("Output cannot be null"); } if (table.getColumns().isEmpty()) { throw new IllegalArgumentException("Table has no columns to write"); } output.println(); final List<TableColumn> columns = table.getColumns(); final List<String[]> rows = table.getRows(); final int numColumns = columns.size(); final Integer[] columnLengths = determineColumnLengths(columns, rows); final List<String> columnNames = columns.stream().map(c -> c.getName()).collect(Collectors.toList()); final Object[] columnLengthsObj = Arrays.copyOf(columnLengths, numColumns, Object[].class); final Object[] columnNamesObj = columnNames.toArray(new Object[numColumns]); final String columnsPatternFormat = String.join("", Collections.nCopies(numColumns, "%%-%ds ")); final String columnsPattern = String.format(columnsPatternFormat, columnLengthsObj); final String header = String.format(columnsPattern, columnNamesObj); output.println(header); final Object[] headerLineValues = new Object[numColumns]; for (int i=0; i < numColumns; i++) { int length = columnLengths[i]; headerLineValues[i] = String.join("", Collections.nCopies(length, "-")); } final String headerLine = String.format(columnsPattern, headerLineValues); output.println(headerLine); for (String[] row : rows) { final Object[] rowValues = new Object[row.length]; for (int i=0; i < row.length; i++) { final TableColumn column = columns.get(i); if (column.isAbbreviated()) { rowValues[i] = StringUtils.abbreviate(row[i], columnLengths[i]); } else { rowValues[i] = row[i]; } } final String rowString = String.format(columnsPattern, rowValues); output.println(rowString); } output.println(); output.flush(); }
DynamicTableWriter implements TableWriter { @Override public void write(final Table table, final PrintStream output) { if (table == null) { throw new IllegalArgumentException("Table cannot be null"); } if (output == null) { throw new IllegalArgumentException("Output cannot be null"); } if (table.getColumns().isEmpty()) { throw new IllegalArgumentException("Table has no columns to write"); } output.println(); final List<TableColumn> columns = table.getColumns(); final List<String[]> rows = table.getRows(); final int numColumns = columns.size(); final Integer[] columnLengths = determineColumnLengths(columns, rows); final List<String> columnNames = columns.stream().map(c -> c.getName()).collect(Collectors.toList()); final Object[] columnLengthsObj = Arrays.copyOf(columnLengths, numColumns, Object[].class); final Object[] columnNamesObj = columnNames.toArray(new Object[numColumns]); final String columnsPatternFormat = String.join("", Collections.nCopies(numColumns, "%%-%ds ")); final String columnsPattern = String.format(columnsPatternFormat, columnLengthsObj); final String header = String.format(columnsPattern, columnNamesObj); output.println(header); final Object[] headerLineValues = new Object[numColumns]; for (int i=0; i < numColumns; i++) { int length = columnLengths[i]; headerLineValues[i] = String.join("", Collections.nCopies(length, "-")); } final String headerLine = String.format(columnsPattern, headerLineValues); output.println(headerLine); for (String[] row : rows) { final Object[] rowValues = new Object[row.length]; for (int i=0; i < row.length; i++) { final TableColumn column = columns.get(i); if (column.isAbbreviated()) { rowValues[i] = StringUtils.abbreviate(row[i], columnLengths[i]); } else { rowValues[i] = row[i]; } } final String rowString = String.format(columnsPattern, rowValues); output.println(rowString); } output.println(); output.flush(); } }
DynamicTableWriter implements TableWriter { @Override public void write(final Table table, final PrintStream output) { if (table == null) { throw new IllegalArgumentException("Table cannot be null"); } if (output == null) { throw new IllegalArgumentException("Output cannot be null"); } if (table.getColumns().isEmpty()) { throw new IllegalArgumentException("Table has no columns to write"); } output.println(); final List<TableColumn> columns = table.getColumns(); final List<String[]> rows = table.getRows(); final int numColumns = columns.size(); final Integer[] columnLengths = determineColumnLengths(columns, rows); final List<String> columnNames = columns.stream().map(c -> c.getName()).collect(Collectors.toList()); final Object[] columnLengthsObj = Arrays.copyOf(columnLengths, numColumns, Object[].class); final Object[] columnNamesObj = columnNames.toArray(new Object[numColumns]); final String columnsPatternFormat = String.join("", Collections.nCopies(numColumns, "%%-%ds ")); final String columnsPattern = String.format(columnsPatternFormat, columnLengthsObj); final String header = String.format(columnsPattern, columnNamesObj); output.println(header); final Object[] headerLineValues = new Object[numColumns]; for (int i=0; i < numColumns; i++) { int length = columnLengths[i]; headerLineValues[i] = String.join("", Collections.nCopies(length, "-")); } final String headerLine = String.format(columnsPattern, headerLineValues); output.println(headerLine); for (String[] row : rows) { final Object[] rowValues = new Object[row.length]; for (int i=0; i < row.length; i++) { final TableColumn column = columns.get(i); if (column.isAbbreviated()) { rowValues[i] = StringUtils.abbreviate(row[i], columnLengths[i]); } else { rowValues[i] = row[i]; } } final String rowString = String.format(columnsPattern, rowValues); output.println(rowString); } output.println(); output.flush(); }  }
DynamicTableWriter implements TableWriter { @Override public void write(final Table table, final PrintStream output) { if (table == null) { throw new IllegalArgumentException("Table cannot be null"); } if (output == null) { throw new IllegalArgumentException("Output cannot be null"); } if (table.getColumns().isEmpty()) { throw new IllegalArgumentException("Table has no columns to write"); } output.println(); final List<TableColumn> columns = table.getColumns(); final List<String[]> rows = table.getRows(); final int numColumns = columns.size(); final Integer[] columnLengths = determineColumnLengths(columns, rows); final List<String> columnNames = columns.stream().map(c -> c.getName()).collect(Collectors.toList()); final Object[] columnLengthsObj = Arrays.copyOf(columnLengths, numColumns, Object[].class); final Object[] columnNamesObj = columnNames.toArray(new Object[numColumns]); final String columnsPatternFormat = String.join("", Collections.nCopies(numColumns, "%%-%ds ")); final String columnsPattern = String.format(columnsPatternFormat, columnLengthsObj); final String header = String.format(columnsPattern, columnNamesObj); output.println(header); final Object[] headerLineValues = new Object[numColumns]; for (int i=0; i < numColumns; i++) { int length = columnLengths[i]; headerLineValues[i] = String.join("", Collections.nCopies(length, "-")); } final String headerLine = String.format(columnsPattern, headerLineValues); output.println(headerLine); for (String[] row : rows) { final Object[] rowValues = new Object[row.length]; for (int i=0; i < row.length; i++) { final TableColumn column = columns.get(i); if (column.isAbbreviated()) { rowValues[i] = StringUtils.abbreviate(row[i], columnLengths[i]); } else { rowValues[i] = row[i]; } } final String rowString = String.format(columnsPattern, rowValues); output.println(rowString); } output.println(); output.flush(); }  @Override void write(final Table table, final PrintStream output); }
DynamicTableWriter implements TableWriter { @Override public void write(final Table table, final PrintStream output) { if (table == null) { throw new IllegalArgumentException("Table cannot be null"); } if (output == null) { throw new IllegalArgumentException("Output cannot be null"); } if (table.getColumns().isEmpty()) { throw new IllegalArgumentException("Table has no columns to write"); } output.println(); final List<TableColumn> columns = table.getColumns(); final List<String[]> rows = table.getRows(); final int numColumns = columns.size(); final Integer[] columnLengths = determineColumnLengths(columns, rows); final List<String> columnNames = columns.stream().map(c -> c.getName()).collect(Collectors.toList()); final Object[] columnLengthsObj = Arrays.copyOf(columnLengths, numColumns, Object[].class); final Object[] columnNamesObj = columnNames.toArray(new Object[numColumns]); final String columnsPatternFormat = String.join("", Collections.nCopies(numColumns, "%%-%ds ")); final String columnsPattern = String.format(columnsPatternFormat, columnLengthsObj); final String header = String.format(columnsPattern, columnNamesObj); output.println(header); final Object[] headerLineValues = new Object[numColumns]; for (int i=0; i < numColumns; i++) { int length = columnLengths[i]; headerLineValues[i] = String.join("", Collections.nCopies(length, "-")); } final String headerLine = String.format(columnsPattern, headerLineValues); output.println(headerLine); for (String[] row : rows) { final Object[] rowValues = new Object[row.length]; for (int i=0; i < row.length; i++) { final TableColumn column = columns.get(i); if (column.isAbbreviated()) { rowValues[i] = StringUtils.abbreviate(row[i], columnLengths[i]); } else { rowValues[i] = row[i]; } } final String rowString = String.format(columnsPattern, rowValues); output.println(rowString); } output.println(); output.flush(); }  @Override void write(final Table table, final PrintStream output);  }