| 7 | import java.util.List; |
| 8 | |
| 9 | public class Strings { |
| 10 | public static String join(Iterable<String> strings, String sep, String prefix, String suffix) { |
| 11 | final StringBuilder sb = new StringBuilder(); |
| 12 | boolean first = true; |
| 13 | for (String s : strings) { |
| 14 | if (! first) sb.append(sep); |
| 15 | if (prefix != null) sb.append(prefix); |
| 16 | sb.append(s); |
| 17 | if (suffix != null) sb.append(suffix); |
| 18 | first = false; |
| 19 | } |
| 20 | return sb.toString(); |
| 21 | } |
| 22 | |
| 23 | public static String repeat(String str, int num) { |
| 24 | final String[] strs = new String[num]; |
| 25 | Arrays.fill(strs, str); |
| 26 | return join(Arrays.asList(strs), "", "", ""); |
| 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()); } |
| 53 | } |
| 54 | } |
nothing calls this directly
no outgoing calls
no test coverage detected