| 27 | } |
| 28 | |
| 29 | public static List<String> formatTable(List<String[]> rows) { |
| 30 | final Integer[] maxLengths = new Integer[rows.get(0).length]; |
| 31 | for (String[] row : rows) { |
| 32 | if (maxLengths.length != row.length) throw new IllegalStateException("mismatched columns"); |
| 33 | for (int i = 0; i < maxLengths.length; i++) { |
| 34 | if (maxLengths[i] == null || maxLengths[i] < row[i].length()) { |
| 35 | maxLengths[i] = row[i].length(); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | final List<String> lines = new LinkedList<String>(); |
| 41 | for (String[] row : rows) { |
| 42 | for (int i = 0; i < maxLengths.length; i++) { |
| 43 | final String pad = repeat(" ", maxLengths[i] - row[i].length()); |
| 44 | row[i] = row[i] + pad; |
| 45 | } |
| 46 | lines.add(join(Arrays.asList(row), " ", "", "")); |
| 47 | } |
| 48 | return lines; |
| 49 | } |
| 50 | |
| 51 | public static class ToStringComparator implements Comparator<Object> { |
| 52 | public int compare(Object o1, Object o2) { return o1.toString().compareTo(o2.toString()); } |