01: /**
02: *
03: */package clime.messadmin.model.stats;
04:
05: import java.io.Serializable;
06:
07: /**
08: * Used for collecting data and building basic statistics.
09: *
10: * @author Cédrik LIME
11: */
12: public class StatisticsAgregator extends MinMaxTracker implements
13: Serializable {
14: protected volatile double total = 0.0;
15: /** Intermediate value used to calculate std dev */
16: protected volatile double sumOfSquares = 0.0;
17:
18: public StatisticsAgregator() {
19: super ();
20: }
21:
22: public StatisticsAgregator(long min, long max) {
23: super (min, max, 0);
24: }
25:
26: public void registerValue(long value) {
27: registerValue(value, System.currentTimeMillis());
28: }
29:
30: /** Calculate aggregate stats (min, max, etc.) */
31: public void registerValue(long value, long currentTimeMillis) {
32: total += value;
33: sumOfSquares += value * value;
34: super .registerValue(value, currentTimeMillis);
35: }
36:
37: public double getTotal() {
38: return total;
39: }
40:
41: public double getAvg() {
42: if (hits == 0) {
43: return 0.0;
44: } else {
45: return total / hits;
46: }
47: }
48:
49: public double getStdDev() {
50: double stdDeviation = 0.0;
51: if (hits != 0) {
52: double sumOfX = total;
53: int n = hits;
54: int nMinus1 = (n <= 1) ? 1 : n - 1; // avoid 0 divides;
55:
56: double numerator = sumOfSquares - ((sumOfX * sumOfX) / n);
57: stdDeviation = Math.sqrt(numerator / nMinus1);
58: }
59:
60: return stdDeviation;
61: }
62:
63: protected StringBuffer toStringBuffer() {
64: StringBuffer buffer = super .toStringBuffer().append(',');
65: buffer.append("total=").append(getTotal()).append(',');//$NON-NLS-1$
66: buffer.append("avg*=").append(getAvg()).append(',');//$NON-NLS-1$
67: buffer.append("stdDev*=").append(getStdDev());//$NON-NLS-1$
68: return buffer;
69: }
70:
71: }
|