aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/main/java/org/onap/cli/fw/output/print/TableGenerator.java
blob: 389a98fe4b312d29ef421a657af9a7927f9a818f (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * Copyright 2017 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.cli.fw.output.print;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Printing Command result in Table format.
 *
 */
public class TableGenerator {

    private static final int PADDING_SIZE = 1;
    private static final String NEW_LINE = "\n";
    private static final String TABLE_JOINT_SYMBOL = "+";
    private static final String TABLE_V_SPLIT_SYMBOL = "|";
    private static final String TABLE_H_SPLIT_SYMBOL = "-";

    /**
     * Generate list of rows into table format.
     *
     * @param rowsList
     *            list of rows
     * @param printSeparator
     *            boolean
     * @return stringBuilder in tabular format
     */
    public String generateTable(List<List<Object>> rowsList, boolean printSeparator) {
        StringBuilder stringBuilder = new StringBuilder();

        if (rowsList.isEmpty()) {
            return stringBuilder.toString();
        }

        Map<Integer, Integer> columnMaxWidthMapping = getMaximumWidhtofColumns(rowsList);
        Map<Integer, Integer> rowMaxWidthMapping = getMaximumWidhtofRows(rowsList);

        if (printSeparator) {
            createRowLine(stringBuilder, rowsList.get(0).size(), columnMaxWidthMapping);
            stringBuilder.append(NEW_LINE);
        }

        for (int rowIndex = 0; rowIndex < rowsList.size(); rowIndex++) {

            List<Object> row = rowsList.get(rowIndex);
            int splitRowSize = rowMaxWidthMapping.get(rowIndex);
            if (splitRowSize > 1) {
                for (int cellIndex = 0; cellIndex < row.size(); cellIndex++) {
                    Object cell = row.get(cellIndex);
                    String finalCell;
                    if (cell != null) {
                        if (cell instanceof String) {
                            finalCell = (String) cell;
                        } else {
                            finalCell = ((List<String>) cell).get(0);
                        }
                        fillCell(stringBuilder, finalCell, cellIndex, columnMaxWidthMapping, printSeparator);
                    }
                }

                stringBuilder.append(NEW_LINE);

                for (int splitCellIndex = 1; splitCellIndex < splitRowSize; splitCellIndex++) {
                    for (int cellIndex = 0; cellIndex < row.size(); cellIndex++) {
                        Object cell = row.get(cellIndex);
                        String finalCell = "";
                        if (cell instanceof List) {
                            List<String> list = (List<String>) cell;
                            if (splitCellIndex < list.size()) {
                                finalCell = list.get(splitCellIndex);
                            }
                        }
                        fillCell(stringBuilder, finalCell, cellIndex, columnMaxWidthMapping, printSeparator);
                    }

                    stringBuilder.append(NEW_LINE);
                }

            } else {
                for (int cellIndex = 0; cellIndex < row.size(); cellIndex++) {
                    fillCell(stringBuilder, (String) row.get(cellIndex), cellIndex, columnMaxWidthMapping,
                            printSeparator);
                }
                stringBuilder.append(NEW_LINE);
            }

            if (printSeparator) {
                createRowLine(stringBuilder, rowsList.get(0).size(), columnMaxWidthMapping);
                stringBuilder.append(NEW_LINE);
            }

        }
        return stringBuilder.toString();
    }

    /**
     * Fill required space.
     *
     * @param stringBuilder
     *            table
     * @param length
     *            No. of spaces
     */
    private void fillSpace(StringBuilder stringBuilder, int length) {
        for (int i = 0; i < length; i++) {
            stringBuilder.append(" ");
        }
    }

    /**
     * Create a new row line.
     *
     * @param stringBuilder
     *            table
     * @param headersListSize
     *            No. of colums
     * @param columnMaxWidthMapping
     *            mapping of columns with cell width
     */
    private void createRowLine(StringBuilder stringBuilder, int headersListSize,
            Map<Integer, Integer> columnMaxWidthMapping) {
        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);
        }
    }

    /**
     * Get max width of columns.
     *
     * @param rowsList
     *            list of rows
     * @return mapping of column and max cell width
     */
    private Map<Integer, Integer> getMaximumWidhtofColumns(List<List<Object>> rowsList) {
        Map<Integer, Integer> columnMaxWidthMapping = new HashMap<>();
        if (rowsList == null || rowsList.isEmpty()) {
            return new HashMap<>();
        }
        for (int columnIndex = 0; columnIndex < rowsList.get(0).size(); columnIndex++) {
            columnMaxWidthMapping.put(columnIndex, 0);
        }

        for (List<Object> row : rowsList) {
            updateColWidth(row, columnMaxWidthMapping);
        }

        for (int columnIndex = 0; columnIndex < rowsList.get(0).size(); columnIndex++) {

            if (columnMaxWidthMapping.get(columnIndex) % 2 != 0) {
                columnMaxWidthMapping.put(columnIndex, columnMaxWidthMapping.get(columnIndex) + 1);
            }
        }

        return columnMaxWidthMapping;
    }

    /**
     * update the column width.
     *
     * @param row
     *            list of values
     * @param columnMaxWidthMapping
     *            mapping of column and max cell width
     */
    private void updateColWidth(List<Object> row, Map<Integer, Integer> columnMaxWidthMapping) {
        for (int columnIndex = 0; columnIndex < row.size(); columnIndex++) {
            Object obj = row.get(columnIndex);
            if (obj instanceof String) {
                String str = (String) obj;
                if (str.length() > columnMaxWidthMapping.get(columnIndex)) {
                    columnMaxWidthMapping.put(columnIndex, str.length());
                }
            } else if (obj instanceof List) {
                List<String> list = (List<String>) obj;

                int maxLength = list.get(0).length();

                for (String str : list) {
                    if (str.length() > maxLength) {
                        maxLength = str.length();
                    }
                }

                if (maxLength > columnMaxWidthMapping.get(columnIndex)) {
                    columnMaxWidthMapping.put(columnIndex, maxLength);
                }

            }

        }
    }

    /**
     * Get max width of rows.
     *
     * @param rowsList
     *            list of rows
     * @return map of rows and max width
     */
    private Map<Integer, Integer> getMaximumWidhtofRows(List<List<Object>> rowsList) {
        Map<Integer, Integer> rowMaxWidthMapping = new HashMap<>();

        for (int rowIndex = 0; rowIndex < rowsList.size(); rowIndex++) {
            rowMaxWidthMapping.put(rowIndex, 1);
        }

        for (int rowIndex = 0; rowIndex < rowsList.size(); rowIndex++) {
            int maxSize = 1;
            for (Object obj : rowsList.get(rowIndex)) {
                if (obj instanceof List && ((List) obj).size() > maxSize) {
                    maxSize = ((List) obj).size();
                }
            }

            if (maxSize > 1) {
                rowMaxWidthMapping.put(rowIndex, maxSize);
            }
        }
        return rowMaxWidthMapping;
    }

    /**
     * Get optimal cell padding.
     *
     * @param cellIndex
     *            cell index
     * @param datalength
     *            data length
     * @param columnMaxWidthMapping
     *            map of column and max cell width
     * @param cellPaddingSize
     *            cell padding size
     * @return cell padding
     */
    private int getOptimumCellPadding(int cellIndex, int datalength, Map<Integer, Integer> columnMaxWidthMapping,
            int cellPaddingSize) {
        int datLen = datalength;
        int paddingSize = cellPaddingSize;
        if (datLen % 2 != 0) {
            datLen++;
        }

        if (datLen < columnMaxWidthMapping.get(cellIndex)) {
            paddingSize = paddingSize + (columnMaxWidthMapping.get(cellIndex) - datLen) / 2;
        }

        return paddingSize;
    }

    /**
     * Fill the cell with required value.
     *
     * @param stringBuilder
     *            table
     * @param cell
     *            table cell value
     * @param cellIndex
     *            cell index
     * @param columnMaxWidthMapping
     *            map of column and max cell width
     */
    private void fillCell(StringBuilder stringBuilder, String cell, int cellIndex,
            Map<Integer, Integer> columnMaxWidthMapping, boolean printSeparator) {

        String filledCell = cell;

        if (filledCell == null) {
            filledCell = "";
        }
        int cellLength = filledCell.length();
        int cellPaddingSize = getOptimumCellPadding(cellIndex, cellLength, columnMaxWidthMapping, PADDING_SIZE);

        if (cellIndex == 0 && printSeparator) {
            stringBuilder.append(TABLE_V_SPLIT_SYMBOL);
        }

        stringBuilder.append(filledCell);
        fillSpace(stringBuilder, cellPaddingSize);

        if (cellLength % 2 != 0) {
            stringBuilder.append(" ");
        }

        fillSpace(stringBuilder, cellPaddingSize);
        if (printSeparator) {
            stringBuilder.append(TABLE_V_SPLIT_SYMBOL);
        }

    }
}