use of com.biglybt.core.util.average.Average in project BiglyBT by BiglySoftware.
the class CoreStats method setEnableAverages.
public static synchronized void setEnableAverages(boolean enabled) {
if (enabled == enable_averages) {
return;
}
enable_averages = enabled;
if (enabled) {
if (average_timer == null) {
average_timer = new Timer("CoreStats:average");
averages = new HashMap();
average_timer.addPeriodicEvent(AVERAGE_PERIOD, new TimerEventPerformer() {
private final Map ave = averages;
@Override
public void perform(TimerEvent event) {
Map stats = getStatsSupport(null);
Iterator it = stats.entrySet().iterator();
boolean new_averages = false;
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
Object value = entry.getValue();
if (value instanceof Long) {
long last_value;
Average a;
boolean new_average;
Object[] a_entry = (Object[]) ave.get(key);
if (a_entry == null) {
a = AverageFactory.MovingImmediateAverage(10);
last_value = 0;
a_entry = new Object[] { a, value };
ave.put(key, a_entry);
new_averages = new_average = true;
} else {
a = (Average) a_entry[0];
last_value = ((Long) a_entry[1]).longValue();
new_average = false;
}
if (stats_types.get(key) == CUMULATIVE) {
if (!new_average) {
a.update(((Long) value).longValue() - last_value);
}
} else {
a.update(((Long) value).longValue());
}
a_entry[1] = value;
}
}
if (new_averages) {
fireProvidersChangeListeners();
}
}
});
}
} else {
if (average_timer != null) {
average_timer.destroy();
average_timer = null;
averages = null;
}
}
}
Aggregations