View Javadoc

1   /*
2    * $Id: TimeAverage.java 8077 2007-08-27 20:15:25Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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  }