01: /**
02: *
03: */package clime.messadmin.model.stats;
04:
05: import java.io.Serializable;
06: import java.util.Date;
07:
08: /**
09: * Used for collecting data and building basic statistics.
10: *
11: * @author Cédrik LIME
12: */
13: public class MinMaxTracker extends HitsCounter implements Serializable {
14: protected volatile long lastValue = 0;
15:
16: protected volatile long min = Long.MAX_VALUE;
17: /** The time this object was updated for a min */
18: protected volatile long minAccessTime = 0;
19:
20: protected volatile long max = Long.MIN_VALUE;
21: /** The time this object was updated for a max */
22: protected volatile long maxAccessTime = 0;
23:
24: public MinMaxTracker() {
25: super ();
26: }
27:
28: public MinMaxTracker(long min, long max, long current) {
29: super ();
30: this .min = min;
31: this .max = max;
32: this .lastValue = current;
33: }
34:
35: public void addValue(long value) {
36: addValue(value, System.currentTimeMillis());
37: }
38:
39: /** Calculate aggregate stats (min, max, etc.) */
40: public void addValue(long value, long currentTimeMillis) {
41: lastValue += value;
42: registerValue(lastValue, currentTimeMillis);
43: }
44:
45: public void registerValue(long value) {
46: registerValue(value, System.currentTimeMillis());
47: }
48:
49: /** Calculate aggregate stats (min, max, etc.) */
50: public void registerValue(long value, long currentTimeMillis) {
51: lastValue = value;
52:
53: if (value < min) {
54: min = value;
55: minAccessTime = currentTimeMillis;
56: }
57:
58: if (value > max) {
59: max = value;
60: maxAccessTime = currentTimeMillis;
61: }
62:
63: super .hit(currentTimeMillis);
64: }
65:
66: public long getLastValue() {
67: return lastValue;
68: }
69:
70: public long getMin() {
71: return min;
72: }
73:
74: public long getMax() {
75: return max;
76: }
77:
78: public Date getMinAccessTime() {
79: return new Date(minAccessTime);
80: }
81:
82: public Date getMaxAccessTime() {
83: return new Date(maxAccessTime);
84: }
85:
86: protected StringBuffer toStringBuffer() {
87: StringBuffer buffer = super .toStringBuffer().append(',');
88: buffer.append("lastValue=").append(getLastValue()).append(',');//$NON-NLS-1$
89: buffer.append("min=").append(getMin()).append(',');//$NON-NLS-1$
90: buffer
91: .append("minAccessTime=").append(getMinAccessTime()).append(',');//$NON-NLS-1$
92: buffer.append("max=").append(getMax()).append(',');//$NON-NLS-1$
93: buffer.append("maxAccessTime=").append(getMaxAccessTime());//$NON-NLS-1$
94: return buffer;
95: }
96: }
|