package org.terracotta.statistics.registry;
import org.terracotta.statistics.Sample;
import org.terracotta.statistics.SampledStatistic;
import org.terracotta.statistics.StatisticType;
import org.terracotta.statistics.Table;
import org.terracotta.statistics.ValueStatistic;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static java.util.stream.Collectors.toList;
public class Statistic<T extends Serializable> implements Serializable {
private static final long serialVersionUID = 1L;
private final StatisticType type;
private final List<Sample<T>> samples;
public Statistic(StatisticType type) {
this(type, Collections.emptyList());
}
public Statistic(StatisticType type, Sample<T> sample) {
this(type, Collections.singletonList(sample));
}
public Statistic(StatisticType type, List<Sample<T>> samples) {
this.type = type;
this.samples = samples;
}
public StatisticType getType() {
return type;
}
public boolean isEmpty() {
return samples.isEmpty();
}
public List<Sample<T>> getSamples() {
return samples;
}
public Optional<T> getLatestSampleValue() {
return getLatestSample().map(Sample::getSample);
}
public Optional<Sample<T>> getLatestSample() {
return samples.isEmpty() ? Optional.empty() : Optional.of(samples.get(samples.size() - 1));
}
@Override
public String toString() {
return "Statistic{" +
"" + "type=" + type +
", samples=" + samples +
'}';
}
static <T extends Serializable> Statistic<T> (ValueStatistic<T> valueStatistic, long sinceMillis, long now) {
if (valueStatistic instanceof SampledStatistic) {
return new Statistic<>(
valueStatistic.type(),
((SampledStatistic<T>) valueStatistic).history(sinceMillis)
.stream()
.filter(s -> accepted(s.getSample()))
.collect(toList()));
}
if (sinceMillis <= now) {
T value = valueStatistic.value();
if (accepted(value)) {
return new Statistic<>(valueStatistic.type(), new Sample<>(now, value));
}
}
return new Statistic<>(valueStatistic.type());
}
private static <T extends Serializable> boolean accepted(T sample) {
return sample != null && !(sample instanceof Table && ((Table) sample).isEmpty());
}
}