View Javadoc

1   /*
2    * $Id: Average.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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 Average extends AggregateCounter
16  {
17  
18      private double sum = 0;
19      private long times = 0;
20  
21      public Average(String name, AbstractCounter base)
22      {
23          super(name, Type.AVERAGE, base);
24      }
25  
26      public double nextValue()
27      {
28          return (times > 0) ? sum / times : 0;
29      }
30  
31      public void doCompute()
32      {
33          this.sum += getBase().nextValue();
34          this.times++;
35      }
36  
37  }