package com.microchip.mplab.ipecmd.common; import java.util.HashMap; import java.util.List; import java.util.Map; public class TableGenerator { private int PADDING_SIZE = 2; private String NEW_LINE = "\n"; private String TABLE_JOINT_SYMBOL = "+"; private String TABLE_V_SPLIT_SYMBOL = "|"; private String TABLE_H_SPLIT_SYMBOL = "-"; public String generateTable(List headersList, List> rowsList, int... overRiddenHeaderHeight) { StringBuilder stringBuilder = new StringBuilder(); int rowHeight = overRiddenHeaderHeight.length > 0 ? overRiddenHeaderHeight[0] : 1; Map columnMaxWidthMapping = this.getMaximumWidhtofTable(headersList, rowsList); stringBuilder.append(this.NEW_LINE); stringBuilder.append(this.NEW_LINE); this.createRowLine(stringBuilder, headersList.size(), columnMaxWidthMapping); stringBuilder.append(this.NEW_LINE); for (int headerIndex = 0; headerIndex < headersList.size(); headerIndex++) { this.fillCell(stringBuilder, headersList.get(headerIndex), headerIndex, columnMaxWidthMapping); } stringBuilder.append(this.NEW_LINE); this.createRowLine(stringBuilder, headersList.size(), columnMaxWidthMapping); for (List row : rowsList) { for (int i = 0; i < rowHeight; i++) { stringBuilder.append(this.NEW_LINE); } for (int cellIndex = 0; cellIndex < row.size(); cellIndex++) { this.fillCell(stringBuilder, row.get(cellIndex), cellIndex, columnMaxWidthMapping); } } stringBuilder.append(this.NEW_LINE); this.createRowLine(stringBuilder, headersList.size(), columnMaxWidthMapping); stringBuilder.append(this.NEW_LINE); stringBuilder.append(this.NEW_LINE); return stringBuilder.toString(); } private void fillSpace(StringBuilder stringBuilder, int length) { for (int i = 0; i < length; i++) { stringBuilder.append(" "); } } private void createRowLine(StringBuilder stringBuilder, int headersListSize, Map columnMaxWidthMapping) { for (int i = 0; i < headersListSize; i++) { if (i == 0) { stringBuilder.append(this.TABLE_JOINT_SYMBOL); } for (int j = 0; j < columnMaxWidthMapping.get(i) + this.PADDING_SIZE * 2; j++) { stringBuilder.append(this.TABLE_H_SPLIT_SYMBOL); } stringBuilder.append(this.TABLE_JOINT_SYMBOL); } } private Map getMaximumWidhtofTable(List headersList, List> rowsList) { Map columnMaxWidthMapping = new HashMap<>(); for (int columnIndex = 0; columnIndex < headersList.size(); columnIndex++) { columnMaxWidthMapping.put(columnIndex, 0); } for (int columnIndex = 0; columnIndex < headersList.size(); columnIndex++) { if (headersList.get(columnIndex).length() > columnMaxWidthMapping.get(columnIndex)) { columnMaxWidthMapping.put(columnIndex, headersList.get(columnIndex).length()); } } for (List row : rowsList) { for (int columnIndexx = 0; columnIndexx < row.size(); columnIndexx++) { if (row.get(columnIndexx).length() > columnMaxWidthMapping.get(columnIndexx)) { columnMaxWidthMapping.put(columnIndexx, row.get(columnIndexx).length()); } } } for (int columnIndexxx = 0; columnIndexxx < headersList.size(); columnIndexxx++) { if (columnMaxWidthMapping.get(columnIndexxx) % 2 != 0) { columnMaxWidthMapping.put(columnIndexxx, columnMaxWidthMapping.get(columnIndexxx) + 1); } } return columnMaxWidthMapping; } private int getOptimumCellPadding(int cellIndex, int datalength, Map columnMaxWidthMapping, int cellPaddingSize) { if (datalength % 2 != 0) { datalength++; } if (datalength < columnMaxWidthMapping.get(cellIndex)) { cellPaddingSize += (columnMaxWidthMapping.get(cellIndex) - datalength) / 2; } return cellPaddingSize; } private void fillCell(StringBuilder stringBuilder, String cell, int cellIndex, Map columnMaxWidthMapping) { int cellPaddingSize = this.getOptimumCellPadding(cellIndex, cell.length(), columnMaxWidthMapping, this.PADDING_SIZE); if (cellIndex == 0) { stringBuilder.append(this.TABLE_V_SPLIT_SYMBOL); } this.fillSpace(stringBuilder, cellPaddingSize); stringBuilder.append(cell); if (cell.length() % 2 != 0) { stringBuilder.append(" "); } this.fillSpace(stringBuilder, cellPaddingSize); stringBuilder.append(this.TABLE_V_SPLIT_SYMBOL); } }