no.ssb.jsonstat.v1.table.CsvRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-stat-java Show documentation
Show all versions of json-stat-java Show documentation
Json stat implementation in Java
The newest version!
package no.ssb.jsonstat.v1.table;
import no.ssb.jsonstat.v1.Data;
import java.util.List;
public class CsvRenderer implements Renderer {
@Override
public String render(Table table) {
StringBuilder headerBuilder = new StringBuilder();
StringBuilder bodyBuilder = new StringBuilder();
List headers = table.getHeaders();
headerBuilder.append("#");
for (int i1 = 0; i1 < headers.size(); i1++) {
TableHeader header = headers.get(i1);
headerBuilder.append(header.getLabel().orElseGet(() -> header.getId().orElse(null)));
if (i1 < headers.size()-1) {
headerBuilder.append(",");
}
}
headerBuilder.append("\n");
List> rows = table.getRows();
for (List row : rows) {
for(int i = 0; i < row.size(); i++) {
Data data = row.get(i);
bodyBuilder.append(data.toString());
if (i < row.size()-1) {
bodyBuilder.append(",");
}
}
bodyBuilder.append("\n");
}
headerBuilder.append(bodyBuilder);
return headerBuilder.toString();
}
}