package ext.hello;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class DrawTable {
final String TAB = " ";
final String SPACE = " ";
final String RT = "\n";
final char NODE = '+';
final char DASH = '-';
final char SEP = '|';
private String title = "";
private String[] header = null;
private List<List<String>> data = null;
public DrawTable(String title, String[] header, List<List<String>> data) {
this.data = data;
this.header = header;
this.title = title;
}
public String draw() {
StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer();
StringBuffer bottom = new StringBuffer();
int COL = header.length;
Map<Integer, List<String>> dataRowMap = new HashMap<>();
int[][] colHWPair = new int[COL][2];
preHeaderWidth(COL, dataRowMap, colHWPair);
writeHeader(sb, sb1, bottom, COL, colHWPair);
writeRecord(sb, bottom, COL, colHWPair);
return sb.toString();
}
private void writeRecord(StringBuffer sb, StringBuffer bottom, int COL, int[][] colHWPair) {
StringBuffer sb1;
for (List<String> rec : data) {
sb1 = new StringBuffer();
sb1.append(SEP);
for (int i = 0; i < COL; i++) {
int[] pair = colHWPair[i];
String d = rec.get(i);
sb1.append(d);
for (int j = d.length(); j < pair[0]; j++) {
sb1.append(SPACE);
}
sb1.append(SEP);
}
sb1.append(RT).append(bottom.toString()).append(RT);
sb.append(sb1.toString());
}
}
private void writeHeader(StringBuffer sb, StringBuffer sb1, StringBuffer bottom, int COL, int[][] colHWPair) {
bottom.append(NODE);
sb1.append(SEP);
for (int i = 0; i < COL; i++) {
// Write Dash
int[] pair = colHWPair[i];
for (int j = 0; j < pair[0]; j++) {
bottom.append(DASH);
}
bottom.append(NODE);
sb1.append(header[i]);
for (int j = header[i].length(); j < pair[0]; j++) {
sb1.append(SPACE);
}
sb1.append(SEP);
}
sb.append(bottom.toString()).append('\n').append(sb1.toString()).append('\n').append(bottom.toString())
.append(RT);
}
private void preHeaderWidth(int COL, Map<Integer, List<String>> dataRowMap, int[][] colHWPair) {
for (int i = 0; i < COL; i++) {
String head = header[i];
List<String> dataListCol = dataRowMap.get(i);
if (dataListCol == null) {
dataListCol = new ArrayList<>();
dataRowMap.put(i, dataListCol);
}
colHWPair[i][0] = head.length();
colHWPair[i][1] = 1;
for (List<String> dataRec : data) {
String colData = dataRec.get(i);
int w = 0;
int h = 0;
if (!(colData == null || "".equals(colData))) {
int size = colData.length();
w = size;
h = 1;
}
colHWPair[i][0] = w > colHWPair[i][0] ? w : colHWPair[i][0];
colHWPair[i][1] = h > colHWPair[i][1] ? w : colHWPair[i][1];
dataListCol.add(colData);
}
}
}
public static List<List<String>> fileReader(String file) {
List<List<String>> data = new ArrayList<>();
BufferedReader fileIn = null;
try {
fileIn = new BufferedReader(new FileReader(file));
while (true) {
String lineData = fileIn.readLine();
if (lineData == null) {
break;
}
String[] colData = lineData.split(",");
List<String> colDataList = new ArrayList<>();
data.add(colDataList);
for (String col : colData) {
colDataList.add(col);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileIn.close();
} catch (IOException e) {
}
}
return data;
}
public static void main(String[] args) {
String title = "HelloWorld";
String fName = "C:\\Users\\shyuan\\temp\\data.csv";
List<List<String>> data = fileReader(fName);
String[] header = data.get(0).toArray(new String[]{});
data.remove(0);
System.out.println(new DrawTable(title, header, data).draw());
}
}