1 22 23 package org.continuent.sequoia.console.text.formatter; 24 25 30 public class TableFormatter 31 { 32 42 public static String format(String [] headers, String [][] cells, 43 boolean headersAsRow) 44 { 45 StringBuffer buf = new StringBuffer (); 46 int[] longestLengths = findLongestDataLengths(headers, cells, headersAsRow); 47 String horizontalRule = createHorizontalRule(longestLengths); 48 if (headersAsRow) 49 { 50 buf.append(horizontalRule); 51 appendHeadersTo(headers, buf, longestLengths); 52 buf.append(horizontalRule); 53 appendCellsTo(cells, buf, longestLengths); 54 buf.append(horizontalRule); 55 } 56 else 57 { 58 appendHeaderAndCells(headers, cells, buf, longestLengths, horizontalRule); 59 } 60 return buf.toString(); 61 62 } 63 64 74 private static String createHorizontalRule(int[] longestLengths) 75 { 76 StringBuffer buf = new StringBuffer ("+"); 77 for (int i = 0; i < longestLengths.length; i++) 78 { 79 appendStringTo(buf, "-", longestLengths[i] + 2); 82 buf.append("+"); 83 } 84 buf.append("\n"); 85 return buf.toString(); 86 } 87 88 89 96 private static void appendStringTo(StringBuffer buf, String string, int n) 97 { 98 for (int i = 0; i < n; i++) 99 { 100 buf.append(string); 101 } 102 } 103 104 117 private static int[] findLongestDataLengths(String [] headers, 118 String [][] cells, boolean headersAsRow) 119 { 120 if (headersAsRow) 121 { 122 int[] longestLengths = new int[headers.length]; 124 for (int j = 0; j < longestLengths.length; j++) 125 { 126 int maxLength = 0; 127 maxLength = Math.max(maxLength, headers[j].length()); 128 for (int i = 0; i < cells.length; i++) 129 { 130 maxLength = Math.max(maxLength, cells[i][j].length()); 131 } 132 longestLengths[j] = maxLength; 133 } 134 return longestLengths; 135 } 136 else 137 { 138 int[] longestLengths = new int[2]; 142 longestLengths[0] = 0; 143 for (int i = 0; i < headers.length; i++) 144 { 145 longestLengths[0] = Math.max(longestLengths[0], headers[i].length()); 146 } 147 longestLengths[1] = 0; 148 for (int i = 0; i < cells.length; i++) 149 { 150 for (int j = 0; j < cells[i].length; j++) 151 { 152 String cell = cells[i][j]; 153 longestLengths[1] = Math.max(longestLengths[1], cell.length()); 154 } 155 } 156 return longestLengths; 157 } 158 } 159 160 161 169 private static void appendCellsTo(String [][] cells, StringBuffer buf, 170 int[] longestLengths) 171 { 172 for (int i = 0; i < cells.length; i++) 173 { 174 buf.append("| "); 175 for (int j = 0; j < cells[i].length; j++) 176 { 177 String cell = cells[i][j]; 178 buf.append(cell); 179 appendStringTo(buf, " ", longestLengths[j] - cell.length()); 180 buf.append(" | "); 181 } 182 buf.append("\n"); 183 } 184 } 185 186 194 private static void appendHeadersTo(String [] headers, StringBuffer buf, 195 int[] longestLengths) 196 { 197 buf.append("| "); 198 for (int i = 0; i < headers.length; i++) 199 { 200 String header = headers[i]; 201 buf.append(header); 202 appendStringTo(buf, " ", longestLengths[i] - header.length()); 203 buf.append(" | "); 204 } 205 buf.append("\n"); 206 } 207 208 220 private static void appendHeaderAndCells(String [] headers, String [][] cells, 221 StringBuffer buf, int[] longestLengths, String horizontalRule) 222 { 223 buf.append(horizontalRule); 224 for (int i = 0; i < cells.length; i++) 225 { 226 for (int j = 0; j < cells[i].length; j++) 227 { 228 buf.append("| "); 229 buf.append(headers[j]); 230 appendStringTo(buf, " ", longestLengths[0] - headers[j].length()); 231 buf.append(" | "); 232 String cell = cells[i][j]; 233 buf.append(cell); 234 appendStringTo(buf, " ", longestLengths[1] - cell.length()); 235 buf.append(" |\n"); 236 } 237 buf.append(horizontalRule); 238 } 239 } 240 } 241 | Popular Tags |