1
2
3
4
5
6
7
8
9
10
11 package org.mule.util.counters.impl;
12
13 import org.mule.util.counters.CounterFactory.Type;
14
15 public class TimeAverage extends AggregateCounter
16 {
17
18 private double sum = 0.0;
19 private double lastValue = 0.0;
20 private final long firstTime = System.currentTimeMillis();
21 private long lastTime = firstTime;
22
23 public TimeAverage(String name, AbstractCounter base)
24 {
25 super(name, Type.AVERAGE, base);
26 }
27
28 public double nextValue()
29 {
30 long current = System.currentTimeMillis();
31 return (sum + lastValue * (current - this.lastTime)) / (current - firstTime);
32 }
33
34 public void doCompute()
35 {
36 long current = System.currentTimeMillis();
37 this.sum += this.lastValue * (current - this.lastTime);
38 this.lastValue = getBase().nextValue();
39 this.lastTime = current;
40 }
41
42 }