diff --git a/java/src/org/broadinstitute/sting/playground/utils/report/templates/TextTable.java b/java/src/org/broadinstitute/sting/playground/utils/report/templates/TextTable.java new file mode 100644 index 000000000..72b8250ba --- /dev/null +++ b/java/src/org/broadinstitute/sting/playground/utils/report/templates/TextTable.java @@ -0,0 +1,180 @@ +package org.broadinstitute.sting.playground.utils.report.templates; + +import org.broadinstitute.sting.utils.StingException; + +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +/** + * this class handles generating text table output + */ +class TextTable { + // make a table of the number of rows + ArrayList> rows = new ArrayList>(); + + // keep a complementary array of the sizes of each column + ArrayList minimumSizes = new ArrayList(); + List header = new ArrayList(); + + // our default width + private static final int defaultMinimumSize = 15; + + // our seperator + private String seperator = ""; + + // our default cell entry + private static final String defaultCell = ""; + + private String name; + private String description; + + + /** + * create a text table, with the: + * @param name name of the table + * @param description what the table represents + * @param header the header fields + */ + TextTable(String name, String description, List header) { + this.name = name; + this.description = description; + for (int index = 0; index < header.size(); index++) + determineMinimumColumnWidth(header.get(index).length(),index); + this.header.addAll(header); + } + + /** + * create a text table + */ + TextTable() { + } + + /** + * set a cell of the table + * @param row, zero based + * @param column zero based + * @param entry cell to set + */ + public void setCell(int row, int column, String entry) { + if (rows.size() <= row) + createRow(row,column); + ArrayList rowData = rows.get(row); + if (rowData.size() <= column) + for (int x = rowData.size(); x <= column; x++) + rowData.add(""); + rows.get(row).set(column,entry); + determineMinimumColumnWidth(entry.length(),column); + } + + private void createRow(int row, int column) { + for (int x = rows.size(); x <= row; x++) { + ArrayList blank = new ArrayList(); + for (int col = 0; col <= column; col++) + blank.add(defaultCell); + rows.add(blank); + + } + } + + /** + * write the table to the writer + * @param writer the writer + */ + public void toPrintWriter(Writer writer) { + int index = 0; + for (String col : header) + writeToDisk(writer, col, index++, (index == header.size() -1)); + appendEndLine(writer,"\n"); + index = 0; + for (String col : header) + writeToDisk(writer, col, index++, (index == header.size() -1)); + appendEndLine(writer,"\n"); + for (ArrayList row : rows) { + if (row.size() > header.size()) + throw new IllegalStateException("More row-cells in table " + name + " then header columns"); + for (int y = 0; y < header.size(); y++) + if (row.size() >= y) + writeToDisk(writer, "", y, (y == header.size() - 1)); + else + writeToDisk(writer, row.get(y), y, (y == header.size() - 1)); + } + try { + writer.append("\n"); + } catch (IOException e) { + throw new StingException("Unable to write to the Writer"); + } + } + + private void appendEndLine(Writer writer, String str) { + try { + writer.append(str); + } catch (IOException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + /** + * write to disk a single string + * @param writer the writer to use + * @param str the string to write + * @param y an index of the string, for column width look-up + * @param lastVal are we the last value + */ + void writeToDisk(Writer writer, String str, int y, boolean lastVal) { + try { + writer.append(String.format("%1$-" + (getMinimumSizes(y) + 3) + "s", str)); + if (y != rows.size() - 1) + writer.append(seperator); + } catch (IOException e) { + throw new StingException("Unable to write to the Writer"); + } + } + + /** + * get the minimum size for a column + * @param column the column index + * @return the width + */ + public int getMinimumSizes(int column) { + if (column >= minimumSizes.size()) + return defaultMinimumSize; + return minimumSizes.get(column); + } + /** + * determine the minimum column width + * @param size the size of this string + * @param position the position (column) + */ + void determineMinimumColumnWidth(int size, int position) { + if (minimumSizes.size() <= position) + for (int x = minimumSizes.size(); x <= position; x++) + minimumSizes.add(x,0); + minimumSizes.set(position,(minimumSizes.get(position) > size) ? minimumSizes.get(position) : size); + } + + public List getHeader() { + return header; + } + + public void setHeader(List header) { + this.header = header; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/java/test/org/broadinstitute/sting/playground/utils/report/templates/TextTableUnitTest.java b/java/test/org/broadinstitute/sting/playground/utils/report/templates/TextTableUnitTest.java new file mode 100644 index 000000000..0369a16cf --- /dev/null +++ b/java/test/org/broadinstitute/sting/playground/utils/report/templates/TextTableUnitTest.java @@ -0,0 +1,46 @@ +package org.broadinstitute.sting.playground.utils.report.templates; + +import org.broadinstitute.sting.playground.utils.report.templates.TextTable; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; + +/** + * test out the text table + */ +public class TextTableUnitTest { + @Test + public void testBasicSetCell() { + TextTable table = new TextTable("name","description",new ArrayList()); + table.setCell(1,5,"entry"); + int entriesSeen = 0; + ArrayList> rows = table.rows; + for (int x = 0; x <= 1; x++) + for (int y = 0; y <= 5; y++) + if (x == 1 && y == 5) { + Assert.assertTrue(rows.get(x).get(y).equals("entry")); + entriesSeen++; + } else + Assert.assertTrue(rows.get(x).get(y).equals("")); + Assert.assertEquals("Incorrect number of entries seen",1,entriesSeen); + } + + @Test + public void testBasicSetTwoCells() { + TextTable table = new TextTable("name","description",new ArrayList()); + table.setCell(1,5,"entry"); + table.setCell(1,1,"entry"); + int entriesSeen = 0; + ArrayList> rows = table.rows; + for (int x = 0; x <= 1; x++) + for (int y = 0; y <= 5; y++) + if ((x == 1 && y == 5) || (x == 1 && y == 1)) { + Assert.assertTrue(rows.get(x).get(y).equals("entry")); + entriesSeen++; + } + else + Assert.assertTrue(rows.get(x).get(y).equals("")); + Assert.assertEquals("Incorrect number of entries seen",2,entriesSeen); + } +}