KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > stats > counter > sampled > SampledCounterImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.stats.counter.sampled;
5
6 import com.tc.stats.LossyStack;
7 import com.tc.stats.counter.CounterImpl;
8
9 import java.math.BigDecimal JavaDoc;
10 import java.math.BigInteger JavaDoc;
11 import java.util.TimerTask JavaDoc;
12
13 /**
14  * A counter that keeps sampled values
15  */

16 public class SampledCounterImpl extends CounterImpl implements SampledCounter {
17   private final LossyStack history;
18   private final boolean resetOnSample;
19   private final TimerTask JavaDoc samplerTask;
20   private TimeStampedCounterValue min;
21   private TimeStampedCounterValue max;
22   private Double JavaDoc average = null;
23
24   public SampledCounterImpl(SampledCounterConfig config) {
25     super(config.getInitialValue());
26
27     this.history = new LossyStack(config.getHistorySize());
28     this.resetOnSample = config.isResetOnSample();
29
30     this.samplerTask = new TimerTask JavaDoc() {
31       public void run() {
32         recordSample();
33       }
34     };
35
36     recordSample();
37   }
38
39   public TimeStampedCounterValue getMostRecentSample() {
40     return (TimeStampedCounterValue) this.history.peek();
41   }
42
43   public TimeStampedCounterValue[] getAllSampleValues() {
44     return (TimeStampedCounterValue[]) this.history.toArray(new TimeStampedCounterValue[this.history.depth()]);
45   }
46
47   public void shutdown() {
48     if (samplerTask != null) {
49       samplerTask.cancel();
50     }
51   }
52
53   TimerTask JavaDoc getTimerTask() {
54     return this.samplerTask;
55   }
56
57   synchronized void recordSample() {
58     final long sample;
59     if (resetOnSample) {
60       sample = getAndSet(0L);
61     } else {
62       sample = getValue();
63     }
64
65     final long now = System.currentTimeMillis();
66     TimeStampedCounterValue timedSample = new TimeStampedCounterValue(now, sample);
67
68     if ((min == null) || (sample < min.getCounterValue())) {
69       min = timedSample;
70     }
71
72     if ((max == null) || (sample > max.getCounterValue())) {
73       max = timedSample;
74     }
75
76     average = null; // force average to be computed again
77
history.push(timedSample);
78   }
79
80   public synchronized TimeStampedCounterValue getMin() {
81     return this.min;
82   }
83
84   public synchronized TimeStampedCounterValue getMax() {
85     return this.max;
86   }
87
88   public synchronized double getAverage() {
89     if (average == null) {
90       average = new Double JavaDoc(computeAverage());
91     }
92     return this.average.doubleValue();
93   }
94
95   private double computeAverage() {
96     TimeStampedCounterValue[] all = getAllSampleValues();
97
98     if (all.length == 0) { return 0D; }
99
100     BigInteger JavaDoc total = BigInteger.ZERO;
101     for (int i = 0, n = all.length; i < n; i++) {
102       final long sample = all[i].getCounterValue();
103       total = total.add(new BigInteger JavaDoc(String.valueOf(sample)));
104     }
105
106     return new BigDecimal JavaDoc(total).divide(new BigDecimal JavaDoc(all.length), BigDecimal.ROUND_HALF_UP).doubleValue();
107   }
108 }
109
Popular Tags