no.ssb.jsonstat.v1.table.Table 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 com.google.common.collect.Lists;
import no.ssb.jsonstat.v1.Category;
import no.ssb.jsonstat.v1.Data;
import no.ssb.jsonstat.v1.Dataset;
import no.ssb.jsonstat.v1.Dimension;
import no.ssb.jsonstat.v1.util.IntCartesianProduct;
import java.util.*;
import java.util.stream.StreamSupport;
import static no.ssb.jsonstat.v1.util.CollectionUtils.join;
import static no.ssb.jsonstat.v1.util.CollectionUtils.product;
public final class Table {
private Optional title;
private final List headers = new ArrayList<>();
private final List> rows = new ArrayList<>();
public Table(Optional title, List headers, List> rows) {
this.title = title;
this.headers.addAll(headers);
for (List row : rows) {
this.rows.add(Lists.newArrayList(row));
}
}
public static Table fromDataset(Dataset dataset) {
return fromDataset(dataset, findRowDimension(dataset));
}
public static Table fromDataset(Dataset dataset, Dimension rowDimension) {
List dimensions = dataset.getDimensions();
List headers = buildHeader(dimensions, rowDimension.getId());
List> rows = dataset.getRows(rowDimension);
//TODO: maybe this should really be part of dataset.getRows()...
int i = 0;
for (String s : rowDimension.getCategory()) {
List row = rows.get(i);
int j = 0;
row.add(j, new Data(rowDimension.getCategory().getLabel(s).orElse(s), Optional.empty()));
for (Dimension dimension : dimensions) {
if (dimension.isConstant()) {
boolean added = false;
for (String id : dimension.getCategory()) {
row.add(j, new Data(dimension.getCategory().getLabel(id).orElse(id), Optional.empty()));
added = true;
}
if (!added) {
row.add(j, new Data(dimension.getLabel().orElse(dimension.getId()), Optional.empty()));
}
j++;
}
}
i++;
}
return new Table(dataset.getLabel(), headers, rows);
}
private static List buildHeader(List dimensions, String rowDimension) {
//TODO: This is stupid. Fix it.
List> categories = new ArrayList<>();
List headers = new ArrayList<>();
for (Dimension dimension : dimensions) {
boolean isRow = rowDimension.equals(dimension.getId());
if (dimension.isRequired() && !isRow) {
Category category = dimension.getCategory();
List cats = new ArrayList<>();
for (String id : category) {
cats.add(category.getLabel(id).orElse(id));
}
categories.add(cats);
}
else if (dimension.isConstant()) {
Optional dimensionId = StreamSupport.stream(dimension.getCategory().spliterator(), false)
.findFirst();
headers.add(new TableHeader(dimensionId, dimension.getLabel()));
}
if (isRow) {
headers.add(new TableHeader(Optional.empty(), dimension.getLabel()));
}
}
List combinations = product(categories);
for (String[] combination : combinations) {
String label = join(Arrays.asList(combination), " ");
headers.add(new TableHeader(Optional.empty(), Optional.of(label)));
}
return headers;
}
private static Dimension findRowDimension(Dataset ds) {
IntCartesianProduct p = ds.asCartasianProduct();
return ds.getDimensions().get(p.getMaxIndex());
}
public Optional getTitle() {
return title;
}
public TableHeader getHeader(int index) {
return headers.get(index);
}
public TableHeader getHeader(String id) {
return getHeader(getHeaderIndex(id));
}
public int getHeaderIndex(String id) {
for (int i = 0; i < headers.size(); i++) {
TableHeader h = headers.get(i);
if (h.getId().equals(Optional.of(id))) {
return i;
}
}
return -1;
}
public List getHeaders() {
return headers;
}
public List getRow(int index) {
if (index < rows.size()) {
return rows.get(index);
}
return Collections.emptyList();
}
public List> getRows() {
return rows;
}
public A render(Renderer renderer) {
return renderer.render(this);
}
}