-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableGeneratorByLine.java
More file actions
195 lines (151 loc) · 5.91 KB
/
TableGeneratorByLine.java
File metadata and controls
195 lines (151 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package cag.metro.bloq.utilidades;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TableGeneratorByLine
{
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 List<String> generateTable(List<String> headersList, List<List<String>> rowsList,
int... overRiddenHeaderHeight)
{
List<String> tabla = new ArrayList<String>(0);
int rowHeight = overRiddenHeaderHeight.length > 0 ? overRiddenHeaderHeight[0] : 1;
Map<Integer, Integer> columnMaxWidthMapping = getMaximumWidhtofTable(headersList, rowsList);
tabla.add(NEW_LINE);
createRowLine(tabla, headersList.size(), columnMaxWidthMapping);
StringBuilder cabeceraBuilder = new StringBuilder();
for (int headerIndex = 0; headerIndex < headersList.size(); headerIndex++)
{
fillCell(cabeceraBuilder, headersList.get(headerIndex), headerIndex, columnMaxWidthMapping);
}
tabla.add(cabeceraBuilder.toString());
createRowLine(tabla, headersList.size(), columnMaxWidthMapping);
for (List<String> row : rowsList)
{
StringBuilder valoresBuilder = new StringBuilder();
for (int cellIndex = 0; cellIndex < row.size(); cellIndex++)
{
fillCell(valoresBuilder, row.get(cellIndex), cellIndex, columnMaxWidthMapping);
}
tabla.add(valoresBuilder.toString());
}
createRowLine(tabla, headersList.size(), columnMaxWidthMapping);
return tabla;
}
private void fillSpace(StringBuilder stringBuilder, int length)
{
for (int i = 0; i < length; i++)
{
stringBuilder.append(" ");
}
}
private void createRowLine(List<String> tabla, int headersListSize,
Map<Integer, Integer> columnMaxWidthMapping)
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < headersListSize; i++)
{
if (i == 0)
{
stringBuilder.append(TABLE_JOINT_SYMBOL);
}
for (int j = 0; j < columnMaxWidthMapping.get(i) + PADDING_SIZE * 2; j++)
{
stringBuilder.append(TABLE_H_SPLIT_SYMBOL);
}
stringBuilder.append(TABLE_JOINT_SYMBOL);
}
tabla.add(stringBuilder.toString());
}
private Map<Integer, Integer> getMaximumWidhtofTable(List<String> headersList, List<List<String>> rowsList)
{
Map<Integer, Integer> 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<String> row : rowsList)
{
for (int columnIndex = 0; columnIndex < row.size(); columnIndex++)
{
if (row.get(columnIndex).length() > columnMaxWidthMapping.get(columnIndex))
{
columnMaxWidthMapping.put(columnIndex, row.get(columnIndex).length());
}
}
}
for (int columnIndex = 0; columnIndex < headersList.size(); columnIndex++)
{
if (columnMaxWidthMapping.get(columnIndex) % 2 != 0)
{
columnMaxWidthMapping.put(columnIndex, columnMaxWidthMapping.get(columnIndex) + 1);
}
}
return columnMaxWidthMapping;
}
private int getOptimumCellPadding(int cellIndex, int datalength, Map<Integer, Integer> columnMaxWidthMapping,
int cellPaddingSize)
{
if (datalength % 2 != 0)
{
datalength++;
}
if (datalength < columnMaxWidthMapping.get(cellIndex))
{
cellPaddingSize = cellPaddingSize + (columnMaxWidthMapping.get(cellIndex) - datalength) / 2;
}
return cellPaddingSize;
}
private void fillCell(StringBuilder stringBuilder, String cell, int cellIndex,
Map<Integer, Integer> columnMaxWidthMapping)
{
int cellPaddingSize = getOptimumCellPadding(cellIndex, cell.length(), columnMaxWidthMapping, PADDING_SIZE);
if (cellIndex == 0)
{
stringBuilder.append(TABLE_V_SPLIT_SYMBOL);
}
fillSpace(stringBuilder, cellPaddingSize);
stringBuilder.append(cell);
if (cell.length() % 2 != 0)
{
stringBuilder.append(" ");
}
fillSpace(stringBuilder, cellPaddingSize);
stringBuilder.append(TABLE_V_SPLIT_SYMBOL);
}
public static void main(String[] args) {
TableGeneratorByLine tableGenerator = new TableGeneratorByLine();
List<String> headersList = new ArrayList<>();
headersList.add("Tramos SMI");
headersList.add("% No embargable");
headersList.add("% embargable");
headersList.add("No embargable");
headersList.add("Embargable");
List<List<String>> rowsList = new ArrayList<>();
for (int i = 0; i < 5; i++)
{
List<String> row = new ArrayList<>();
row.add("XXXX,XX");
row.add("XXXX,XX");
row.add("XXXX,XX");
row.add("XXXX,XX");
row.add("XXXX,XX");
rowsList.add(row);
}
tableGenerator.generateTable(headersList, rowsList).forEach(line -> {
System.out.println(line);
});
}
}