View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util.counters.impl;
8   
9   import org.mule.util.counters.CounterFactory.Type;
10  
11  public class TimeAverage extends AggregateCounter
12  {
13  
14      private double sum = 0.0;
15      private double lastValue = 0.0;
16      private final long firstTime = System.currentTimeMillis();
17      private long lastTime = firstTime;
18  
19      public TimeAverage(String name, AbstractCounter base)
20      {
21          super(name, Type.AVERAGE, base);
22      }
23  
24      public double nextValue()
25      {
26          long current = System.currentTimeMillis();
27          return (sum + lastValue * (current - this.lastTime)) / (current - firstTime);
28      }
29  
30      public void doCompute()
31      {
32          long current = System.currentTimeMillis();
33          this.sum += this.lastValue * (current - this.lastTime);
34          this.lastValue = getBase().nextValue();
35          this.lastTime = current;
36      }
37  
38  }