package org.terracotta.statistics;
import java.io.Serializable;
import java.util.Collection;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class TableValueStatistic extends TableSkeleton implements ValueStatistic<Table> {
private static final long serialVersionUID = 1L;
public static TableValueStatistic.Builder newBuilder(String... innerStatisticNames) {
return new TableValueStatistic.Builder(innerStatisticNames);
}
private TableValueStatistic(String... innerStatisticNames) {
super(innerStatisticNames);
}
@Override
public StatisticType type() {
return StatisticType.TABLE;
}
@Override
public Table value() {
String[] statisticNames = getStatisticNames();
return Table.newBuilder(statisticNames)
.withRows(getRowLabels(), (row, rowBuilder) -> {
ValueStatistic<? extends Serializable>[] vals = getStatistics(row);
for (int i = 0; i < vals.length; i++) {
rowBuilder.setStatistic(statisticNames[i], vals[i].type(), vals[i].value());
}
})
.build();
}
public static class Builder {
private final TableValueStatistic stat;
private Builder(String... innerStatisticNames) {
stat = new TableValueStatistic(innerStatisticNames);
}
public <T extends Serializable> Builder registerStatistic(String rowName, String statisticName, ValueStatistic<T> accessor) {
stat.insert(rowName, statisticName, accessor);
return this;
}
public Builder withRow(String rowName, Consumer<RowBuilder> c) {
c.accept(new RowBuilder() {
@Override
public <T extends Serializable> RowBuilder registerStatistic(String statisticName, ValueStatistic<T> accessor) {
stat.insert(rowName, statisticName, accessor);
return this;
}
});
return this;
}
public Builder withRows(Collection<String> rowNames, BiConsumer<String, RowBuilder> c) {
rowNames.forEach(rowName -> c.accept(rowName, new RowBuilder() {
@Override
public <T extends Serializable> RowBuilder registerStatistic(String statisticName, ValueStatistic<T> accessor) {
stat.insert(rowName, statisticName, accessor);
return this;
}
}));
return this;
}
public ValueStatistic<Table> build() {
return stat;
}
}
public interface RowBuilder {
<T extends Serializable> RowBuilder registerStatistic(String statisticName, ValueStatistic<T> accessor);
}
}