FileDocCategorySizeDatePackage
MovingImmediateAverage.javaAPI DocAzureus 3.0.3.42049Tue Mar 21 15:01:34 GMT 2006com.aelitis.azureus.core.util.average

MovingImmediateAverage

public class MovingImmediateAverage extends Object implements Average
Implements a basic moving average.

Fields Summary
private final int
periods
private double[]
data
private int
pos
Constructors Summary
public MovingImmediateAverage(int periods)
Create a new moving average.


   
            
      
      this.periods = periods;
      this.data = new double[periods];
      for (int i=0; i < periods; i++) { data[i] = 0.0; }
   
Methods Summary
private doublecalculateAve()

      double sum = 0.0;
      int	lim = pos>periods?periods:pos;
      for (int i=0; i < lim; i++) {
         sum += data[i];
      }
      return sum / lim;
   
public doublegetAverage()
Return average-so-far.

 return calculateAve(); 
public voidreset()

	   pos = 0;
   
public doubleupdate(double newValue)
Update average and return average-so-far.

      data[pos++%periods] = newValue;
      if ( pos==Integer.MAX_VALUE){
    	  pos = pos%periods;
      }
      return calculateAve();