Class SimpleMovingAverage


  • @NotThreadSafe
    public class SimpleMovingAverage
    extends java.lang.Object
    Helps calculate simple moving averages for a number of values. Only the last values added to the series will be included in the calculation.
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleMovingAverage​(int count)
      Creates a new instance of the SimpleMovingAverage class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int value)
      Adds the given value to the moving average.
      double getAverage​(double defaultValue)
      Gets a value indicating the current moving average.
      void reset()
      Clears the SimpleMovingAverage of any data.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SimpleMovingAverage

        public SimpleMovingAverage​(int count)
        Creates a new instance of the SimpleMovingAverage class.
        Parameters:
        count - The maximum number of elements to keep track of.
    • Method Detail

      • add

        public void add​(int value)
        Adds the given value to the moving average. If the moving average is already at capacity, this will overwrite the oldest value in the series.
        Parameters:
        value - The value to add.
      • reset

        public void reset()
        Clears the SimpleMovingAverage of any data.
      • getAverage

        public double getAverage​(double defaultValue)
        Gets a value indicating the current moving average.
        Parameters:
        defaultValue - The default value to use.
        Returns:
        The average, or defaultValue if there are no values recorded.