View Javadoc

1   /*
2    * $Id: InstantRate.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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 InstantRate extends AggregateCounter
16  {
17  
18      private double firstTime;
19      private double lastTime;
20      private double value;
21  
22      public InstantRate(String name, AbstractCounter base)
23      {
24          super(name, Type.INSTANT_RATE, base);
25      }
26  
27      public double nextValue()
28      {
29          if (firstTime == 0 || firstTime == lastTime)
30          {
31              return Double.NaN;
32          }
33          else
34          {
35              return value / (lastTime - firstTime) * 1000.0;
36          }
37      }
38  
39      public void doCompute()
40      {
41          firstTime = lastTime;
42          lastTime = System.currentTimeMillis();
43          value = getBase().nextValue();
44      }
45  
46  }