You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.8 KiB
74 lines
1.8 KiB
package model;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.function.BiFunction;
|
|
|
|
public class Statistics<T> {
|
|
private final Map<String, Integer> counters;
|
|
private final Map<String, Double> measurements;
|
|
private final Map<String, String> attributes;
|
|
private final T context;
|
|
|
|
public Statistics(T context) {
|
|
this.context = context;
|
|
this.counters = new HashMap<>();
|
|
this.measurements = new HashMap<>();
|
|
this.attributes = new HashMap<>();
|
|
}
|
|
|
|
public void increment(String key) {
|
|
counters.merge(key, 1, Integer::sum);
|
|
}
|
|
|
|
public void increment(String key, int delta) {
|
|
counters.merge(key, delta, Integer::sum);
|
|
}
|
|
|
|
public void record(String key, double value) {
|
|
measurements.put(key, value);
|
|
}
|
|
|
|
public void record(String key, String value) {
|
|
attributes.put(key, value);
|
|
}
|
|
|
|
public int getCount(String key) {
|
|
return counters.getOrDefault(key, 0);
|
|
}
|
|
|
|
public double getMeasurement(String key) {
|
|
return measurements.getOrDefault(key, 0.0);
|
|
}
|
|
|
|
public String getAttribute(String key) {
|
|
return attributes.get(key);
|
|
}
|
|
|
|
public Map<String, Integer> getAllCounters() {
|
|
return new HashMap<>(counters);
|
|
}
|
|
|
|
public Map<String, Double> getAllMeasurements() {
|
|
return new HashMap<>(measurements);
|
|
}
|
|
|
|
public Map<String, String> getAllAttributes() {
|
|
return new HashMap<>(attributes);
|
|
}
|
|
|
|
public T getContext() {
|
|
return context;
|
|
}
|
|
|
|
public void clear() {
|
|
counters.clear();
|
|
measurements.clear();
|
|
attributes.clear();
|
|
}
|
|
|
|
public String toString() {
|
|
return String.format("Statistics{context=%s, counters=%s, measurements=%s}",
|
|
context, counters, measurements);
|
|
}
|
|
}
|