Java lib to build and print table on console? [closed]
We don’t allow 开发者_Go百科questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this questionIs there a Java lib out there which allows you to programatically build a table, e.g. by adding rows/cols one after each other, and finally returns a string (...that can be printed out on console or so)?
The libs I have found and know address UI components (JTable, Swing, AWT) only...
I was looking for something like this and ended up writing it myself, which I am happy to share with you. I wanted something that I can use like
ConsoleStringTable table= new ConsoleStringTable();
table.addString(0, 0, "AveryVeryVeryLongWord");
table.addString(0, 1, "AnotherWord");
table.addString(1, 0, "Short");
table.addString(1, 1, "Fast");
System.out.println(table.toString());
Which outputs
AveryVeryVeryLongWord AnotherWord
Short Fast
Implementation
using guava for padding
package util;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Strings;
public class ConsoleStringTable {
private class Index {
int _row, _colum;
public Index (int r, int c) {
_row= r;
_colum= c;
}
@Override
public boolean equals (Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Index other= (Index) obj;
if (_colum != other._colum)
return false;
if (_row != other._row)
return false;
return true;
}
@Override
public int hashCode () {
final int prime= 31;
int result= 1;
result= prime * result + _colum;
result= prime * result + _row;
return result;
}
}
Map<Index, String> _strings= new HashMap<ConsoleStringTable.Index, String>();
Map<Integer, Integer> _columSizes= new HashMap<Integer, Integer>();
int _numRows= 0;
int _numColumns= 0;
public void addString (int row, int colum, String content) {
_numRows= Math.max(_numRows, row + 1);
_numColumns= Math.max(_numColumns, colum + 1);
Index index= new Index(row, colum);
_strings.put(index, content);
setMaxColumnSize(colum, content);
}
private void setMaxColumnSize (int colum, String content) {
int size= content.length();
Integer currentSize= _columSizes.get(colum);
if (currentSize == null || (currentSize != null && currentSize < size)) {
_columSizes.put(colum, size);
}
}
public int getColumSize (int colum) {
Integer size= _columSizes.get(colum);
if (size == null) {
return 0;
} else {
return size;
}
}
public String getString (int row, int colum) {
Index index= new Index(row, colum);
String string= _strings.get(index);
if (string == null) {
return "";
} else {
return string;
}
}
public String getTableAsString (int padding) {
String out= "";
for (int r= 0; r < _numRows; r++) {
for (int c= 0; c < _numColumns; c++) {
int columSize= getColumSize(c);
String content= getString(r, c);
int pad= c == _numColumns - 1 ? 0 : padding;
out+= Strings.padEnd(content, columSize + pad, ' ');
}
if (r < _numRows - 1) {
out+= "\n";
}
}
return out;
}
@Override
public String toString () {
return getTableAsString(1);
}
}
I don't know such a library but you could go with a template engine like FreeMarker or Velocity. Define your table layout in a template, put your content (e.g. List of List to define your rows and columns) in the template model and 'merge' the template with your content to receive your result (table) String. Might be not the simplest approach but it is also not that complicated. In any case you have the full control of your result!
精彩评论