All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.ssb.jsonstat.v1.table.CsvRenderer Maven / Gradle / Ivy

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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy